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实战系列看了一遍 ...
随机推荐
- bzoj1143 2718
最小可相交路径覆盖 先预处理可到达的点然后转化为最小不相交路径覆盖 type node=record point,next:longint; end; ..] of node; ...
- C# VS2010中,用微软自带的System.Data.OracleClient来连接Oracle数据库
由于微软在.Net框架4.0中已经决定撤销使用System.Data.OracleClient,造成在VS2010中无法连接Oracle数据库,但它还依旧存在于.Net架构中,我们可以通过自己引用 C ...
- [CODEVS1037]取数游戏
N(2 <=N<=200,且为偶数)个正整数的序列放在一个游戏平台上,A.B两人轮流从序列的两端取数,取数后该数字被去掉并累加到本玩家的得分中,当数取尽时,游戏结束.以最终得分多者为胜(A ...
- vijosP1164 曹冲养猪
vijosP1164 曹冲养猪 链接:https://vijos.org/p/1164 [思路] 数学. 如果x不能满足模公式则+gcd,gcd=a的积(a互质)使加上gcd后依然满足前面的模公式. ...
- IntelliJ 直接编辑国际化文件(properties)方法
IntelliJ 直接编辑国际化文件(properties)方法 settings-File Encodings 右下角 Transparent native-to-ascii conversion的 ...
- HW3.21
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- weblogic启动时日志重定向(nohup.out)
由于weblogic使用 nohup ./startWebLogic.sh & 启动时会将所有日志打印到nohup.out上,长此以往会导致该文件越来越大,不便于管理. 故下面介绍如何重 ...
- golang中赋值string到array
要把一个string赋值给一个array,哥哥遇到一个纠结的困难,研究一番,发现主要原因是array和slice在golang里不是一个东西,本文提供两种解决方案. 在网络编程中network pac ...
- H5离线缓存机制-manifest
简介:Manifest 其实就是web应用的一种缓存机制,主要用于现在webapp应用中,它是浏览器自己的一种机制,随着移动互联网时代的到来,网络可靠性降低,如果我们已经将需要的文件缓存下下来,一旦网 ...
- Android框架结构图