Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails 5-ActionCable-未能升级到WebSocket_Ruby On Rails_Node.js_Websocket_Ruby On Rails 5 - Fatal编程技术网

Ruby on rails Rails 5-ActionCable-未能升级到WebSocket

Ruby on rails Rails 5-ActionCable-未能升级到WebSocket,ruby-on-rails,node.js,websocket,ruby-on-rails-5,Ruby On Rails,Node.js,Websocket,Ruby On Rails 5,我正在尝试在MyRails 5应用程序上实现webSocket 这是我的频道: class MessagesChannel < ApplicationCable::Channel def subscribed stream_from "messages_#{params[:topic]}" end end 和我的连接类(也使用设备): 这就是我得到的: Started GET "/cable" for 127.0.0.1 at 2017-03-13 14:25:01 -0

我正在尝试在MyRails 5应用程序上实现webSocket

这是我的频道:

class MessagesChannel < ApplicationCable::Channel
  def subscribed
    stream_from "messages_#{params[:topic]}"
  end
end
和我的连接类(也使用设备):

这就是我得到的:

Started GET "/cable" for 127.0.0.1 at 2017-03-13 14:25:01 -0300
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2017-03-13 14:25:01 -0300
Request origin not allowed:
Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
Finished "/cable/" [WebSocket] for 127.0.0.1 at 2017-03-13 14:25:01 -0300
我也不知道如何在NodeJS客户端上指定要侦听的通道。这也会有帮助

编辑1-尝试将以下内容添加到development.rb

config.action_cable.allowed_request_origins = ['http://localhost:3000']
编辑2-还尝试在节点客户端上添加频道,如下所示:

const ws = new WebSocket('ws://localhost:3000/cable', `messages_${topic}`);
但两次尝试都没有成功

编辑3-另一个客户端测试:

  ws.on('open', function open() {
    console.log("on open!!");
     ws.send('{"command":"subscribe","identifier":"{\"channel\":\"MessagesChannel\"}"');
  });

您的客户端在哪个端口上运行?您希望允许客户端,因此如果rails在localhost 3000上运行,则需要将其更改为正确的源url:

config.action_cable.allowed_request_origins = ['http://localhost:3000']
为了开发,我将使用regex暂时允许所有来源:

config.action_cable.allowed_request_origins = [ /http:\/\/.*/, /https:\/\/.*/ ]
此外,为了更容易地测试连接,请使用此JSFIDLE-它现在可以消除客户端的设置,以便能够连接:

一旦连接好,我建议您使用这个库从客户端订阅action cable频道,因为您提到了Node,我假设它将用JavaScript编写


多么棒的回答!所以我已经测试过了。我接通了。它起作用了。但是有个问题。我正在使用nodejs来测试它。但我将有另一个客户端使用react native(它使用WebSocket)。那么,我如何只使用WebSocket“订阅”频道呢?或者我必须使用另一个库来实现这一点?另一个注意事项:使用文档。并且我的节点应用程序未在浏览器上运行:(
  ws.on('open', function open() {
    console.log("on open!!");
     ws.send('{"command":"subscribe","identifier":"{\"channel\":\"MessagesChannel\"}"');
  });
config.action_cable.allowed_request_origins = ['http://localhost:3000']
config.action_cable.allowed_request_origins = [ /http:\/\/.*/, /https:\/\/.*/ ]