热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

HowdoIemitamessagetoallusersinaprivatechatsharingaconversation_idusingnode.jsands

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在共享conversation_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);

2 个解决方案

#1


39  

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
  2. Socket.io客房
  3. Socket.IO subscribe to multiple channels
  4. Socket.IO订阅了多个频道
  5. Socket.io rooms difference between broadcast.to and sockets.in
  6. Socket.io在broadcast.to和sockets.in之间的区别

#2


7  

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.id存储在数组中,

   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.

为此我们需要知道recievername.You需要向服务器发出接收者名称。

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

希望这对你有用。!!祝你好运


推荐阅读
author-avatar
手机用户2502931993
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有