Socket.io 0.7 – Sending messages to individual clients
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的更多相关文章
- node相关--socket.io
使用Socket.IO可以避免webSocket产生的问题: 传输: Socket.IO中消息的传递是基于传输的,而非全部依靠WebSocket; 它总会尝试选择对用户来说速度最快.对服务器性能来说最 ...
- socket.io emit callback调用探秘
socket.io https://socket.io/ https://socket.io/docs/ What Socket.IO is Socket.IO is a library that e ...
- .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 ...
- Socket.IO 1.0 正式发布,快速可靠的实时引擎
Socket.IO 是目前 Web 领域最火的实时引擎,用于实现基于事件的双向实时的通信.它适用于任何平台,浏览器或设备,专注于可靠性和速度.您可以将数据推送到客户端,并获得实时的计数,日志或图表. ...
- CabloyJS V3.2.0支持Socket IO
CabloyJS v3.2.0引入了Socket IO,并且实现了统一的在线推送和离线推送机制 效果演示 1. IM 用户向系统发送一条消息,系统通过websocket在线通道向用户推送一条回复 2. ...
- 后端Python3+Flask结合Socket.io配合前端Vue2.0实现简单全双工在线客服系统
原文转载自「刘悦的技术博客」https://v3u.cn/a_id_158 在之前的一篇文章中:为美多商城(Django2.0.4)添加基于websocket的实时通信,主动推送,聊天室及客服系统,详 ...
- 【socket.io研究】0.前提准备
WebSocket出现之前,web实时推送,一般采用轮询和Comet技术(可细分为长轮询机制和流技术两种),需要大量http请求,服务器受不了.HTML5定义了WebSocket协议,基于TCP协议, ...
- Practical Node.js (2018版) 第9章: 使用WebSocket建立实时程序,原生的WebSocket使用介绍,Socket.IO的基本使用介绍。
Real-Time Apps with WebSocket, Socket.IO, and DerbyJS 实时程序的使用变得越来越广泛,如传统的交易,游戏,社交,开发工具DevOps tools, ...
- AngularJS+Node.js+socket.io 开发在线聊天室
所有文章搬运自我的个人主页:sheilasun.me 不得不说,上手AngularJS比我想象得难多了,把官网提供的PhoneCat例子看完,又跑到慕课网把大漠穷秋的AngularJS实战系列看了一遍 ...
随机推荐
- Bootstrap Paginator分页插件的使用
今天,我为大家带来的一款做得非常优秀的分页插件BootStrap Paginator,他是一款js插件,由于本人也是才刚刚搞出来的,所以暂时对它也不是特别了解,只能大楖告诉大家怎么使用.我这里使用的是 ...
- Sending data to USB printer in C#?
using System; using System.Drawing; using System.Drawing.Printing; using System.IO; using System.Run ...
- Android 实用代码七段(三)
前言 终于又攒了一篇出来,本系列以实用为主,欢迎和我分享和推荐好用的代码段~~ 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com 农民伯伯: http: ...
- 如何监听非本地IP
做HA的时候,如果每个机器上同时需要监听多个IP的同一个端口.那么肯定是需要写死监听的IP和端口.比如在haproxy里面: frontend free bind default_backend te ...
- C#.NET 打印连续纸高度动态变化(基于长江支流的金质打印通)
问题是这样的,打印机使用的是卷筒的连续纸,要打印的内容因为数据行数不同,高度会有变化.这时如果能在打印时动态改变纸张大小(其实只改变高度即可)当然是最好的选择. 我使用了网上久负盛名的[长江支流]的“ ...
- 处理.NET中的内存泄露
Fabrice Marguerie是一位软件架构师和咨询师,他在MSDN发表了如何检测和避免.NET程序内存与资源泄漏的文章.此文章描述了编写.NET程序时可能发生的内存与资源泄漏,以及如何避免这些泄 ...
- Curl之Post Json
curl Post Json $ curl -i -X POST -H "'Content-type':'application/x-www-form-urlencoded', 'chars ...
- 【转】Installing OpenCV on Debian Linux
In this post I will describe the process of installing OpenCV(both versions 2.4.2 and 2.4.3) on Debi ...
- 【转】centos 6.4 samba 安装配置
关掉防火墙 service iptables stop chkconfig iptables off (永久关闭防火墙) 关闭SELinux vi /etc/sysconfig/selinu ...
- LittleTools之批量替换工具
身为程序员,有很多事情都可以交给机器来做,这样可以提高工作效率. 在此先写个批量替换工具,用来将某些对象统一替换为另一对象. 比方说场景中摆了一堆树,位置.比例.旋转都已经调好了,但是对树的样式不太满 ...