【问题标题】:Socket.io 1.x + Express (using http): How to send messages to specific client id?Socket.io 1.x + Express(使用http):如何将消息发送到特定的客户端ID?
【发布时间】:2014-07-28 12:07:49
【问题描述】:

我是 socket.io 的新手,我正在使用 NodeJS (express 4) 做一个简单的 API。我正在开发一种类似于 facebook 上旧的“poke”动作的动作。一个用户向其他用户发送一个 poke,这个用户会实时收到通知(这就是我使用 socket.io 的原因)。

这是代码:

app.js

var port   = 3000;
var app    = module.exports = express();
var server = require('http').Server(app);
...
server.listen(port);
require('./config/socket-io')(app, server, secret);

socket-io.js

module.exports = function(app, server, secret) {
    var clients = {};
    console.log("initiating sockets...");
    var sio = require('socket.io').listen(server, {'log level': 2});

    sio.on('connection', function (socket) {
        console.log("...new connection: "+socket.client.id);
        clients[socket.id] = socket;
        socket.emit('identification', { data : socket.client.id });

        socket.on('newShoutOut', function(data) {
            var receptor    = data.idTo;
            var emiter      = socket.client.id;
            console.log("...new shout out from " +emiter+ " to "+receptor);
            sio.sockets.sockets[receptor].emit({ data : data.data, from : emiter });
        });

        socket.on('disconnect', function() {
            console.log("..."+socket.client.id + " disconnected");
        });
    });
};

这里可以区分三种状态:

  • 连接:服务器检测到所有客户端连接到主机:端口。之后,服务器将其 ID 发送给每个客户端。这很好用。

  • 发送消息:一个客户端向其他客户端发送通知。目前,服务器接收来自一个客户端的通知,但“接收者”没有收到任何内容。

  • 断开连接:在这种情况下无关紧要。

我的问题是,向直接知道 ID 的客户发送消息的方式是什么?我做错了什么?我尝试了很多选项来直接向特定的客户端 ID 发送消息,但没有奏效...

编辑

前端

var socket = io('http://localhost:3000');
var id = "";
socket.on('connection', function (data) {
    console.log("connected!");
    console.log(data);
});
socket.on('identification', function(data) {
    id = data.data;
    $("#socket_info h1").html("ID: "+id);
});
socket.on('newShoutOut', function(data) {
    console.log("newShoutOut received!");
});

【问题讨论】:

  • 这是一个重复的问题,但您可以在这里找到一个很棒的解决方案:stackoverflow.com/questions/4647348/…
  • @bencripps 是的,我几天前看到了这个问题,但问题是,如果我从那里更改不推荐使用的东西(删除 createServer() 并添加 http),我会收到一个错误:Object #<Namespace> has no method 'socket' with完全相同的代码。
  • 您是否在客户端设置了处理程序以接收“newShoutOut”响应?看起来您的发出函数是匿名的。尝试命名它,并从您的页面设置一个侦听器。 socket.on('recieved private message', function( data ) { alert( data ) });
  • @bencripps 感谢您的回答! :) 我编辑了帖子以显示我在前端的测试代码。我不确定这是否是接收套接字的最佳方式,但 connectionidentification 事件有效。

标签: javascript node.js express socket.io


【解决方案1】:

好的,所以我假设喊话来自用户?您需要在客户端创建事件,例如:

var button = $('#button');

button.on('click', function() {
    var msg = 'message',
        userID = '123'; //get the ID who they are messaging

    socket.emit('sendShoutOut', {msg: msg, id: userID});
});

然后您需要在服务器上接收该响应,并在该函数中回复用户:

socket.on('sendShoutOut', function( data ) {

    socket.sockets.sockets[data.id].emit('sendPrivateMsg', { data : data.msg, from : emiter });
});

最后,必须通知接收方,因此您需要在客户端处理响应:

socket.on('sendPrivateMsg', function( data ) { 
    alert(data);    
});

希望这会有所帮助。

【讨论】:

  • 你是对的!这是因为我正在发送一个没有事件名称的匿名函数,并且从未到达客户端...谢谢。
  • 顺便说一句 @bencripps,很抱歉,但如果你能看看 this problem 会很棒...
猜你喜欢
  • 2011-06-06
  • 2012-05-24
  • 2015-08-06
  • 2011-10-18
  • 1970-01-01
  • 2021-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多