簡體   English   中英

socket.io和node.js將消息發送到特定客戶端

[英]socket.io and node.js to send message to particular client

向所有客戶端發送消息的效果很好但我想將消息發送到特定用戶名。 我的server.js文件看起來像。 它的作用是運行http://localhost:8080 ,客戶端代碼將用戶添加到對象用戶名以及套接字對象中。 並立即將單個消息返回給每個連接的客戶端。

//var io = require('socket.io').listen(8080);
var app = require('http').createServer(handler)
  , io = require('socket.io').listen(app)
  , fs = require('fs')
var usernames={};  
 app.listen(8080);

// on server started we can load our client.html page
function handler ( req, res ) {
  fs.readFile( __dirname + '/client.html' ,
  function ( err, data ) {
    if ( err ) {
      console.log( err );
      res.writeHead(500);
      return res.end( 'Error loading client.html' );
    }
    res.writeHead( 200 );
    res.end( data );
  });
};
io.set('log level', 1); // reduce logging
io.sockets.on('connection', function (socket) {

 socket.on('adduser', function(username){
        // store the username in the socket session for this client
        socket.username = username;
        // add the client's username to the global list
        usernames[username] = username;
        // send client to room 1
        console.log(username+' has connected to the server');
        // echo to client they've connected
  });

  socket.on('pmessage', function (data) {
        // we tell the client to execute 'updatechat' with 2 parameters
        io.sockets.emit("pvt",socket.username,data+socket.username); // This works
        io.sockets.socket(socket.username).emit("pvt",socket.username,data+socket.username); // THIS DOESNOT
   });

  socket.on('disconnect', function(){
        // remove the username from global usernames list
        delete usernames[socket.username];

        // echo globally that this client has left
       console.log(socket.username + ' has disconnected');
    });
});

發出消息的部分

socket.on('pmessage', function (data) {
        // we tell the client to execute 'updatechat' with 2 parameters
        io.sockets.emit("pvt",socket.username,data+socket.username); // This works and sends message to all clients
        io.sockets.socket(socket.username).emit("pvt",socket.username,data+socket.username); // THIS DOESNOT
   });

試試這個:

socket.on('pmessage', function (data) {
    // we tell the client to execute 'updatechat' with 2 parameters
    io.sockets.emit("pvt",socket.username,data+socket.username);
    io.sockets.socket(socket.id).emit("pvt",socket.username,data+socket.username); 
});

socket.id由socket.io保存,它包含客戶端的唯一ID。 您可以使用以下方法檢查:

console.log(socket.id);

您必須存儲所有客戶端ID,並根據您必須獲取特定客戶端的ID,然后使用以下代碼

var store={};//this will store all client id and username e.g. store={jay:"34jhjshdj343sasa"}
socket.on('pmessage', function (data) {
    var receiverUserName=data.username;
    var message=data.message;
    var id = store[receiverUserName];//get ID
    // we tell the client to execute 'updatechat' with 2 parameters
    io.sockets.emit("pvt",socket.username,data+socket.username);
    io.sockets.socket(id).emit("pvt",socket.username,data+socket.username); 
});

然后這將在特定客戶端上發出

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM