Socket.io:net::ERR_CONNECTION_CLOSED [英] Socket.io: net::ERR_CONNECTION_CLOSED

查看:1232
本文介绍了Socket.io:net::ERR_CONNECTION_CLOSED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的网站上设置socket.io,但是服务器记录一个错误,指出该端口已在使用中(我将其设置为与服务器的端口相同).我正在使用Express.

I am trying to set up socket.io on my web site but the server logs an error saying the port is already in use (I set it to the same as my server's). I am using Express.

如果将其设置为其他端口,则会收到"net :: ERR_CONNECTION_CLOSED"错误.

If I set it to a different port, I get a 'net::ERR_CONNECTION_CLOSED' error.

我该如何解决?

推荐答案

您可能正在创建(显式或隐式)应该在同一端口上侦听的两个单独的HTTP服务器实例,因此第二个实例将引发错误,因为首先是已经占用了端口.

You are probably creating (either explicitly or implicitly) two separate HTTP server instances that should listen on the same port, so the second instance will throw an error because the first is already occupying the port.

相反,您可以在Express和socket.io之间共享同一个HTTP服务器,因此只有一个HTTP服务器可以处理这两者:

Instead, you can share the same HTTP server between both Express and socket.io, so there will be only one HTTP server that handles both:

const express  = require('express');
const app      = express();
const server   = app.listen(3000); // or whatever port you want
const io       = require('socket.io')(server);

app.listen(...)返回Express将为您创建的HTTP服务器实例.您可以将该实例传递到其构造函数(由require('socket.io')返回)中,以将该实例重新用于socket.io.

app.listen(...) returns the HTTP server instance that Express will create for you. You can re-use that instance for socket.io by passing it into its constructor (returned by require('socket.io')).

这篇关于Socket.io:net::ERR_CONNECTION_CLOSED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆