2015-06-27 51 views
0

我有一個Rails應用程序,它帶有websocket-rails寶石。觸發器/從軌道轉輪內部訂閱websocket-rail事件

在我的應用程序有,我有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_idclient_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 

也許我錯過了一些非常愚蠢的東西,所以我在這裏要求你的幫助!

回答

0

您可以使用碘作爲一個客戶端的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的路線將優先......但只要您的路線不互相覆蓋,您就是金。

請注意,爲了允許兩個應用程序一起運行,Plezi將強制您使用Iodine server作爲您的Rack服務器。爲了避免這種情況,您可以使用安慰劑API並在另一個進程上運行Plezi。

您可以閱讀更多框架的README file