Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
509 views
in Technique[技术] by (71.8m points)

javascript - 使用socket.io和node.js将消息发送到特定客户端(Send message to specific client with socket.io and node.js)

I'm working with socket.io and node.js and until now it seems pretty good, but I don't know how to send a message from the server to an specific client, something like this:(我正在使用socket.io和node.js,直到现在看起来还不错,但我不知道如何从服务器向特定客户端发送消息,如下所示:)

client.send(message, receiverSessionId)

But neither the .send() nor the .broadcast() methods seem to supply my need.(但是.send().broadcast()方法似乎都不能满足我的需求。)

What I have found as a possible solution, is that the .broadcast() method accepts as a second parameter an array of SessionIds to which not send the message, so I could pass an array with all the SessionIds connected at that moment to the server, except the one I wish send the message, but I feel there must be a better solution.(我发现作为一种可能的解决方案,是.broadcast()方法接受一个不发送消息的SessionIds数组作为第二个参数,所以我可以传递一个数组,当时连接的所有SessionId都连接到服务器,除了我希望发送消息的那个,但我觉得必须有一个更好的解决方案。)

Any ideas?(有任何想法吗?)

  ask by Rodolfo Palma translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ivo Wetzel's answer doesn't seem to be valid in Socket.io 0.9 anymore.(Ivo Wetzel的答案似乎不再适用于Socket.io 0.9。)

In short you must now save the socket.id and use io.sockets.socket(savedSocketId).emit(...) to send messages to it.(简而言之,您现在必须保存socket.id并使用io.sockets.socket(savedSocketId).emit(...)向其发送消息。)

This is how I got this working in clustered Node.js server:(这就是我在集群Node.js服务器中使用它的方法:)

First you need to set Redis store as the store so that messages can go cross processes:(首先,您需要将Redis存储设置为存储,以便消息可以跨进程:)

var express = require("express");
var redis = require("redis");
var sio = require("socket.io");

var client = redis.createClient()
var app = express.createServer();
var io = sio.listen(app);

io.set("store", new sio.RedisStore);


// In this example we have one master client socket 
// that receives messages from others.

io.sockets.on('connection', function(socket) {

  // Promote this socket as master
  socket.on("I'm the master", function() {

    // Save the socket id to Redis so that all processes can access it.
    client.set("mastersocket", socket.id, function(err) {
      if (err) throw err;
      console.log("Master socket is now" + socket.id);
    });
  });

  socket.on("message to master", function(msg) {

    // Fetch the socket id from Redis
    client.get("mastersocket", function(err, socketId) {
      if (err) throw err;
      io.sockets.socket(socketId).emit(msg);
    });
  });

});

I omitted the clustering code here, because it makes this more cluttered, but it's trivial to add.(我在这里省略了聚类代码,因为它使它更加混乱,但添加起来很简单。)

Just add everything to the worker code.(只需将所有内容添加到工作人员代) More docs here http://nodejs.org/api/cluster.html(更多文档http://nodejs.org/api/cluster.html)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...