简体   繁体   English

如何在nodejs上与主服务器一起运行socketIO服务器

[英]How to run socketIO server together with main server on nodejs

Currently, we have a main server that is being hosted on localhost:3000 but to run our socket.io function, we need to run it on the same server.目前,我们有一个托管在 localhost:3000 上的主服务器,但是要运行我们的 socket.io 函数,我们需要在同一台服务器上运行它。 However, we need to run it separately (npm start separately).但是,我们需要单独运行它(npm start 单独)。 Is there a way to run it together or on the same server without it crashing?有没有办法一起运行它或在同一台服务器上运行它而不会崩溃?

You cannot run socket.io in a separate process, but on the same port as some other web server in some other process.您不能在单独的进程中运行 socket.io,但在与其他进程中的其他 Web 服务器相同的端口上。 The OS will not allow that as only one process can have a listening server on a specific port.操作系统不允许这样做,因为只有一个进程可以在特定端口上拥有一个监听服务器。 If they are in the same process, that's easy as pie as socket.io is built to share an http server in the same process (one listening server internally, traffic divided between the two uses).如果它们在同一个进程中,这很容易,因为 socket.io 被构建为在同一个进程中共享一个 http 服务器(内部一个侦听服务器,流量在两个用途之间分配)。 But, not from separate processes.但是,不是来自单独的进程。

To do that, you'd have to use something like nginx on your port 3000 to proxy plain web requests to one server on some other port say 3001 and socket.io requests to another server on some other port say 3002. The client would only deal with port 3000 and nginx would direct the traffic to the right server on different ports.为此,您必须在端口 3000 上使用 nginx 之类的东西,将纯 Web 请求代理到其他端口上的一个服务器,例如 3001,并将 socket.io 请求代理到其他端口上的另一台服务器,例如 3002。客户端只会处理端口 3000 和 nginx 会将流量引导到不同端口上的正确服务器。


I'm thinking that when you say "npm start separately" , you must have some other problem you're trying to solve with that statement and we could probably help with a better way to solve that actual problem (if you disclosed what that actual requirement is) while keeping socket.io and the http server in the same process and thus no need for a proxy to divide the traffic between two separate servers.我在想,当你说“npm start separate”时,你一定有其他问题要尝试用该语句解决,我们可能会提供更好的方法来解决这个实际问题(如果你披露了那个实际问题要求是)同时将 socket.io 和 http 服务器保持在同一个进程中,因此不需要代理来划分两个独立服务器之间的流量。

For example, you could start up your web server with no socket.io server started and then you could tell your web server process to start up the socket.io server later.例如,您可以在未启动 socket.io 服务器的情况下启动您的 web 服务器,然后您可以告诉您的 web 服务器进程稍后启动 socket.io 服务器。 Or you could start both the web server and socket.io server in the same process at initialization time, but have a temporary server configuration that blocks incoming socket.io connections until some other requirement is met.或者您可以在初始化时在同一个进程中同时启动 Web 服务器和 socket.io 服务器,但是有一个临时服务器配置会阻止传入的 socket.io 连接,直到满足其他一些要求。

But, without understanding what the real requirement is, you're just lobbing us an XY problem where you describe your attempted solution rather than the actual problem that needs to be solved.但是,在不了解真正的需求是什么的情况下,您只是向我们提出了一个XY 问题,您在其中描述了您尝试的解决方案,而不是需要解决的实际问题。 When we explain that your attempted solution is the wrong way to go, we need to know what the real problem is to help further.当我们解释您尝试的解决方案是错误的方法时,我们需要知道真正的问题是什么才能进一步提供帮助。

This is simple SocketIo Server Code.这是简单的 SocketIo 服务器代码。 It's not a client code.它不是客户端代码。
You have to download SocketIO at npm.你必须在 npm 下载 SocketIO。

const express = require('express');
const http = require('http');
const SocketIo = require('socket.io');

const app = express();

app.set('view engine', 'pug');
app.set('views', __dirname + '\\views');
app.use('/public', express.static(__dirname + '/public'));

app.get('/', (req, res) => res.render('home'));

const handleListen = () => console.log('Listening on http://localhost:3000');

const httpServer = http.createServer(app);
const wsServer = SocketIo(httpServer);

wsServer.on('connection', (socket) => {
  console.log('someone joined!')
  socket.on('join_room', (roomName) => {
    socket.join(roomName);
    socket.to(roomName).emit('welcome');
  });
});

httpServer.listen(3000, handleListen);

For more Info visit official documentation.有关更多信息,请访问官方文档。 https://socket.io/get-started/chat https://socket.io/get-started/chat

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM