Socket.IO TypeError: Cannot read property 'emit' of undefined

后端 未结 1 1808
后悔当初
后悔当初 2021-01-28 22:46

I wrote the following code for chat client to client(client1 to server server to client2) but I am getting an error:

Missing error handler on socket. T

相关标签:
1条回答
  • 2021-01-28 23:27

    You've binded Your app to 3000-th port, and set socket.io to work with http module that listens on 3000.

    So You've to change on clientside this:

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

    to this:

    var socket = io.connect('http://localhost:3000');
    

    or just:

    var socket = io.connect();
    



    also from Your code:

    users[data.uid]=data.uid;
    users[data.uname] =data.uname;
    

    I don't see any logic here :) Why not to keep it like:

    users[data.uid] = {uid: data.uid, uname: data.uname}
    sockets[socket.id] = socket;
    if(!users[data.uid].sockets) {
      users[data.uid].sockets = [];
    }
    users[data.uid].sockets.push(socket);
    
    0 讨论(0)
提交回复
热议问题