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

后端 未结 5 2031
陌清茗
陌清茗 2020-12-30 07:23

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

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

inside that i am putted server f

相关标签:
5条回答
  • 2020-12-30 07:48

    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.

    0 讨论(0)
  • 2020-12-30 07:55

    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.

    0 讨论(0)
  • 2020-12-30 07:56

    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:

    '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.

    0 讨论(0)
  • 2020-12-30 08:06

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

    var iosocket = io.connect('http://localhost:8080');
    
    0 讨论(0)
  • 2020-12-30 08:10

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

    Use netstat -a -v to know which port are currently used.

    0 讨论(0)
提交回复
热议问题