I want to create a connection between rails ActionCable
which will act as Server
and NodeJs
as Client
.
我想在rails ActionCable之间创建一个连接,它将充当服务器,node . js作为客户端。
This is my code in connection.rb
file.
这是我的连接代码。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
This is code of my channel.
这是我的频道代码。
# app/channels/socket_connection_channel.rb
class SocketConnectionChannel < ApplicationCable::Channel
def subscribed
stream_from "socket_connect"
end
def speak
ActionCable.server.broadcast("socket_connect",
message: "Connected")
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end
And this is NodeJs code in server.js
file
这是服务器中的NodeJs代码。js文件
const WebSocket = require('ws');
const ws = new WebSocket('ws://0.0.0.0:3001/cable');
ws.on('open', function open() {
ws.send(JSON.stringify({'data': 'Sample Data'}));
});
ws.on('message', function incoming(data) {
console.log(data);
});
ws.on('socket_connected', function incoming(data) {
console.log('socket');
});
When I run the server node server
当我运行服务器节点服务器时。
{"type":"welcome"}
{"type":"ping","message":1497487145}
{"type":"ping","message":1497487148}
{"type":"ping","message":1497487151}
and on rails server displays the following logs
在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"}
What I want to achieve is that node server
will make the Websocket connection on rails ActionCable
and subscribe to SocketConnect
Channel and will transmit
the data to Rails server via this channel.
我想实现的是节点服务器将在rails ActionCable上进行Websocket连接并订阅SocketConnect通道,并通过该通道将数据传输到rails服务器。
I have found some examples of chat
application on rails action server where the client
and server
both are on smae rails
platform. But, I haven't found any example where client
is on separate palatform than server.
我在rails action server上找到了一些聊天应用程序的示例,其中客户机和服务器都位于smae rails平台上。但是,我还没有找到任何客户端在不同的palatform上而不是服务器上的例子。
But, I am unable to find any method to Subscribe
for this channel and make a stable connection on both ends to transmit data and I also don't know how to get data from this request.
但是,我找不到任何方法来订阅这个通道并在两端建立一个稳定的连接来传输数据,我也不知道如何从这个请求中获取数据。
Please help me in this. Thanks in advance
请在这件事上帮助我。谢谢提前
1 个解决方案
#1
2
Try this. Sending subscribe and setting identifier for socketchannel is important point.
试试这个。为socketchannel发送订阅和设置标识符是很重要的一点。
rails
# 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
node.js
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();
node.js
App.sendmessage("Hello world");
https://github.com/mwalsher/actioncable-js is also helpful for you.
https://github.com/mwalsher/actioncable-js对您也有帮助。
#1
2
Try this. Sending subscribe and setting identifier for socketchannel is important point.
试试这个。为socketchannel发送订阅和设置标识符是很重要的一点。
rails
# 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
node.js
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();
node.js
App.sendmessage("Hello world");
https://github.com/mwalsher/actioncable-js is also helpful for you.
https://github.com/mwalsher/actioncable-js对您也有帮助。