我想使用WebSocket将数据推送到客户端。我现在不知道如何返回数据(从Redis)到客户端。有人能给我解释一下吗?
sockets_controller.rb
class SocketsController < WebsocketRails::BaseController
def create
send_message :new_event, {:message => 'TEST'}
end
end
events.rb
WebsocketRails::EventMap.describe do
subscribe :new_event, 'sockets#create'
end
websocket_rails.rb
WebsocketRails.setup do |config|
config.redis_options = { driver: :ruby }
config.standalone = false
config.synchronize = true
end
客户端:
$socket.on('test', function(data) {
console.log(data);
$scope.events.upshift(data);
if($scope.events.length>50){
$scope.events.length=50;
EventsList =$scope.events;
}
});
routerApp.config(["$socketProvider", function ($socketProvider) {
$socketProvider.setUrl("http://172.16.20.179:3000/events");
}]);
客户端问题如下:
GET http://172.16.20.179:3000/socket.io/?
EIO=3&transport=polling&t=1439215071732-9
XMLHttpRequest cannot load http://172.16.20.179:3000/socket.io/?
EIO=3&transport=polling&t=1439215056291-6. No 'Access-Control-Allow-
Origin' header is present on the requested resource. Origin
'http://localhost:4000' is therefore not allowed access. The response
had HTTP status code 404.
似乎你正在尝试使用Socket.IO
的web套接字。Rails没有支持Socket.IO
的实现。
如果你想要一个关于如何从Angular.JS中与websocket-rails交互的例子,你可以使用下面的指南:
https://medium.com/@karimbutt集成- websocket rails -与- angularjs -或-其他- js框架- 256729 - e95a86