Note that this is just for Socket.io version 0.7, and possibly higher if they don’t change the API again.

I’m writing an iPhone app right now using PhoneGap and jQuery Mobile on the phone, and node.js and socket.io on an Amazon EC2 server, and I hit a wall when I started using Socket.io. The problem is that the documentation is sparse, disorganized, and hard to find. In addition to that, there are dozens of blog posts and community members who’ll teach you how to do things in older versions of socket, but the API has changed, so you just end up with undefined errors.

My exact problem was with sending messages to individual clients.

// on the server
var users = {};
io.sockets.on('connection', function (socket) {
socket.emit('who are you');
socket.on('check in', function (incoming) {
users[incoming.phonenumber] = socket.id;
});
}); // on the client
socket.on('who are you', function (incoming) {
socket.emit('check in', {phonenumber: savedphonenumber});
});

To recap: The server is listening for connections. When a client connects, the server emits the custom message “who are you”. The client hears it and responds with it’s identifier (I’m using their phone number, which I verify and store earlier in the process). The server then stores the client’s socket ID in a users object, with their identifier as the key, and the socket ID as the value.

From then on, it’s a simple case of grabbing the user’s socket ID from the user’s table when you need to send them a message, and sending it like this:

var socketid = users[clientphonenumber];
io.sockets.socket(socketid).emit('for your eyes only');

One thing to consider is what happens to that users object if you have thousands of users. It’ll get pretty bloated, pretty quickly. You need to figure out a strategy for removing users from the users object if they disconnect AND if you don’t hear from them in a while (in case the disconnect event doesn’t fire).

Source:http://chrissilich.com/blog/socket-io-0-7-sending-messages-to-individual-clients/

Socket.io 0.7 – Sending messages to individual clients的更多相关文章

  1. node相关--socket.io

    使用Socket.IO可以避免webSocket产生的问题: 传输: Socket.IO中消息的传递是基于传输的,而非全部依靠WebSocket; 它总会尝试选择对用户来说速度最快.对服务器性能来说最 ...

  2. socket.io emit callback调用探秘

    socket.io https://socket.io/ https://socket.io/docs/ What Socket.IO is Socket.IO is a library that e ...

  3. .net , java webSocket 连接 Socket.io (1.4.4版本) 问题

    .net版Socketio4net类库和java版socket.io-java-client类库 连接socket.io 1.4版本都不行,网上大多是socket.io 0.9版本的,socket.i ...

  4. Socket.IO 1.0 正式发布,快速可靠的实时引擎

    Socket.IO 是目前 Web 领域最火的实时引擎,用于实现基于事件的双向实时的通信.它适用于任何平台,浏览器或设备,专注于可靠性和速度.您可以将数据推送到客户端,并获得实时的计数,日志或图表. ...

  5. CabloyJS V3.2.0支持Socket IO

    CabloyJS v3.2.0引入了Socket IO,并且实现了统一的在线推送和离线推送机制 效果演示 1. IM 用户向系统发送一条消息,系统通过websocket在线通道向用户推送一条回复 2. ...

  6. 后端Python3+Flask结合Socket.io配合前端Vue2.0实现简单全双工在线客服系统

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_158 在之前的一篇文章中:为美多商城(Django2.0.4)添加基于websocket的实时通信,主动推送,聊天室及客服系统,详 ...

  7. 【socket.io研究】0.前提准备

    WebSocket出现之前,web实时推送,一般采用轮询和Comet技术(可细分为长轮询机制和流技术两种),需要大量http请求,服务器受不了.HTML5定义了WebSocket协议,基于TCP协议, ...

  8. Practical Node.js (2018版) 第9章: 使用WebSocket建立实时程序,原生的WebSocket使用介绍,Socket.IO的基本使用介绍。

    Real-Time Apps with WebSocket, Socket.IO, and DerbyJS 实时程序的使用变得越来越广泛,如传统的交易,游戏,社交,开发工具DevOps tools, ...

  9. AngularJS+Node.js+socket.io 开发在线聊天室

    所有文章搬运自我的个人主页:sheilasun.me 不得不说,上手AngularJS比我想象得难多了,把官网提供的PhoneCat例子看完,又跑到慕课网把大漠穷秋的AngularJS实战系列看了一遍 ...

随机推荐

  1. @Resource和@Autowired

    @Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了.@Resource有两个属性是比较重要的,分 ...

  2. WordPress Backdoor未授权访问漏洞和信息泄露漏洞

    漏洞名称: WordPress Backdoor未授权访问漏洞和信息泄露漏洞 CNNVD编号: CNNVD-201312-497 发布时间: 2013-12-27 更新时间: 2013-12-27 危 ...

  3. SharePoint对象模型性能考量

    转:http://blog.csdn.net/ma_jiang/article/details/6553373 部分常见用例场景都是关于显示和编辑SharePoint列表的--可惜,这也是我们看到很多 ...

  4. POJ --- 2918 求解数独

    Tudoku   Description Tom is a master in several mathematical-theoretical disciplines. He recently fo ...

  5. javascript 关于cookie的操作

    <script language=javascript> //获得coolie 的值 function cookie(name){ var cookieArray=document.coo ...

  6. [Raobin] Ext.net在页面中以窗体的形式打开另外的页面

    public ActionResult NewPartWindow(string id, string copy, string code) { var vm = new VM(this); var ...

  7. [摘]selenium-ide编辑命令

    ----//编辑命令 selenium为我们录制的脚本不是100%符合我们的需求的,所以,编辑录制的脚本是必不可少的工作. 1.  编辑一行命令或注释. 在Table标签下选中某一行命令,命令由com ...

  8. Upgrading to EF6

    In previous versions of EF the code was split between core libraries (primarily System.Data.Entity.d ...

  9. bat 批处理 字符串 截取

    由于项目中配置项太多,经常有同事在配置xml的时候,讲 配置的路径搞错,先需要搞一个脚本,可以自动将路径截取出来, 晚上收集了点资料,暂时先上几个 bat 后面留着 ,具体实现. @echo off ...

  10. Dynamic Vertex Buffers

    ynamic vertex buffers on the other hand allow us to manipulate the information inside the vertex buf ...