如何将活动的 WebSocket 传递给 Node.js 中的集群线程?
How to pass an active WebSocket to a clustered thread in Node.js?
在 Node.js 中,他们公开了一种方便的方式,通过以下方式将 net.Sockets 传递给子进程 (cluster.Worker):
var socket; // some instance of net.Socket
var worker = process.fork();
worker.on("online", function() {
worker.send("socket", socket);
});
超级酷而且工作得心应手。但是我如何使用 WebSocket 连接来做到这一点?我愿意尝试任何模块。
目前我尝试使用各种模块,例如 ws。它们中的大多数存储初始 net.Socket HTTP 请求然后对其进行升级,但是 none 似乎很简单,可以作为 net.Socket 传递给子进程,因为它们需要大量握手信息据我所知,WebSocket 规范。
我知道有一些骇人听闻的解决方案,例如在子进程的唯一端口上打开 WebSocket 服务器,然后告诉 WebScoket 连接在该端口上重新连接,但我需要为每个子线程打开一个端口。或者,通过 process.send 将所有数据管道传输到 WebSocket 连接,以便主线程执行所有 io,但这会破坏 运行 多个线程上的一些性能优势。
那么有人有什么想法吗?
幸好我想通了。 ws may have been too much for my intended purposes. Instead I found a pretty obscure WebSocket library, lark-websocket which exposes a function that given a net.Socket
can wrap it up in in their Client class and work with it as a WebSocket. The only issue was both the parent and child threads would then try to ping the connection on the other end so I had to fork it 并添加父线程暂停 ping 的方法。
这里有一些示例代码供感兴趣的人使用:
var cluster = require("cluster");
var ws = require('lark-websocket');
if(cluster.isMaster) { // make a child process and pipe all ws connections to it
var worker = cluster.fork();
worker.once("online", function() {
console.log("worker online with pid", worker.process.pid);
})
ws.createServer(function(client, request){
worker.send("socket", client._socket); // send all websocket clients to the worker thread
}).listen(27015);
}
else { // we are a worker, so we handle the ws connections
process.on("message", function(message, handler) {
if(message === "socket") { // Note: Node js can only send sockets via handler if message === "socket", because passing sockets between threads is sketchy as fuck
var client = ws.createClient(handler);
client.on('message',function(msg){
console.log("worker " + process.pid + " got:", msg);
client.send("I got your: " + msg);
});
}
});
}
在 Node.js 中,他们公开了一种方便的方式,通过以下方式将 net.Sockets 传递给子进程 (cluster.Worker):
var socket; // some instance of net.Socket
var worker = process.fork();
worker.on("online", function() {
worker.send("socket", socket);
});
超级酷而且工作得心应手。但是我如何使用 WebSocket 连接来做到这一点?我愿意尝试任何模块。
目前我尝试使用各种模块,例如 ws。它们中的大多数存储初始 net.Socket HTTP 请求然后对其进行升级,但是 none 似乎很简单,可以作为 net.Socket 传递给子进程,因为它们需要大量握手信息据我所知,WebSocket 规范。
我知道有一些骇人听闻的解决方案,例如在子进程的唯一端口上打开 WebSocket 服务器,然后告诉 WebScoket 连接在该端口上重新连接,但我需要为每个子线程打开一个端口。或者,通过 process.send 将所有数据管道传输到 WebSocket 连接,以便主线程执行所有 io,但这会破坏 运行 多个线程上的一些性能优势。
那么有人有什么想法吗?
幸好我想通了。 ws may have been too much for my intended purposes. Instead I found a pretty obscure WebSocket library, lark-websocket which exposes a function that given a net.Socket
can wrap it up in in their Client class and work with it as a WebSocket. The only issue was both the parent and child threads would then try to ping the connection on the other end so I had to fork it 并添加父线程暂停 ping 的方法。
这里有一些示例代码供感兴趣的人使用:
var cluster = require("cluster");
var ws = require('lark-websocket');
if(cluster.isMaster) { // make a child process and pipe all ws connections to it
var worker = cluster.fork();
worker.once("online", function() {
console.log("worker online with pid", worker.process.pid);
})
ws.createServer(function(client, request){
worker.send("socket", client._socket); // send all websocket clients to the worker thread
}).listen(27015);
}
else { // we are a worker, so we handle the ws connections
process.on("message", function(message, handler) {
if(message === "socket") { // Note: Node js can only send sockets via handler if message === "socket", because passing sockets between threads is sketchy as fuck
var client = ws.createClient(handler);
client.on('message',function(msg){
console.log("worker " + process.pid + " got:", msg);
client.send("I got your: " + msg);
});
}
});
}