Ruby on rails Rails ActionCable连接(服务器)与NodeJs(客户端)
我想在railsRuby on rails Rails ActionCable连接(服务器)与NodeJs(客户端),ruby-on-rails,node.js,websocket,client-server,actioncable,Ruby On Rails,Node.js,Websocket,Client Server,Actioncable,我想在railsActionCable之间创建一个连接,它将充当Server和NodeJs作为Client 这是我在connection.rb文件中的代码 # app/channels/application_cable/connection.rb module ApplicationCable class Connection < ActionCable::Connection::Base identified_by :uuid def connect
ActionCable
之间创建一个连接,它将充当Server
和NodeJs
作为Client
这是我在connection.rb
文件中的代码
# app/channels/application_cable/connection.rb
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :uuid
def connect
self.uuid = SecureRandom.uuid
end
end
end
当我运行服务器节点服务器时
{"type":"welcome"}
{"type":"ping","message":1497487145}
{"type":"ping","message":1497487148}
{"type":"ping","message":1497487151}
在rails服务器上显示以下日志
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2017-06-15
02:39:02 +0200
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
Registered connection (5db66385-0923-4633-8837-ba957fc0a7f5)
Received unrecognized command in {"data"=>"Sample Data"}
我想要实现的是节点服务器
将在rails上建立Websocket连接ActionCable
并订阅SocketConnect
频道,并将数据通过该频道传输到rails服务器
我在rails action server上找到了一些聊天
应用程序的示例,其中客户端
和服务器
都位于smaerails
平台上。但是,我还没有发现任何客户机
与服务器在不同表单上的示例
但是,我找不到任何方法来订阅这个频道,并在两端建立稳定的连接来传输数据,我也不知道如何从这个请求中获取数据
请帮我做这件事。提前感谢试试这个。发送订阅并设置socketchannel的标识符是很重要的一点
轨道
node.js
对您也有帮助。谢谢您的回复。当我在单独的node.js
文件中编写此代码并使用node node.js
命令运行服务器时,它会显示以下错误<代码>(函数(导出、要求、模块、文件名、目录名){window.App={}
ReferenceError:window未定义
window不是必需的。声明var-App={}后只需执行它=>“var-App={};”
我仍然收到错误窗口未定义
。我假设它与HTML
/DOM
对象关联。但我只使用js文件运行服务器
。我不知道如何初始化窗口
。谢谢。你太棒了。它成功了:)。您是否可以共享任何引用或来源,其中提供了其他方法/函数的所有详细信息/用法。请参考此。这将更有帮助。:D
{"type":"welcome"}
{"type":"ping","message":1497487145}
{"type":"ping","message":1497487148}
{"type":"ping","message":1497487151}
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2017-06-15
02:39:02 +0200
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
Registered connection (5db66385-0923-4633-8837-ba957fc0a7f5)
Received unrecognized command in {"data"=>"Sample Data"}
# app/channels/socket_connection_channel.rb
class SocketConnectionChannel < ApplicationCable::Channel
def subscribed
@uuid = params[:uuid]
stop_all_streams
stream_from "socket_connect_#{@uuid}"
logger.info ">>> Subscribed #{@uuid}!"
end
def speak
logger.info "[ActionCable] received message : #{data['message']}"
ActionCable.server.broadcast "socket_connect_#{@uuid}", message: "#{data['message']} from server"
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end
var App = {};
App.sendmessage = function(send) {
data = {
message : send,
action : "speak"
};
message = {
command: "message",
identifier: JSON.stringify(App.param),
data: JSON.stringify(data)
};
App.ws.send(JSON.stringify(message));
}
App.connect_server = function() {
const WebSocket = require('ws');
App.ws = new WebSocket('ws://0.0.0.0:3001/cable', ["actioncable-v1-json", "actioncable-unsupported"]);
App.param = {channel: "SocketConnectionChannel", uuid: guid()};
App.ws.on('open', function open() {
data = {
command: "subscribe",
identifier: JSON.stringify(App.param)
}
App.ws.send(JSON.stringify(data));
});
App.ws.on('message', function (event) {
console.log(event);
});
function guid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}
}
App.connect_server();
App.sendmessage("Hello world");