当前位置: 首页 > 知识库问答 >
问题:

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

韩靖琪
2023-03-14

我有一个关于如何改进这个代码的问题。我是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个答案

米树
2023-03-14

我有一个示例教程(我的实践代码)。它将帮助您理解socket.io。socket.io的新版本是1.0。它有点不同,我对此不太熟悉,我使用的是旧版本,但我知道新版本没有太大不同。请通过评论它仍然是有价值的,它包括来自

  1. 简单消息
  2. 私人消息
  3. 公共消息
  4. 加入/离开群
  5. 组Mesage到
  6. 联机/脱机状态和联机用户数

代码: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")
        }
    });
});
/**
 * 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)
})
 类似资料:
  • 我正在存放插座。每个用户使用redis登录时的id 当这个用户的任何帖子。_id被创建时,我试图使用上面存储的socket.id向那个特定用户的套接字发出一个事件 问题是我无法使用上述代码向该特定套接字发出。我正在使用套接字。io版本

  • 我已经按照Quetion1和Quetion2从堆栈溢出发送消息到特定的客户端,基于其会话ID,但找不到成功。 下面是我的示例RestController类 会话ID:当客户端发送create会话请求时,会生成新的Spring会话ID,并将其存储在MongoDB中。之后,当客户端发送Web套接字连接请求时,会收到与预期存储在mongoDb中的相同的会话ID。直到一切正常。 现在,我的工作是根据Ses

  • 我从插座开始。io节点。js,我知道如何在本地发送消息和广播函数:-所有连接的客户端都接收相同的消息。 现在,我想知道如何向特定客户端发送私人消息,我的意思是两个人之间的私人聊天(客户端到客户端流)的一个套接字。谢谢。

  • 我的服务器代码使用将消息发送到特定的套接字,请参阅https://socket.io/docs/emit-cheatsheet/ 但是,当连接坏了,客户机断开并再次连接时,socketId会发生变化,并且会失败。那么如何实现re-emit方法来处理重新连接呢? 我的问题与此相反,socket.io在x秒/第一次尝试获得响应失败后停止重新发出事件,从中我了解到socket.io客户端在重新连接时可以

  • 现在我有了下面的类,它使用OkHttp处理WebSockets,但它的设置方式是,我无法保持WebSocket打开,以便向web Socket发送新消息。如何在不创建新的websocket连接的情况下继续向同一个打开的websocket发送消息?

  • 我是新手。NET(C#)和WebSocket。。。我已经安装了VS2012和Windows Server 2012,并且已经启动并运行了WebSocket。我似乎无法从一个套接字接收消息并将其发送到另一个特定的套接字。唯一的选择似乎是向所有套接字广播消息。有没有一种方法可以将消息只发送给特定的用户?我希望聊天室主持人有机会拒绝不适当的帖子。