来自不同服务器的http服务器和Web套接字 [英] http server and web sockets from separate servers
问题描述
配置一个http服务器(使用express)和一个分配给它的套接字服务器(socket.io)非常容易:
It's pretty easy to configure a http server (using express) and a socket server (socket.io) assigned to it:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
如何在两个不同的node.js实例中运行http服务器和套接字服务器?
我的想法是通过这种方式利用性能,从而释放http节点实例,也不再将通知发送回客户端.
My idea is to leverage the performance this way, releasing the http node instance from the responsibility of also sending notifications back to the clients.
推荐答案
在常规的Socket.IO + Express应用中,Socket.IO拦截以/socket.io/
开头的请求.
In a regular Socket.IO + Express app, Socket.IO intercepts requests starting with /socket.io/
.
您可以将Nginx(或支持代理的任何其他网络服务器)设置为侦听80端口,如果请求以/socket.io/
开头,则将其代理到Socket.IO进程,否则设置为Express进程.
You may set Nginx (or any other webserver that supports proxying) listening 80 port, and make it proxy to Socket.IO process if request starts with /socket.io/
, and to Express process otherwise.
编辑:要在单独的过程中设置Socket.IO,可以使用以下代码:
To set up Socket.IO in separate process you may use the following code:
var io = require('socket.io')();
io.on('connection', function(socket){
//here you can emit and listen messages
});
io.listen(3000);
这篇关于来自不同服务器的http服务器和Web套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!