Sending message to specific client with socket.io and empty message queue

后端 未结 9 1910
误落风尘
误落风尘 2020-12-22 20:16

I´m going crazy with socket.io! Documentation is so bad it\'s simply not true.

I want to send a feedback to specific client over socket.io

My server side loo

相关标签:
9条回答
  • 2020-12-22 20:36

    I believe io.sockets.socket has been removed and has been a problem in Socket.IO (https://github.com/socketio/socket.io/issues/1618).

    You can use io.sockets.connected[socket.id] and store the socket.id to reference with the user via username:

    var usernames = {};
    usernames[username] = socket.id;
    // Sending the message
    io.sockets.connected[usernames[username]].emit(...);
    

    I don't see it anywhere on the documentation so I can see how this hasn't been answered. Also, if you don't have any reference via usernames, you can instead try:

    users[socket.id] = socket.id;
    

    to duplicate the key and value for easy access.

    There is also possibly another way by using io.clients as described below, but I haven't used that method.

    OTHER SOLUTION: Send message to specific client with socket.io and node.js

    0 讨论(0)
  • 2020-12-22 20:41

    i tired with the latest version of node and socket.io below i am going to post complete code

      <ul id="messages"></ul>
    <form action="">
      <input id="id1"  /><button>Send</button>
    </form>
    <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
    <script>
    
      var io = io.connect();
      $('form').submit(function(){
        io.emit('drumevent', $('#id1').val());
        $('#id1').val('');
        return false;
      });
      io.on('drumevent', function(msg){
      console.log(msg);
    
    
    
        $('#messages').append($('<li></li>').text(msg.message+'    quantity = '+msg.quantity));
      });
    </script>
    

    server side code

      var usernames = {};io.on('connection', function(socket){usernames["username"] = socket.id;
    socket.on('drumevent', function(msg){     
    var socketId = socket.id;io.to(socketId).emit('drumevent', data);
    
    0 讨论(0)
  • 2020-12-22 20:48

    The correct way to do this in Socket.io 1.0+ is:

    io.to(users_socket_id).emit('new', 'hello');
    

    You can also substitute a room name for 'users_socket_id' to emit to a specific socket.io room.

    0 讨论(0)
  • 2020-12-22 20:50

    2 things

    1) You should really place your io.sockets.on(..) outside your app/update route to prevent adding multiple listeners for clients.

    2) io.sockets.socket(id); should not be used, it should have been socket.emit('new', 'hello')

    0 讨论(0)
  • 2020-12-22 20:51

    Have you tried using?

    var socket = io.connect('http://localhost:80/');
    
    0 讨论(0)
  • 2020-12-22 20:57

    In socket.io 1.0, this is how it would work. It may work for lower versions, but I cannot guarantee it.

    socket.to(socket_id_here).emit('new', 'hello');
    

    This works because socket.io automatically adds a socket to a room with the socket's id on connection.

    Also, if you plan to upgrade to version 1.0, there are a lot of changes to the api, so you'll sometimes have to change some code to make it work in 1.0.

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