NowJS and Reconnects 保留
原文http://blog.nowjs.com/
This particular feature has been highly anticipated by the NowJS community for some time. With the switch to Socket.IO 0.6.18, which now has built-in support for socket reconnecting, implementation of this feature was actually quite doable. We've tested with both simulated server downtime and client network issues, and it's worked satisfactorily. Several minor changes were necessary to support this feature in Now -- nothing that actually affects the API for developers using this tool; just a few changes to how things work in the backend.
Several disclaimers to make about Now and the general state of reconnects: first of all, it's not possible to preserve the client's session ID without forking Socket.IO. I have it from one of the Socket.IO folks that 0.7 will make everything public, including generateSessionId(), so when we bridge over to that version (we'll have to make sure that nothing breaks -- we've been developing a test suite, so that should hopefully ease the transition), this may change.
Additionally, client groups are not preserved across reconnects. One way to handle this would be to have the client know which groups he's in, and then send this information when the socket's reconnect event is triggered.
Note that client scopes, however, are preserved, so in the multiroom chat example, for instance, a reconnecting user originally in room 3 will be treated as if he's in room 1, but he'll still have the same name.
And now, the code.
The first block of code, pulled from handleDisconnection(), is as follows:
// y-combinator trick
(function (y) {
y(now, y, [now]);
})(function (obj, fn, seen) {
for (var i in obj) {
if (obj[i] && seen.indexOf(obj[i]) === -1 &&
typeof obj[i] === 'object' && obj[i] != document) {
seen[seen.length] = obj[i];
fn(obj[i], fn, seen);
}
else if (typeof obj[i] === 'function' && obj[i].remote) {
delete obj[i];
}
}
});
I've omitted the tiny portion from constructRemoteFunction() where I tagged all remote functions as such. Essentially, this snippet runs through the now object and deletes all remote functions -- if this is not done, then upon reconnecting, the user will find himself running into infinite recursion -- when re-establishing the connection, since the client transmits his scope first, the server-side function will be redefined to call the client-side function, which simply calls the server-side function.
About the actual code: I figured it would be amusing to use the y-combinator trick instead of conventional methods which involve binding values to variables, especially since Flotype (the company behind NowJS) is a YC-funded company. Plus, it's just an awesome concept.
The other snippet, pulled from handleNewConnection(), is both straightforward and standard:
if (client.handled) return;
client.handled = true;
Since the rest of the function involves attaching function listeners, this ensures that these listeners are only attached once.
Well, that's that. Let us know what you think in the comments below, and in our IRC channel #nowjs on irc.freenode.net
分享到:
相关推荐
这个游戏可能类似于俄罗斯... 我在 2012 年 3 月使用limejs now nowjs 在几天内制作了这个游戏,但我决定发布代码,因为我非常喜欢这个游戏概念。 演示 。 请注意,由于它是作为 github 上的静态页面托管的,因此
通过Web应用程序轻松构建vimrc为什么构建vimrc文件应该并不困难。 毕竟,您要配置的代码编辑器是1991年发明的。 没关系,您的经验水平是不变的,总有新的方法可以改变vim的经验,但是由于提示分散在Internet上,因此...
nodejs010-nodejs-cryptiles-0.2.2-1.el6.centos.alt.noarch.rpm
免费JAVA毕业设计 2024成品源码+论文+数据库+启动教程 启动教程:https://www.bilibili.com/video/BV1SzbFe7EGZ 项目讲解视频:https://www.bilibili.com/video/BV1Tb421n72S 二次开发教程:https://www.bilibili.com/video/BV18i421i7Dx
基于麻雀搜索算法优化的深度置信网络(SSA-DBN)参数调整与数据分类预测——以隐藏层节点、迭代次数和学习率为优化目标的MATLAB实现,基于麻雀搜索算法优化深度置信网络(SSA-DBN)的数据分类预测 优化参数为隐藏层节点、迭代次数和学习率 利用交叉验证抑制过拟合问题 matlab代码, ,SSA-DBN; 参数优化; 隐藏层节点; 迭代次数; 学习率; 交叉验证; 过拟合抑制; MATLAB代码,基于SSA-DBN优化的数据分类预测方法:参数优化与过拟合抑制
BeTheme第一次发布于2014年5月21日,自那时以来,已有数以百万计的人下载了BeTheme,其评分为4.8。这个主题是WooCommerce支持的,在此帮助下,您可以制作一个电子商务网站,还可以制作博客、新闻和其他类型的网站。BeTheme 21.5.6 wordpress主题模板特点:放大器支撑多用途主题500+预制件演示单击演示安装移动友好型主题联络表格7支持自转滑块。
基于S7-200智能控制与组态王4x3界面的书架式堆垛立体车库系统设计与应用,基于S7-200和组态王4x3书架式堆垛式立体库立体车库 ,S7-200; 组态王4x3; 书架式堆垛式立体库; 立体车库,基于S7-200与组态王4x3的立体车库系统
1、文件内容:pykde4-akonadi-4.10.5-6.el7.rpm以及相关依赖 2、文件形式:tar.gz压缩包 3、安装指令: #Step1、解压 tar -zxvf /mnt/data/output/pykde4-akonadi-4.10.5-6.el7.tar.gz #Step2、进入解压后的目录,执行安装 sudo rpm -ivh *.rpm 4、安装指导:私信博主,全程指导安装
基于28379D的异步电机无速度传感器控制:MD500与MD500E滑模同步调制代码研究,各种代码md500代码,异步电机,基于28379D,带无速度传感器控制,参数辨识,同步调制等功能。 还有md500e代码,滑模无感代码,逆变整流代码 ,核心关键词:md500代码; 异步电机; 28379D; 无速度传感器控制; 参数辨识; 同步调制; md500e代码; 滑模无感控制; 逆变整流代码。,基于28379D的MD500电机异步控制系统与参数辨识软件
"可再生能源驱动的热电联供微网经济运行优化研究:基于具体文献的程序复现与MATLAB粒子群算法应用",含可再生能源的热电联供型微网经济运行优化 有具体文献 程序复现 MATLAB粒子群算法 ,核心关键词: 可再生能源; 热电联供型微网; 经济运行优化; 具体文献; 程序复现; MATLAB粒子群算法。,含可再能源热电联供型微网运行优化策略复现于特定文献中的MATLAB模型研究。
1、文件内容:pyserial-2.6-6.el7.rpm以及相关依赖 2、文件形式:tar.gz压缩包 3、安装指令: #Step1、解压 tar -zxvf /mnt/data/output/pyserial-2.6-6.el7.tar.gz #Step2、进入解压后的目录,执行安装 sudo rpm -ivh *.rpm 4、安装指导:私信博主,全程指导安装
finishBitmap.jpg
"英博尔控制器调速软件全面升级,引领行业新风尚",英博尔控制器调速软件全新 ,英博尔; 控制器; 调速软件; 全新,英博尔控制器调速软件全新升级
电机定子模态频率计算方法及公式在Excel表格中的应用,电机定子模态频率计算公式,公式法,exl表格 ,电机定子模态频率计算公式; 公式法; EXL表格,电机定子模态频率计算方法及公式法在Excel表格中的应用
一、项目简介 包含:项目源码、数据库脚本等,该项目附带全部源码可作为毕设使用。 二、技术实现 jdk版本:1.8 及以上 ide工具:IDEA或者eclipse 数据库: mysql5.5及以上 后端:spring+springboot+mybatis+maven+mysql 前端: vue , css,js , elementui 三、系统功能 1、系统角色主要包括:管理员、用户 2、系统功能 主要功能包括: 用户登录注册 首页 个人中心 修改密码 个人信息 用户管理 管理员管理 问卷管理 题目管理 题目统计 问卷调查管理 新闻资讯管理 轮播图管理 问卷调查 新闻资讯 个人中心 问卷调查记录 后台管理 详见 https://flypeppa.blog.csdn.net/article/details/143189415
免费JAVA毕业设计 2024成品源码+论文+数据库+启动教程 启动教程:https://www.bilibili.com/video/BV1SzbFe7EGZ 项目讲解视频:https://www.bilibili.com/video/BV1Tb421n72S 二次开发教程:https://www.bilibili.com/video/BV18i421i7Dx
1、文件内容:pulseaudio-esound-compat-10.0-6.el7_9.rpm以及相关依赖 2、文件形式:tar.gz压缩包 3、安装指令: #Step1、解压 tar -zxvf /mnt/data/output/pulseaudio-esound-compat-10.0-6.el7_9.tar.gz #Step2、进入解压后的目录,执行安装 sudo rpm -ivh *.rpm 4、安装指导:私信博主,全程指导安装
免费JAVA毕业设计 2024成品源码+论文+数据库+启动教程 启动教程:https://www.bilibili.com/video/BV1SzbFe7EGZ 项目讲解视频:https://www.bilibili.com/video/BV1Tb421n72S 二次开发教程:https://www.bilibili.com/video/BV18i421i7Dx
一种基于Lifelogging视频的文本标签生成模型.pdf
MATLAB仿真:MIMO系统FLMS算法的优化与实现,一个mimo系统的flms算法的MATLAB仿真 ,Mimo系统; FLMS算法; MATLAB仿真,"MIMO系统FLMS算法MATLAB仿真"