向特定的套接字 id 发送私人消息

Posted

技术标签:

【中文标题】向特定的套接字 id 发送私人消息【英文标题】:sending a private message to a specific socket id 【发布时间】:2014-08-26 15:46:06 【问题描述】:

我有一个关于如何改进此代码的问题。我是 Socket.IO 的新手,我一直在 YouTube 上观看有关私人消息传递的视频教程。我只是想知道是否有更好的方法来改进以下代码行?

    var io = require('socket.io');

   exports.initialize = function(server)
    io = io.listen(server);
    var usernames = ;

    io.sockets.on('connection', function(socket)
        socket.on('new user', function(data, callback)
            if(data in usernames)
                callback(false);
            else
                callback(true);
                //save the username to the socket for retrieval
                socket.username = data;
                usernames[socket.username] = socket;
                io.sockets.emit('usernames', Object.keys(usernames));
            
        );

        socket.on('sendMessage', function(data, callback)
            var msg = data.trim();
            if(msg.substr(0,3) === "/w ")
                msg = msg.substr(3);
                var space = msg.indexOf(" ");
                if( space !== 1)
                    var name = msg.substr(0, space);
                    msg = msg.substr(space+1);
                    if(name in usernames)                         
                        usernames[name].emit('new message', message: msg, username: socket.username);
                       
                 else 
                    callback("error invalid user");
                
             else
                //retrieve the username saved in the socket
                io.sockets.emit('new message', message: data, username: socket.username);
            

        );

        socket.on('disconnect', function(data)
            if(!socket.username) return;
            //NEW
            delete usernames[socket.nickname];
            // OLD
            //usernames.splice(usernames.indexOf(socket.username), 1);
            io.sockets.emit('usernames', usernames);
        );
    );
;

我正在尝试使用:

io.sockets.socket(id)emit('new message', message: msg, username: socket.username);

但它似乎不起作用。

【问题讨论】:

【参考方案1】:

我有一个示例教程(我的练习代码)。它将帮助您了解socket.io。 socket.io 的新版本是 1.0。它有点不同,我对此不太熟悉,我使用的是旧版本,但我知道新版本没有太大区别。请浏览它仍然有价值的 cmets,它包括来自

的示例
    简单消息 私信 公开留言 加入/离开群组 将消息分组到 在线/离线状态和在线用户数

代码: Server.js

/**
 * require socket.io and coonect to port , here port no is 1180
 */
var io = require('socket.io').listen(1180);
/**
 * An object to store Socket object of every user by name
 * @type Object
 */
var onLine = 
/**
 * An object to store all groups name
 * @type Object
 */
var group = ;

var onLine = 
var onLineBySocket = ;
/**
 * On Connection - When a client gets connected
 * @param  Object socket An Object to identifiy the remote user
 * or client. Every client has its own unique socket. This socket
 * variable corresponds to the client who has just initiated any
 * socket event. 
 * Many user can initiate same socket event simultaneously but
 * Under this block, Socket will remain unique for every one.
 * Socket object will belong to the client whose has just 
 * communicated with server
 */
io.sockets.on('connection', function(socket) 
    /**
     * A simple socket event at server side.
     * Set up an identity over the network. Set client's name
     * using socket.set function.
     * @param  String name Set your name on network
     */
    socket.on('connectMe', function(data) 
        socket.set('name', data.name)
        onLine[data.name] = socket
        onLineBySocket[socket.id] = data.name
        socket.get('name', function (err, name) 
            if(!err) 
                // send back acknowledgement to the client requested for
                // connectMe or registeration over this socket network
                socket.emit('connectionEstablished', name)
                // notify all remote user about this new socket or client
                socket.broadcast.emit('onLine',name)
            
        );
    );

    /**
     * Predefined/Reserved event
     * whenever a client gets disconnecte from server, this event 
     * gets triggered
     * @return [type]      [description]
     */
    socket.on('disconnect', function() 
        console.log(onLineBySocket, 'onLineBySocket')
        socket.get('name', function (err, name) 
            if(!err) 
                socket.broadcast.emit('notification', name + ' is now offLine')
            
        );
    )

    /**
     * Socket Handler for sending private message to someone,
     * @param  String to  Send To
     * @param  String msg Message
     */
    socket.on('privateMessage', function(data) 
        socket.get('name', function (err, name) 
            if(!err) 
                // get the user from list by its name to get its socket, 
                // then emit event privateMessage
                // again here we want to make you clear
                // that every single client connection has its own
                // unique SOcket Object, we need to get this Socket object
                // to communicate with every other client. The socket variable
                // in this scope is the client who wants to send the private 
                // message but the socket of the receiver is not know.
                // Get it from the saved list when connectMe handlers gets called
                // by each user.
                onLine[data.to].emit('newPrivateMessage',from:name, msg:data.msg, type:'Private Msg')
            
        );
    );


    /**
     * Send Public Message or broadcast(to all except the sender itself)
     */
    socket.on('publicMessage', function(data) 
        socket.broadcast.emit('newPublicMessage',from:data.from, msg:data.msg, type:'Public Msg')
    );

    /**
     * Make and store some private rooms/group. For creating room
     * socket.io itself has no role, we are just saving it in an object
     * and we will refer this object when client wants to join
     * any group
     */
    socket.on('newGroup', function(data) 
        group[data.group] = data.group
        socket.emit('groupCreated',from:'server', msg:'group ' + data.group + ' created')
    );

    /**
     * send object to client which stores all group name
     */
    socket.on('getGroupList', function(data) 
        socket.emit('groupList',group)
    );

    /**
     * Join a room/group
     */
    socket.on('joinGroup', function(data) 
        if(group[data.group]) 
            socket.join(data.group)
            socket.emit('notification','You have joined ' + data.group)
         else 
            socket.emit('notification','group ' + data.group + " doesn't exist")
        
    );

    /**
     * Leave a room/group
     */
    socket.on('leaveGroup', function(data) 
        if(group[data.group]) 
            socket.leave(data.group)
            socket.emit('notification','You have Left ' + data.group)
         else 
            socket.emit('notification','group ' + data.group + " doesn't exist")
        
    );

    /**
     * Broadcast message to every member of particular group
     * using broadcast.to
     */
    socket.on('groupMessage', function(data) 
        if(group[data.group]) 
            socket.broadcast.to(group[data.group]).emit('groupMessage',from:data.from, msg:data.msg)
            socket.emit('notification','Message send')
         else 
            socket.emit('notification','group ' + data.group + " doesn't exist")
        
    );
);

代码:Client.js

/**
 * Connect to remote host
 * @param  String IP Address of remote host with Port No.
 * @return Object
 */
var socket = io.connect('http://localhost:1180');

/**
 * get User Name from url
 * @type String - url
 */
//var myName = window.location.search.match(/=(\w+)/)[1];
var myName = "user"

// Some predefined/reserved socket events
/**
 * Checking Status of My Connection
 * If Connection gets disconnect, socket will try to auto-connect after some interval
 */
socket.on('reconnecting', function(data) 
    console.log('Trying to Re-Connect')
);

/**
 * If socket founds Connection then it started process of connection
 * this is connnecting
 */
socket.on('connecting', function() 
    console.log('Connecting')
)
/**
 * Event triggered when socket gets connected successfully
 */
socket.on('connect', function() 
    console.log('Connected')
);

/**
 * Though we have connected to the socket without any kind
 * of authorisation and any identity. Let's set up an identity
 * over the network. Set your name.
 * @param  String name Set your name on network
 */
function connectMe(name) 
    socket.emit('connectMe', 
        name: name
    );
    myName = name


/**
 * call connectMe function. It will emit a socket request to server
 * to set your name.
 * For this we will use socket.set and socket.get at server side
 */
connectMe(myName)

/**
 * User Defined Socket Handler/Event
 * @param  String name send by server
 * @return [type]
 */
socket.on('connectionEstablished', function(name) 
    console.log('Welcome: ' + name)
)

/**
 * Want to know who has just come onLine
 */
socket.on('onLine', function(name) 
    console.log(name + ' is now onLine')
)

/**
 * Send private message to someone,
 * server will append your name in 'From' 
 * at server side using socket.get
 * @param  String to  Send To
 * @param  String msg Message
 */
function privateMessage(to, msg) 
    socket.emit('privateMessage', 
        to: to,
        msg: msg
    );

/**
 * Receive New Private Message
 * data.type added by server
 */
socket.on('newPrivateMessage', function(data) 
    console.log(data.type + ' from ' + data.from + ': ' + data.msg)
)

/**
 * Send Public Message or broadcast(to all except me)
 * server will append your name in 'From' 
 * at server side using socket.get
 * @param  String msg Message
 */
function publicMessage(msg) 
    socket.emit('publicMessage', 
        from: myName,
        msg: msg
    );

/**
 * Receive New Public Message
 * data.type added by server
 */
socket.on('newPublicMessage', function(data) 
    console.log(data.type + ' from ' + data.from + ': ' + data.msg)
)

/**
 * Make some private rooms/group
 * @param  String group Name of the Group
 */
function createGroup(group, msg) 
    socket.emit('newGroup', 
        group: group
    );


/**
 * Acknowledgemenet from server, group created
 * data.from, data.msg added by server
 */
socket.on('groupCreated', function(data) 
    console.log(' from ' + data.from + ': ' + data.msg)
)

/**
 * Get List of available groups from server
 */
function getGroupList() 
    socket.emit('getGroupList');


/**
 * List of all groups from server
 */
socket.on('groupList', function(data) 
    console.log(' groupList ', data)
)

/**
 * Join a room/group
 * @param  String group Name of the group to join
 */
function joinGroup(group) 
    socket.emit('joinGroup', 
        group: group
    );


/**
 * Send group Messages (To All in Group except me)- Not Public Message(To All)
 * @param  String group GroupName in which you want to send message
 * @param  String msg   Message
 */
function groupMessage(group, msg) 
    socket.emit('groupMessage', 
        group: group,
        from: myName,
        msg: msg
    );


/**
 * Receive group message
 */
socket.on('groupMessage', function(data) 
    console.log('Group Mesage from ' + data.from + ': ' + data.msg)
)

/**
 * Leave a room/group
 * Stop receiving messages from a group
 * @param  String group Name of the group to Leave
 */
function leaveGroup(group) 
    socket.emit('leaveGroup', 
        group: group
    );


/**
 * Get Custom Notifications or Error messages from Server
 */
socket.on('notification', function(msg) 
    console.log(msg)
)

【讨论】:

刚刚在这里找到了答案***.com/questions/24041220/…

以上是关于向特定的套接字 id 发送私人消息的主要内容,如果未能解决你的问题,请参考以下文章

向 Socket.IO 1.0 中的特定 ID 发送消息

Spring引导Web套接字,无需向特定用户发送消息

当套接字重新连接时,如何让 socket.io 服务器向特定套接字重新发送消息?

节点 Js 无法将消息发送到在线特定套接字

节点 Js 无法将消息发送到在线特定套接字

给特定用户的消息 Socket Python