jQuery Nodejs & socket io:警告 - 引发错误:错误:监听 EADDRINUSE

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14647282/
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-08-26 13:49:24  来源:igfitidea点击:

Nodejs & socket io : warn - error raised: Error: listen EADDRINUSE

jquerynode.jssocketswebsocket

提问by masterofdestiny

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

我正在尝试使用 socket.io 尝试 Node.js

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

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

inside that i am putted server file i.e app.js

在里面我放了服务器文件,即 app.js

Here is the content

这是内容

var fs = require('fs')
    , http = require('http')
    , socketio = require('socket.io');

var server = http.createServer(function(req, res) {
    res.writeHead(200, { 'Content-type': 'text/html'});
    res.end(fs.readFileSync(__dirname + '/index.html'));
}).listen(8080, function() {
    console.log('Listening at: http://localhost:8080');
});

socketio.listen(server).on('connection', function (socket) {
    socket.on('message', function (msg) {
        console.log('Message Received: ', msg);
        socket.broadcast.emit('message', msg);
    });
});

Now in the same folder i have another file index.html like

现在在同一个文件夹中我有另一个文件 index.html 喜欢

<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script src="/socket.io/socket.io.js"></script>
    <script>
        $(function(){
            var iosocket = io.connect();

            iosocket.on('connect', function () {
                $('#incomingChatMessages').append($('<li>Connected</li>'));

                iosocket.on('message', function(message) {
                    $('#incomingChatMessages').append($('<li></li>').text(message));
                });
                iosocket.on('disconnect', function() {
                    $('#incomingChatMessages').append('<li>Disconnected</li>');
                });
            });

            $('#outgoingChatMessage').keypress(function(event) {
                if(event.which == 13) {
                    event.preventDefault();
                    iosocket.send($('#outgoingChatMessage').val());
                    $('#incomingChatMessages').append($('<li></li>').text($('#outgoingChatMessage').val()));
                    $('#outgoingChatMessage').val('');
                }
            });
        });
    </script>
</head>
<body>
Incoming Chat: <ul id="incomingChatMessages"></ul>


<input type="text" id="outgoingChatMessage">
</body>

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

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

 node app.js 

I am getting the error

我收到错误

 warn  - error raised: Error: listen EADDRINUSE

I go through some doc and found that port are busy so restarted the system but still i am getting the same error .

我查看了一些文档,发现端口很忙,因此重新启动了系统,但我仍然遇到相同的错误。

Please tell me what might i am doing wrong .

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

回答by Charles

Try another port. 8080 is usually used by tomcat e.g.

尝试另一个端口。8080 通常被 tomcat 使用,例如

Use netstat -a -vto know which port are currently used.

使用netstat -a -v知道哪些端口正在使用。

回答by ashley

It looks to me that your not connecting to the host on the client side:

在我看来,您没有连接到客户端的主机:

var iosocket = io.connect('http://localhost:8080');

回答by Brmm

You are not connecting to your server, this is wrong:

您没有连接到您的服务器,这是错误的:

var iosocket = io.connect();

this is right:

这是对的:

var iosocket = io.connect('http://localhost:8080');

Also your port 8080 is used on your server by "http-proxy", try an other port.

此外,您的服务器上的端口 8080 由“http-proxy”使用,请尝试其他端口。

回答by popartmogul

An ultra-silly/simple possibility is to make sure you don't have multiple terminal windows open. I got this error and realized I had a second pre-existing connection that I had started up my app with. When I closed it and restarted the app the error went away.

一个非常愚蠢/简单的可能性是确保您没有打开多个终端窗口。我收到这个错误,并意识到我有第二个预先存在的连接,我用它来启动我的应用程序。当我关闭它并重新启动应用程序时,错误消失了。

回答by Ruslan

Here is an example for beginners of how you can set up NodeJS + Express + SocketIO so that you have no EADDRINUSE error for the second time you start your app:

这是初学者的示例,说明如何设置 NodeJS + Express + SocketIO 以便第二次启动应用程序时不会出现 EADDRINUSE 错误:

'use strict';

// Init
var http = require('http');
var express = require('express');
var socketIO = require('socket.io');
var cors = require('cors');

// Create Server app
var app = express();
var server = http.createServer(app);
var io = socketIO(server);

// Socket.io settings
io.set('browser client minification', true);
io.set('browser client etag', true);
io.set('browser client gzip', true);
io.set('browser client expires', true);

// Starting Express server
server.listen(serverPort, function() {
  console.log(chalk.green('Server is running on localhost:' + serverPort + '...'));
});

// Middleware
app.use(cors());

// Routes
app.get('/', function(req, res) {
    res.send("Hello world!");
    console.log("Root visited!");
    // Broadcast socketIO message inside Express
    io.emit('new_message', "Hello world!");
});

// Socket.io
var connections = [];
var title = "Untitled";

io.sockets.on('connection', (socket) => {

    socket.once('disconnect', () => {
        connections.splice(connections.indexOf(socket), 1);
        socket.disconnect();
        console.log('Disconnected: %s. Remained: %s.', socket.id, connections.length)
    });

    // ...

    connections.push(socket);
    console.log('Connected: %s. Total: %s', socket.id, connections.length);
});

I think this is a useful example and I hope it will help someone who encountered a problem setting this all up.

我认为这是一个有用的例子,我希望它能帮助那些在设置这一切时遇到问题的人。