用于 websocket-rails gem 的 Ruby websocket 客户端 [英] Ruby websocket client for websocket-rails gem

查看:68
本文介绍了用于 websocket-rails gem 的 Ruby websocket 客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要使用 websocket 功能与外部 ruby​​ 客户端通信的 rails 网页.为了做到这一点,我在 rails 服务器中使用了 websocket-rails gem,定义了 client_connected client_disconnected 事件和从客户端接收消息的特定操作 (new_message).

I am developing a rails webpage which need to use websocket functionality to communicate with an external ruby client. In order to do this, I am using the websocket-rails gem in the rails server, definning the client_connected client_disconnected event and a specific action to received the messages from the client (new_message).

在客户端,我尝试使用不同的 ruby​​ gems,例如 faye-websocket-rubywebsocket-client-simple,但我总是在尝试时遇到错误发送信息.在服务器上,我找不到处理这些消息的方法.两个 gem 都有一个发送方法,只接受一个字符串(不能指定事件的名称)

On the client side I have tried to use different ruby gems like faye-websocket-ruby and websocket-client-simple but I always obtain errors when I try to send a message. On the server I can't find the way to process these messages. Both gems has a send method with only accepts a string (not being able to specify the name of the event)

我一直在使用的代码如下:

The code that I have been using is the following:

服务器端

app/controllers/chat_controller.rb

class ChatController < WebsocketRails::BaseController
  def new_message
    puts ')'*40
  end

  def client_connected
    puts '-'*40
  end

  def client_disconnected
    puts '&'*40
  end
end

config/events.rb

WebsocketRails::EventMap.describe do
  subscribe :client_connected, :to => ChatController, :with_method => :client_connected

  subscribe :message, :to => ChatController, :with_method => :new_message

  subscribe :client_disconnected, :to => ChatController, :with_method => :client_disconnected
end

config/initializers/websocket_rails.rb

WebsocketRails.setup do |config|
  config.log_path = "#{Rails.root}/log/websocket_rails.log"
  config.log_internal_events = true
  config.synchronize = false
end

客户端

websocket-client-simple

require 'rubygems'
require 'websocket-client-simple'

ws = WebSocket::Client::Simple.connect 'ws://localhost:3000/websocket'

ws.on :message do |msg|
  puts msg.data
end

ws.on :new_message do
  hash = { channel: 'example' }
  ws.send hash
end

ws.on :close do |e|
  p e
  exit 1
end

ws.on :error do |e|
  p e
end

hash = { channel: 'Example', message: 'Example' }
ws.send 'new_message', hash

loop do
  ws.send STDIN.gets.strip
end

faye-websocket

require 'faye/websocket'
require 'eventmachine'

EM.run {
  ws = Faye::WebSocket::Client.new('ws://localhost:3000/websocket')

  ws.on :open do |event|
    p [:open]
  end

  ws.on :message do |event|
    p [:message, event.data]
  end

  ws.on :close do |event|
    p [:close, event.code, event.reason]
    ws = nil
  end

  ws.send( 'Example Text' )
}

提前致谢.问候.

PD:如果您需要更多代码,请告诉我.

PD: Let me know if you need more code.

推荐答案

我终于找到了解决方案.问题是消息需要使用特定格式构建才能被websocket理解-导轨.

Finally I have found the solution. The problem was that the message needs to be constructed with a certain format in order to be understood by websocket-rails.

示例:ws.send('["new_message",{"data":"示例消息"}]')

其中 new_message 是 websocket-rails 正在侦听的事件.

where new_message is the event which websocket-rails is listening to.

这篇关于用于 websocket-rails gem 的 Ruby websocket 客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆