如何在 ruby rails 中通过 websocket 发送 keep-alive 数据包
How to send a keep-alive packet through websocket in ruby on rails
我要发一个
"Keep alive from client"
我的 websocket 连接每 30 秒发送一次消息。这是我的 websocket 初始化程序中的代码:
ws = WebSocket::Client::Simple.connect 'wss://bitcoin.toshi.io/'
ws.on :message do |msg|
rawJson = msg.data
message_response = JSON.parse(rawJson)
end
ws.on :open do
ws.send "{\"subscribe\":\"blocks\"}"
end
ws.on :close do |e|
puts "WEBSOCKET HAS CLOSED #{e}"
exit 1
end
ws.on :error do |e|
puts "WEBSOCKET ERROR #{e}"
end
没有任何形式的 'keep alive',连接将在大约 45 秒后关闭。我应该如何发送 'heart-beat' 数据包?好像是他们的服务器关闭了连接,不是我的。
可以使用Websocket Eventmachine Clientgem发送心跳:
require 'websocket-eventmachine-client'
EM.run do
ws = WebSocket::EventMachine::Client.connect(:uri => 'wss://bitcoin.toshi.io/')
puts ws.comm_inactivity_timeout
ws.onopen do
puts "Connected"
end
ws.onmessage do |msg, type|
puts "Received message: #{msg}"
end
ws.onclose do |code, reason|
puts "Disconnected with status code: #{code}"
end
EventMachine.add_periodic_timer(15) do
ws.send "{}"
end
end
您可以使用 EM::add_periodic_timer(interval_in_seconds)
为 EventMachine 设置定时器,然后用它发送您的心跳。
如果您使用 Iodine's Websocket 客户端,您可以使用 auto-ping 功能(默认且无法关闭):
require 'iodine/http'
# prevents the Iodine's server from running
Iodine.protocol = :timer
# starts Iodine while the script is still running
Iodine.force_start!
# set pinging to a 40 seconds interval.
Iodine::Http::Websockets.default_timeout = 40
settings = {}
# set's the #on_open event callback.
settings[:on_open] = Proc.new do
write 'sending this connection string.'
end
# set's the #on_message(data) event callback.
settings[:on_message] = Proc.new { |data| puts "Received message: #{data}" }
# connects to the websocket
Iodine::Http.ws_connect 'ws://localhost:8080', settings
这是一个相当基本的客户端,但也易于管理。
编辑
Iodine 还包括一些 cookie 和自定义 header 的支持,如现在在 Iodine's documentation 中所见。因此可以使用不同的身份验证技术(身份验证 headers 或 cookies)。
我要发一个
"Keep alive from client"
我的 websocket 连接每 30 秒发送一次消息。这是我的 websocket 初始化程序中的代码:
ws = WebSocket::Client::Simple.connect 'wss://bitcoin.toshi.io/'
ws.on :message do |msg|
rawJson = msg.data
message_response = JSON.parse(rawJson)
end
ws.on :open do
ws.send "{\"subscribe\":\"blocks\"}"
end
ws.on :close do |e|
puts "WEBSOCKET HAS CLOSED #{e}"
exit 1
end
ws.on :error do |e|
puts "WEBSOCKET ERROR #{e}"
end
没有任何形式的 'keep alive',连接将在大约 45 秒后关闭。我应该如何发送 'heart-beat' 数据包?好像是他们的服务器关闭了连接,不是我的。
可以使用Websocket Eventmachine Clientgem发送心跳:
require 'websocket-eventmachine-client'
EM.run do
ws = WebSocket::EventMachine::Client.connect(:uri => 'wss://bitcoin.toshi.io/')
puts ws.comm_inactivity_timeout
ws.onopen do
puts "Connected"
end
ws.onmessage do |msg, type|
puts "Received message: #{msg}"
end
ws.onclose do |code, reason|
puts "Disconnected with status code: #{code}"
end
EventMachine.add_periodic_timer(15) do
ws.send "{}"
end
end
您可以使用 EM::add_periodic_timer(interval_in_seconds)
为 EventMachine 设置定时器,然后用它发送您的心跳。
如果您使用 Iodine's Websocket 客户端,您可以使用 auto-ping 功能(默认且无法关闭):
require 'iodine/http'
# prevents the Iodine's server from running
Iodine.protocol = :timer
# starts Iodine while the script is still running
Iodine.force_start!
# set pinging to a 40 seconds interval.
Iodine::Http::Websockets.default_timeout = 40
settings = {}
# set's the #on_open event callback.
settings[:on_open] = Proc.new do
write 'sending this connection string.'
end
# set's the #on_message(data) event callback.
settings[:on_message] = Proc.new { |data| puts "Received message: #{data}" }
# connects to the websocket
Iodine::Http.ws_connect 'ws://localhost:8080', settings
这是一个相当基本的客户端,但也易于管理。
编辑
Iodine 还包括一些 cookie 和自定义 header 的支持,如现在在 Iodine's documentation 中所见。因此可以使用不同的身份验证技术(身份验证 headers 或 cookies)。