热门标签 | 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

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


推荐阅读
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 本文介绍了如何利用JavaScript或jQuery来判断网页中的文本框是否处于焦点状态,以及如何检测鼠标是否悬停在指定的HTML元素上。 ... [详细]
  • 探讨在特定情况下使用 Knockout.js 的 if 或 visible 绑定的最佳实践,特别是在处理未定义对象时的策略。 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文详细介绍了如何解决Uploadify插件在Internet Explorer(IE)9和10版本中遇到的点击失效及JQuery运行时错误问题。通过修改相关JavaScript代码,确保上传功能在不同浏览器环境中的一致性和稳定性。 ... [详细]
  • 导航栏样式练习:项目实例解析
    本文详细介绍了如何创建一个具有动态效果的导航栏,包括HTML、CSS和JavaScript代码的实现,并附有详细的说明和效果图。 ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 网络攻防实战:从HTTP到HTTPS的演变
    本文通过一系列日记记录了从发现漏洞到逐步加强安全措施的过程,探讨了如何应对网络攻击并最终实现全面的安全防护。 ... [详细]
  • JavaScript中属性节点的类型及应用
    本文深入探讨了JavaScript中属性节点的不同类型及其在实际开发中的应用,帮助开发者更好地理解和处理HTML元素的属性。通过具体的案例和代码示例,我们将详细解析如何操作这些属性节点。 ... [详细]
  • 2018-2019学年第六周《Java数据结构与算法》学习总结
    本文总结了2018-2019学年第六周在《Java数据结构与算法》课程中的学习内容,重点介绍了非线性数据结构——树的相关知识及其应用。 ... [详细]
  • CentOS 6.8 上安装 Oracle 10.2.0.1 的常见问题及解决方案
    本文记录了在 CentOS 6.8 系统上安装 Oracle 10.2.0.1 数据库时遇到的问题及解决方法,包括依赖库缺失、操作系统版本不兼容、用户权限不足等问题。 ... [详细]
  • 本文将详细介绍通过CAS(Central Authentication Service)实现单点登录的原理和步骤。CAS由耶鲁大学开发,旨在为多应用系统提供统一的身份认证服务。文中不仅涵盖了CAS的基本架构,还提供了具体的配置实例,帮助读者更好地理解和应用这一技术。 ... [详细]
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社区 版权所有