简体   繁体   English

使用 node.js 和 socket.io 在密钥之间创建私人聊天

[英]Creating a private chat between a key using a node.js and socket.io

How do I emit a message to all users in a private chat sharing a conversation_id using node.js and socket.io?如何使用 node.js 和 socket.io 在私人聊天中向所有用户发送一条消息,共享一个 session_id?

var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
conversations = {};

app.get('/', function(req, res) {
res.sendfile('/');
});

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

socket.on('send message', function (data) {

    var conversation_id = data.conversation_id;

    if (conversation_id in conversations) {

        console.log (conversation_id + ' is already in the conversations object');

        // emit the message [data.message] to all connected users in the conversation

    } else {
        socket.conversation_id = data;
        conversations[socket.conversation_id] = socket;

        conversations[conversation_id] = data.conversation_id;

        console.log ('adding '  + conversation_id + ' to conversations.');

        // emit the message [data.message] to all connected users in the conversation

    }
})
});

server.listen(8080);

You have to create a room with conversation_id and make users to subscribe to that room, so that you can emit a private message to that room it by,您必须创建一个带有conversation_id的房间并让用户订阅该房间,以便您可以通过以下方式向该房间发送私人消息,

client客户

var socket = io.connect('http://ip:port');

socket.emit('subscribe', conversation_id);

socket.emit('send message', {
    room: conversation_id,
    message: "Some message"
});

socket.on('conversation private post', function(data) {
    //display data.message
});

Server服务器

socket.on('subscribe', function(room) {
    console.log('joining room', room);
    socket.join(room);
});

socket.on('send message', function(data) {
    console.log('sending room post', data.room);
    socket.broadcast.to(data.room).emit('conversation private post', {
        message: data.message
    });
});

Here is the docs and example for creating a room, subscribing to the room and Emit message to a room:这是创建房间、订阅房间和向房间发送消息的文档和示例:

  1. Socket.io Rooms Socket.io 房间
  2. Socket.IO subscribe to multiple channels Socket.IO 订阅多个频道
  3. Socket.io rooms difference between broadcast.to and sockets.in Socket.io 房间在 broadcast.to 和 sockets.in 之间的区别

SURE: Simply,当然:简单地说,

This is what you need :这是你需要的:

 io.to(socket.id).emit("event", data);

whenever a user joined to the server,socket details will be generated including ID.This is the ID really helps to send a message to particular people.每当用户加入服务器时,都会生成套接字详细信息,包括 ID。这个 ID 确实有助于向特定的人发送消息。

first we need to store all the socket.ids in array,首先我们需要将所有的 socket.ids 存储在数组中,

   var people={};

   people[name] =  socket.id;

here name is the reciever name.这里的名字是接收者的名字。 Example:例子:

  people["ccccc"]=2387423cjhgfwerwer23;

So, now we can get that socket.id with the reciever name whenever we are sending message:所以,现在我们可以在发送消息时获取带有接收者名称的 socket.id:

for this we need to know the recievername.You need to emit reciever name to the server.为此,我们需要知道接收者名称。您需要向服务器发出接收者名称。

final thing is:最后的事情是:

  socket.on('chat message', function(data){
 io.to(people[data.reciever]).emit('chat message', data.msg);
 });

Hope this works well for you.!!Good Luck希望这对你有用。!祝你好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM