2012-05-17 17 views
11

सभी क्लाइंट के लिए संदेश भेजना अच्छी तरह से काम करता है लेकिन मैं विशेष उपयोगकर्ता नाम पर संदेश भेजना चाहता हूं। मेरी server.js फ़ाइल की तरह दिखता है। यह क्या होता है जब http://localhost:8080 चलाया जाता है, क्लाइंट कोड उपयोगकर्ता को उपयोगकर्ता नामों के साथ-साथ सॉकेट ऑब्जेक्ट में जोड़ता है। और तुरंत जुड़े हुए प्रत्येक ग्राहक को व्यक्तिगत संदेश देता है।socket.io और node.js विशेष ग्राहक को संदेश भेजने के लिए

//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 
    }); 

उत्तर

10

इस प्रयास करें:

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 से सहेजा जाता है और यह अपने ग्राहक की विशिष्ट आईडी में शामिल है। आप देख सकते हैं कि का उपयोग करते हुए इस:

console.log(socket.id); 
+0

टाइपरर: io.sockets.socket फ़ंक्शन नहीं है –

+0

1.0 में आपको io.sockets.connected [सॉकेटिड] .emit() का उपयोग करना चाहिए; – milanchandna

1

आप सभी ग्राहक आईडी स्टोर करने के लिए है और वह के अनुसार आप विशेष ग्राहक की आईडी प्राप्त करने के लिए और फिर आप कोड निम्न का उपयोग

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); 
}); 

तो इस वसीयत विशेष ग्राहक

संबंधित मुद्दे