ruby-on-rails – 如何通过rails上的ruby中的websocket发送保持活动数据包

ruby-on-rails – 如何通过rails上的ruby中的websocket发送保持活动数据包,第1张

概述我想送一个 “Keep alive from client” 我的websocket连接每30秒发送一条消息.这是我在websocket初始化程序中的代码如下所示: ws = WebSocket::Client::Simple.connect 'wss://bitcoin.toshi.io/'ws.on :message do |msg| rawJson = msg.data mess 我想送一个 @H_301_7@

@H_301_7@

@H_301_7@“Keep alive from clIEnt”

@H_301_7@我的websocket连接每30秒发送一条消息.这是我在websocket初始化程序中的代码如下所示:

@H_301_7@

ws = WebSocket::ClIEnt::Simple.connect 'wss://bitcoin.toshi.io/'ws.on :message do |msg|  rawJson = msg.data  message_response = JsON.parse(rawJson)endws.on :open do  ws.send "{\"subscribe\":\"blocks\"}"endws.on :close do |e|  puts "WEBSOCKET HAS CLOSED #{e}"  exit 1endws.on :error do |e|  puts "WEBSOCKET ERROR #{e}"end
@H_301_7@没有任何“保持活力”,连接在大约45秒内关闭.我应该如何发送’心跳’数据包?似乎连接由他们的服务器关闭,而不是我的.

解决方法 您可以使用 Websocket Eventmachine Client gem发送听力: @H_301_7@

@H_301_7@

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 "{}"  endend
@H_301_7@您可以使用EM :: add_periodic_timer(interval_in_seconds)为EventMachine设置计时器,然后使用它发送心跳.

总结

以上是内存溢出为你收集整理的ruby-on-rails – 如何通过rails上的ruby中的websocket发送保持活动数据包全部内容,希望文章能够帮你解决ruby-on-rails – 如何通过rails上的ruby中的websocket发送保持活动数据包所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1276649.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存