Unix域套接字,而不是TCP类型服务器的主机/端口
这适用于所有Node.js版本6+ 假设我目前有一个TCP服务器,它有多个客户端:Unix域套接字,而不是TCP类型服务器的主机/端口,tcp,ipc,unix-socket,Tcp,Ipc,Unix Socket,这适用于所有Node.js版本6+ 假设我目前有一个TCP服务器,它有多个客户端: const server = net.createServer(s => { }); server.listen(6000); const udsPath = path.resolve('some-path.sock'); // same file path as above const ws = net.createConnection(udsPath, () => { }); 我通过客户
const server = net.createServer(s => {
});
server.listen(6000);
const udsPath = path.resolve('some-path.sock'); // same file path as above
const ws = net.createConnection(udsPath, () => {
});
我通过客户端连接到它:
const s1 = net.createConnection({port:6000});
const s2 = net.createConnection({port:6000});
const s3 = net.createConnection({port:6000});
TCP在本地计算机上有时可能有点慢。我听说可能有一种方法可以用Unix域套接字代替主机/端口组合,但要保持TCP服务器风格的接口。这可能吗?如何实现
Node.js文档中提到,您可以创建一个服务器,在以下路径上侦听:
但是它没有指定需要的文件类型以及如何创建该文件。事实证明,在MacOS/Linux上这很容易。您不需要创建文件。您需要确保该文件不存在,然后将Node.js核心库指向一个空路径 对于服务器:
const udsPath = path.resolve('some-path.sock');
const wss = net.createServer(s => {
});
wss.listen(udsPath, () => {
});
对于客户:
const server = net.createServer(s => {
});
server.listen(6000);
const udsPath = path.resolve('some-path.sock'); // same file path as above
const ws = net.createConnection(udsPath, () => {
});