javascript Nodejs & socket io 错误:监听 EADDRINUSE

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20841529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:26:08  来源:igfitidea点击:

Nodejs & socket io Error: listen EADDRINUSE

javascripthtmlnode.jssocket.io

提问by Ninjacu

I am giving a try to do a chat with Node.js and socket.io

我正在尝试与 Node.js 和 socket.io 聊天

Now here is my scenario I am using ubuntu 12.04 user and i have folder pp on desktop

现在这是我的场景,我使用的是 ubuntu 12.04 用户,桌面上有文件夹 pp

inside that i am putted server file server.js

在里面我放了服务器文件 server.js

Here is the client:

这是客户端:

$(document).ready(function() {
    var urlServer = location.origin + ':8081';
    var socket = io.connect(urlServer);
});

$(document).ready(function() {
    var urlServer = location.origin + ':8081';
    var socket = io.connect(urlServer);

    $("#boton").on('click', function() {
        var mensaje = $("#mensaje").val();
        socket.emit("mensaje", {msg: mensaje});
    });

    socket.on("mensaje", function(msg) {
        console.log("hemos recibido un mensaje", msg);
    });
});

And here the server

这里是服务器

var server  = require('http').createServer(),
    sio      = require('socket.io'),
    port    = 8081;
server.listen(port);
var io = sio.listen(server, { log:true });
var channels = {};
io.sockets.on('connection', function (socket) {
    console.log("Cliente conectado");

    socket.on('mensaje', function (msg) {
        console.log(msg);
    socket.broadcast.emit('mensaje', msg);
    });

});
console.log('1- Escuchando en http://localhost:' + port , "");
console.log("");

Now in the same folder I have an html file like

现在在同一个文件夹中,我有一个 html 文件,例如

<!DOCTYPE html>
<html>
<head>
    <script src="../jquery.js"></script>
    <script src="../node_modules/socket.io/node_modules/socket.io-client/dist/socket.io"></script>
    <script src="clientechat.js.js"></script>
    <title>Chat con Node</title>
</head>
<body>

    <div id="mensajes"></div>
    <input type="text">
    <input type="submit" id="boton">

</body>
</html>

when i am trying to run the app.js useing node like

当我尝试使用 node 运行 app.js 时

node server.js

I am getting the error

我收到错误

   warn  - error raised: Error: listen EADDRINUSE

I try to restart all but it doesn't works

我尝试重新启动所有但它不起作用

Please tell me what might I am doing wrong.

请告诉我我可能做错了什么。

采纳答案by Kristian Barrett

If you cannot start the application at all you might have an application that is already using the given port. Another option is to try and use another port and see if you can start it.

如果您根本无法启动该应用程序,则您的应用程序可能已经在使用给定端口。另一种选择是尝试使用另一个端口,看看是否可以启动它。

Check @Faisal Ameers answer for linux commands to find program using port.

检查 @Faisal Ameers 对 linux 命令的回答以使用端口查找程序。

Check this post for finding the program using the port on windows: https://stackoverflow.com/a/48199/1958344

查看此帖子以在 Windows 上使用端口查找程序:https: //stackoverflow.com/a/48199/1958344

And this one for OS X: https://stackoverflow.com/a/30029855/1958344

这是 OS X 的:https: //stackoverflow.com/a/30029855/1958344

回答by Faisal Ameer

It means the address you are trying to bind the server to is in use. Do this;

这意味着您尝试将服务器绑定到的地址正在使用中。做这个;

Command:
ps -eaf|grep node

Output:
root     28029 27332  0 14:25 pts/2    00:00:03 node myVNC.js

This shows you the Process id where node is running, which in this case is "28029"

这会向您显示节点正在运行的进程 ID,在本例中为“28029”

Now kill this process id using;

现在使用以下方法终止此进程 ID;

kill -9 28029

回答by Allahbakash.G

best way to kill server and restart with a kill.shfile

杀死服务器并使用kill.sh文件重新启动的最佳方法

回答by Kit

Perhaps it's because you're calling listen(port)twice. The second listen() may be throwing EADDRINUSE because the first listen() is using the port already.

也许是因为你打了listen(port)两次电话。第二个listen() 可能会抛出EADDRINUSE,因为第一个listen() 已经在使用该端口。

server.listen(port); var io = sio.listen(server, { log:true });

server.listen(port); var io = sio.listen(server, { log:true });

回答by Ruslan

It looks like your server set up is incorrect. Have a look at this example of how you can use socket.io with your http server together: Nodejs & socket io : warn - error raised: Error: listen EADDRINUSE

看起来您的服务器设置不正确。看看这个例子,你可以如何将 socket.io 与你的 http 服务器一起使用:Nodejs & socket io:警告 - 引发错误:错误:听 EADDRINUSE