我有一個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-ruby和websocket-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
也許我錯過了一些非常愚蠢的東西,所以我在這裏要求你的幫助!