从rails runner

时间:2015-06-27 00:10:20

标签: ruby-on-rails websocket faye websocket-rails

我有一个带有websocket-rails gem的Rails应用程序。

在我的应用程序中,我使用rails runner MyDaemon.start

启动了一个守护进程

我正在使用websocket-rails Synchronization,因此我的config/initializers/websocket_rails.rb看起来像这样:

WebsocketRails.setup do |config|
  config.log_internal_events = false
  config.standalone = false
  config.synchronize = true
end

MyDaemon内,使用同步功能,我可以触发同时覆盖WebsocketRails::BaseController和我的javascript WebSocketRails的事件。

我要做的是找到一种方法来绑定我MyDaemon的事件。

我尝试使用faye-websocket-rubywebsocket-client-simple来实现一个简单的WebSocket客户端,但是在我的键盘敲了一段时间后,我发现有某种“握手” “使用connection_id消息中的client_connected进行处理。基本上this other so question中提供的解决方案都不适用于我。

我需要了解是否在MyDaemon内部我可以直接订阅某些WebsocketRails回调,甚至在EventMachine中,或者我应该如何在Ruby中实现Websocket客户端。

我最后一次尝试拥有ruby客户端可以在this gist中找到,这是一个示例输出:

ruby client.rb ws://localhost:3000/websocket
[:open, {"upgrade"=>"websocket", "connection"=>"Upgrade", "sec-websocket-accept"=>"zNTdGvxFKJeP+1PyGf27T4x2PGo="}]

JSON message is
[["client_connected", {"id"=>nil, "channel"=>nil, "user_id"=>nil, "data"=>{"connection_id"=>"4b7b91001befb160d17b"}, "success"=>nil, "result"=>nil, "token"=>nil, "server_token"=>nil}]]

client id is 4b7b91001befb160d17b
[:message, "[[\"client_connected\",{\"id\":null,\"channel\":null,\"user_id\":null,\"data\":{\"connection_id\":\"4b7b91001befb160d17b\"},\"success\":null,\"result\":null,\"token\":null,\"server_token\":null}]]"]

JSON message is
[["websocket_rails.ping", {"id"=>nil, "channel"=>nil, "user_id"=>nil, "data"=>{}, "success"=>nil, "result"=>nil, "token"=>nil, "server_token"=>nil}]]

Sending ["pong",{}]
[:message, "[[\"websocket_rails.ping\",{\"id\":null,\"channel\":null,\"user_id\":null,\"data\":{},\"success\":null,\"result\":null,\"token\":null,\"server_token\":null}]]"]
[:close, 1006, ""]

websocket-rails的日志是:

I [2015-06-27 02:08:45.250] [ConnectionManager] Connection opened: #<Connection::2b3dddaf3ec4ed5e3550>
I [2015-06-27 02:08:45.251] [Dispatcher] Started Event: client_connected
I [2015-06-27 02:08:45.251] [Dispatcher] Name: client_connected
I [2015-06-27 02:08:45.251] [Dispatcher] Data: {"connection_id"=>"2b3dddaf3ec4ed5e3550"}
I [2015-06-27 02:08:45.251] [Dispatcher] Connection: #<Connection::2b3dddaf3ec4ed5e3550>
I [2015-06-27 02:08:45.251] [Dispatcher] Event client_connected Finished in 0.000174623 seconds
I [2015-06-27 02:09:05.252] [ConnectionManager] Connection closed: #<Connection::2b3dddaf3ec4ed5e3550>
I [2015-06-27 02:09:05.252] [Dispatcher] Started Event: client_disconnected
I [2015-06-27 02:09:05.252] [Dispatcher] Name: client_disconnected
I [2015-06-27 02:09:05.252] [Dispatcher] Connection: #<Connection::2b3dddaf3ec4ed5e3550>
I [2015-06-27 02:09:05.253] [Dispatcher] Event client_disconnected Finished in 0.000236669 seconds

可能我错过了一些非常愚蠢的东西,所以我来这里请你帮忙!

1 个答案:

答案 0 :(得分:0)

您可以将Iodine用作websocket客户端(我是作者):

require 'iodine/http'
# prevents the Iodine's server from running
Iodine.protocol = :timer
# starts Iodine while the script is still running
Iodine.force_start!
options = {}
options[:on_open] = Proc.new {puts 'Connection Open'; write "Hello World!" }
options[:on_close] = Proc.new {puts 'Connection Closed'}
options[:on_message] = Proc.new {|data| puts "I got: #{data}" }

# connect to an echo server for demo. Use the blocking method:
websocket = Iodine::Http::WebsocketClient.connect "wss://echo.websocket.org/", options
websocket << "sending data"
sleep 0.5
websocket.close

另外,在阅读时我注意到websocket-rails宝石并没有被更新。见this question

作为替代方案,您可以使用Plezi framework(我是作者)在您的Rails应用中运行websockets。

在同一台服务器上同时使用这两个框架非常容易。这样,您就可以在Plezi Websocket控制器中使用Rails模型的代码。

因为Plezi将管理websockets并且Rails可能会呈现404 Not Found页面,所以Plezi的路线将优先...但只要您的路线不会互相覆盖,您就会#39 ;重新金色。

请注意,为了让两个应用程序一起运行,Plezi将强制您使用Iodine server作为Rack服务器。为避免这种情况,您可以使用Placebo API并在不同的流程上运行Plezi。

您可以阅读更多框架&#39; README file