- 浏览: 203778 次
- 来自: ...
文章分类
最新评论
-
赤道螞蟻:
如果是數據庫有定時任務,定時更新表的數據。 表中數據變化時,主 ...
用socket.io实现WebSocket的一个简单例子 -
cwalet:
在世界的中心呼喚愛 写道提示找不到 expressnpm in ...
用socket.io实现WebSocket的一个简单例子 -
在世界的中心呼喚愛:
提示找不到 express
用socket.io实现WebSocket的一个简单例子 -
Anleb:
def m1(a)
puts 'invoke m1'
pu ...
Ruby的一些疑问 -
biyeah:
补充,任何类,只要实现to_proc方法,都可以与&结 ...
Ruby的一些疑问
参考:http://www.bennadel.com/blog/2171-Realtime-Messaging-And-Synchronization-With-NowJS-And-Node-js.htm
先说例子实现的功能。网页上有一图片,图片可以拖动。用浏览器打开多个同一网页,当图片移动时,其它页面的图片会同步移动。例子也展示了用jQuery实现图片的拖动。
测试环境window7,nodejs v0.6.5 分别用ie,firefox,chrome打开http://127.0.0.1:8080/client.html,所有该网页上的图片会同步移动。贴上代码。
server.js端:
需要用sock.io和nowjs第三包,推荐用npm方式安装。nowjs包在window的安装可参考:
http://blog.nowjs.com/running-nowjs-natively-on-windows
我把重要的东西摘录下来:
client.html
先说例子实现的功能。网页上有一图片,图片可以拖动。用浏览器打开多个同一网页,当图片移动时,其它页面的图片会同步移动。例子也展示了用jQuery实现图片的拖动。
测试环境window7,nodejs v0.6.5 分别用ie,firefox,chrome打开http://127.0.0.1:8080/client.html,所有该网页上的图片会同步移动。贴上代码。
server.js端:
需要用sock.io和nowjs第三包,推荐用npm方式安装。nowjs包在window的安装可参考:
http://blog.nowjs.com/running-nowjs-natively-on-windows
// Include the necessary modules. var sys = require("util"); var http = require("http"); var url = require("url"); var path = require("path"); var fileSystem = require("fs"); // ---------------------------------------------------------- // // ---------------------------------------------------------- // // Create an instance of the HTTP server. var server = http.createServer( function (request, response) { // Get the requested "script_name". This is the part of the // path after the server_name. var scriptName = request.url; // Convert the script name (expand-path) to a physical file // on the local file system. var requestdFilePath = path.join(process.cwd(), scriptName); // Read in the requested file. Remember, since all File I/O // (input and output) is asynchronous in Node.js, we need to // ask for the file to be read and then provide a callback // for when that file data is available. // // NOTE: You can check to see if the file exists *before* you // try to read it; but for our demo purposes, I don't see an // immediate benefit since the readFile() method provides an // error object. fileSystem.readFile( requestdFilePath, "binary", function (error, fileBinary) { // Check to see if there was a problem reading the // file. If so, we'll **assume** it is a 404 error. if (error) { // Send the file not found header. response.writeHead(404); // Close the response. response.end(); // Return out of this guard statement. return; } // If we made it this far then the file was read in // without a problem. Set a 200 status response. response.writeHead(200); // Serve up the file binary data. When doing this, we // have to set the encoding as binary (it defaults to // UTF-8). response.write(fileBinary, "binary"); // End the response. response.end(); } ); } ); // Point the server to listen to the given port for incoming // requests. server.listen(8080); // ---------------------------------------------------------- // // ---------------------------------------------------------- // // Create a local memory space for further now-configuration. (function () { // Now that we have our HTTP server initialized, let's configure // our NowJS connector. var nowjs = require("now"); // After we have set up our HTTP server to serve up "Static" // files, we pass it off to the NowJS connector to have it // augment the server object. This will prepare it to serve up // the NowJS client module (including the appropriate port // number and server name) and basically wire everything together // for us. // // Everyone contains an object called "now" (ie. everyone.now) - // this allows variables and functions to be shared between the // server and the client. var everyone = nowjs.initialize(server); // Create primary key to keep track of all the clients that // connect. Each one will be assigned a unique ID. var primaryKey = 0; // When a client has connected, assign it a UUID. In the // context of this callback, "this" refers to the specific client // that is communicating with the server. // // NOTE: This "uuid" value is NOT synced to the client; however, // when the client connects to the server, this UUID will be // available in the calling context. everyone.connected( function () { this.now.uuid = ++primaryKey; } ); // Add a broadcast function to *every* client that they can call // when they want to sync the position of the draggable target. // In the context of this callback, "this" refers to the // specific client that is communicating with the server. everyone.now.syncPosition = function (position) {//syncPosition()在这里定义,在客户端调用 // Now that we have the new position, we want to broadcast // this back to every client except the one that sent it in // the first place! As such, we want to perform a server-side // filtering of the clients. To do this, we will use a filter // method which filters on the UUID we assigned at connection // time. everyone.now.filterUpdateBroadcast(this.now.uuid, position); }; // We want the "update" messages to go to every client except // the one that announced it (as it is taking care of that on // its own site). As such, we need a way to filter our update // broadcasts. By defining this filter method on the server, it // allows us to cut down on some server-client communication. everyone.now.filterUpdateBroadcast = function (masterUUID, position) { // Make sure this client is NOT the same client as the one // that sent the original position broadcast. if (this.now.uuid == masterUUID) { // Return out of guard statement - we don't want to // send an update message back to the sender. return; } // If we've made it this far, then this client is a slave // client, not a master client. this.now.updatePosition(position);//updatePosition()为客户端定义的方法,在这里可调用,用this修饰now。 }; })(); // ---------------------------------------------------------- // // ---------------------------------------------------------- // // Write debugging information to the console to indicate that // the server has been configured and is up and running. sys.puts("Server is running on 8080");
我把重要的东西摘录下来:
引用
Once the core HTTP server is configured and the NowJS module is initialized, we are given access to the "everyone" object. This everyone object then provides us with access to the server-side "now" scope. This "now" scope is shared between the server and every one of the clients. Anything added to or removed from the server-side "now" scope is also added to or removed from every client currently (or eventually) connected to the server.
This is true for both variables and functions! Notice that my server-side Node.js code defines two methods: syncPosition() and filterUpdateBroadcast(). By defining them in the "everyone.now" scope, I am making them available to both the server and to every single one of the connected clients.
But what about that, "everyone.now.updatePosition()", function? Where did that come from? Ah-ha! Here's the real, "there is no spoon" mind-screw - that function is defined on the client (which we'll see in a minute). And, since it's defined in the client's "now" scope, the server-side Javascript can then invoke it as if there were no separation between the server and client contexts.
This is true for both variables and functions! Notice that my server-side Node.js code defines two methods: syncPosition() and filterUpdateBroadcast(). By defining them in the "everyone.now" scope, I am making them available to both the server and to every single one of the connected clients.
But what about that, "everyone.now.updatePosition()", function? Where did that come from? Ah-ha! Here's the real, "there is no spoon" mind-screw - that function is defined on the client (which we'll see in a minute). And, since it's defined in the client's "now" scope, the server-side Javascript can then invoke it as if there were no separation between the server and client contexts.
client.html
<!DOCTYPE html> <html> <head> <title>NowJS And Node.js Realtime Communication</title> <style type="text/css"> html, body { height: 100%; overflow: hidden; width: 100%; } img { left: 9px; position: absolute; top: 70px; } </style> <!-- We have this file stored explicitly. --> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"/> </script> <!-- The NowJS HTTP augmentation will take care of routing this - we don't actually have this physical file stored at this file path. --> <script type = "text/javascript" src = "/nowjs/now.js" ></script> </head> <body> <h1> NowJS And Node.js Realtime Communication </h1> <!-- This will be draggable. When this image drags, we are going to sync the position of it across browsers. --> <img id="myhead" src="./myhead.gif" width="100" height="100" alt="It's my head, do you like it?" /> <!-- Configure the client-side script. --> <script type="text/javascript"> // Get a reference to the target draggable. var myhead = $("#myhead"); // Get a reference to the body - this is the element on which // we'll be tracking mouse movement once the draggable // tracking has been turned on. var body = $("body"); // On mouse-down, turn on draggability. myhead.mousedown( function (event) { // Prevent the default behavior. event.preventDefault(); // Get the current position of the mouse within the // bounds of the target. var localOffset = { x:(event.pageX - myhead.position().left), y:(event.pageY - myhead.position().top) }; // Start tracking the mouse movement on the body. // We're tracking on the body so that the mouse can // move faster than the tracking. body.mousemove( function (event) { // Create a new position object. var newPosition = { left:(event.pageX - localOffset.x), top:(event.pageY - localOffset.y) }; // Update the target position locally. myhead.css(newPosition); // Announce the updated position so that we // can sync accross all clients with NowJS. now.syncPosition(newPosition);//syncPosition()是在服务器端定义的方法,可在客户端调用。 } ); } ); // On mouse-up, turn off draggability. myhead.mouseup( function (event) { // Unbind the mousemove - no need to track movement // once the mouse has been lifted. body.unbind("mousemove"); } ); // I allow the remove server to make a request to update the // position of the target. // // NOTE: By defining this function in the NOW scope, it gives // the server access to it as well. now.updatePosition = function (newPosition){ //updatePosition()这个方法在客户端定义,可在服务器端调用 // Check to see if this client is in master mode; if so, // we won't update the position as this client is // actively updating its own position. myhead.css(newPosition); }; </script> </body> </html>
引用
When the user moves the image on the client, the client broadcasts the new position using the "now.syncPosition()" function. This function, which was defined on the server, then pushes the updated position down to all the other clients using the "now.updatePosition()" function, which was defined on the client.
Even after coding this myself, it's still somewhat confusing; so, let's look at a quick rundown of the various functions to see where they were defined and where they were invoked:
syncPosition()
filterUpdateBroadcast()
updatePosition()
Even after coding this myself, it's still somewhat confusing; so, let's look at a quick rundown of the various functions to see where they were defined and where they were invoked:
syncPosition()
- Defined: Server-side
- Invoked: Client-side
filterUpdateBroadcast()
- Defined: Server-side
- Invoked: Server-side
updatePosition()
- Defined: Client-side
- Invoked: Server-side
发表评论
-
coffeescript+prototypejs使编写nodejs更象ruby-like
2011-12-21 12:01 1595首先安装prototype for nodejs 引用npm ... -
Underscore.js与nodejs相结合
2011-12-14 11:18 10461今天发现这个Underscore.js,文档说 http:// ... -
NowJS and Reconnects
2011-12-14 09:03 1146NowJS and Reconnects 保留 原文http: ... -
Nodejs中的EventEmitter
2011-12-14 04:57 26817EventEmitter是nodejs核心的一部分。很多nod ... -
nodejs+nowjs实现聊天室
2011-12-13 05:05 9463支持多房间,支持私聊,使用nowjs库,服务器端代码不到100 ... -
nowjs和nodejs结合的简单示例
2011-12-10 07:55 2716功能:把服务器端的时间new Date().getTime() ... -
nodejs运行coffeescript
2011-12-10 03:05 2753首先安装coffee-script 引用npm install ... -
用socket.io实现WebSocket的一个简单例子
2011-12-07 10:47 40030用socket.io实现WebSocket的一个简单例子 客 ...
相关推荐
如果一个块落下的值加上水平或垂直块序列的值等于 7,那么下落的块和该块序列将消失。 这些方块上方的任何方块都会掉落并且可能会触发连击。 7 的块本身不会清除,因为总和中必须涉及 2 个或更多块。 这个规则只有...
通过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仿真"