WebSockets 侦听 UNIX 域套接字? [英] WebSockets listening on UNIX domain socket?
问题描述
是否可以在处理 UNIX 域套接字连接的 nginx 服务器后面设置 WebSockets 服务器?
Is it possible to setup a WebSockets server behind an nginx server which handles connections on a UNIX domain socket?
我目前在同一台机器上有多个 WebSocket 服务器实例,并且存在端口共享的问题.所有实例都必须分配一个唯一的端口.我想避免这种情况,而是使用 UNIX 域套接字在 nginx 和 WebSockets 后端之间进行通信.
I currently have several WebSocket server instances on the same machine and have the problem of port sharing. All instances must be allocated a unique port. I want to avoid that and instead communicate between nginx and the WebSockets backend using UNIX domain sockets.
我使用的 WebSockets 库是 ws (https://github.com/einaros/ws)
The WebSockets library I'm using is ws (https://github.com/einaros/ws)
我目前像这样创建我的服务器:
I currently create my server like this:
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({host: "127.0.0.1", port: 8080});
推荐答案
服务器端:
var http = require('http');
var WebSocketServer = require('ws').Server;
var server = http.createServer()
var wss = new WebSocketServer({ server : server});
server.listen('/tmp/server.sock')
客户端:
var ws = require('ws');
var client = new ws("ws+unix:///tmp/server.sock")
client.send('hello')
这篇关于WebSockets 侦听 UNIX 域套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!