- 浏览: 203472 次
- 来自: ...
文章分类
最新评论
-
赤道螞蟻:
如果是數據庫有定時任務,定時更新表的數據。 表中數據變化時,主 ...
用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 1588首先安装prototype for nodejs 引用npm ... -
Underscore.js与nodejs相结合
2011-12-14 11:18 10451今天发现这个Underscore.js,文档说 http:// ... -
NowJS and Reconnects
2011-12-14 09:03 1144NowJS and Reconnects 保留 原文http: ... -
Nodejs中的EventEmitter
2011-12-14 04:57 26805EventEmitter是nodejs核心的一部分。很多nod ... -
nodejs+nowjs实现聊天室
2011-12-13 05:05 9456支持多房间,支持私聊,使用nowjs库,服务器端代码不到100 ... -
nowjs和nodejs结合的简单示例
2011-12-10 07:55 2712功能:把服务器端的时间new Date().getTime() ... -
nodejs运行coffeescript
2011-12-10 03:05 2741首先安装coffee-script 引用npm install ... -
用socket.io实现WebSocket的一个简单例子
2011-12-07 10:47 40019用socket.io实现WebSocket的一个简单例子 客 ...
相关推荐
如果一个块落下的值加上水平或垂直块序列的值等于 7,那么下落的块和该块序列将消失。 这些方块上方的任何方块都会掉落并且可能会触发连击。 7 的块本身不会清除,因为总和中必须涉及 2 个或更多块。 这个规则只有...
通过Web应用程序轻松构建vimrc为什么构建vimrc文件应该并不困难。 毕竟,您要配置的代码编辑器是1991年发明的。 没关系,您的经验水平是不变的,总有新的方法可以改变vim的经验,但是由于提示分散在Internet上,因此...
图形化a+b,可以锻炼你的记忆力和算数速度
柔性输送线sw18可编辑全套技术资料100%好用.zip
本汽车票网上预订系统管理员和用户。管理员功能有个人中心,用户管理,汽车票管理,订单管理,退票管理,换票管理,反馈管理,留言板管理,系统管理等。用户功能有个人中心,汽车票管理,订单管理,退票管理,换票管理,反馈管理等。 内含文档,可轻松上手。
自动锁螺丝机细化完全step全套技术资料100%好用.zip
【创新无忧】基于matlab龙格库塔算法RUN优化极限学习机KELM故障诊断【含Matlab源码 10715期】.zip
pll电荷泵锁相环 cppll(已流片)仿真环境搭建好了 电路到版图都已流片验证,另外送PLL书籍电子版和对应工艺库。 另加50就可以得到完整版图 三阶二型锁相环 参考频率50-100MHz 分频比可调 锁定频率600M-2GHz 锁定时间4us 环形振荡器 ring vco 鉴频鉴相器PFD模块 分频器DIV模块 ,ps counter 电荷泵CP模块
智慧社区有管理员和客户两个角色。客户功能有车位信息,社区信息,周边服务,问卷调查,爱心助老,通知公告,留言反馈,个人中心,客服中心,在线报修管理,投诉建议管理,车位租买管理,社区信息管理,参与答卷管理,我的收藏管理。管理员功能有个人中心,客户管理,在线报修管理,投诉建议管理,车位信息管理,车位租买管理,社区信息管理,周边服务管理,问卷调查管理,参与答卷管理,爱心助老管理,留言板管理,系统管理。 内含文档,可轻松上手。
本科生课程设计封面.doc
展示PRD文档的关键要素编写具体示例。同时提供了一份模板,方便撰写PRD文档。
基于matlab的用于分析弧齿锥齿轮啮合轨迹的程序,输出齿轮啮合轨迹及传递误差。 程序已调通,可直接运行。 程序保证可直接运行。
【创新无忧】基于matlab向量加权平均算法INFO优化极限学习机KELM故障诊断【含Matlab源码 10732期】.zip
仓库管理系统(一个毕设) 毕业设计项目《仓库管理系统(manager_sys)》的概述和指南: 项目标题 《基于Spring MVC和Vue.js的仓库管理系统设计与实现 —— 毕业设计项目》 项目概述 本项目是一个基于Spring MVC、Spring Security、Spring、MyBatis、PageHelper和Vue.js框架的仓库管理系统。系统旨在提供高效、安全的库存管理解决方案,包括权限管理、商品管理、订单处理和库存预警等功能。 系统特点 权限管理:利用Spring Security实现基于角色的访问控制(RBAC),动态分配权限。 业务流程:涵盖商品、订单、库存的完整操作流程,确保库存管理的准确性。 日志记录:通过Spring AOP实现操作日志的记录,便于追踪和审计。 数据统计:首页展示商品销量统计图和每日销售统计图,直观展示业务状况。 系统预览 登录和首页:用户登录后进入系统首页,查看统计信息。 产品管理:管理商品信息,包括添加、修改、删除等操作。 订单管理:处理订单,包括创建订单、更新库存等。 权限管理:管理用户角色和权限。 日志管理:查看系统操作日志。 运
A星算法 A*算法 自己研究编写的Matlab路径规划算法 Astar算法走迷宫 可自行设置起始点,目标点,自由更地图。 ——————————————————— 可以和人工势场法融合 动态障碍物
《MATLAB神经网络原理与实例精解》是一本深度学习初学者的理想教程,它全面涵盖了神经网络的基础理论以及MATLAB实现方法。这本书旨在帮助读者理解神经网络的工作原理,并通过具体的MATLAB实例,让读者能够动手实践,从而深入掌握神经网络在实际问题中的应用。 神经网络是一种模仿人脑神经元结构的计算模型,它由大量的处理单元——神经元组成,通过权重连接形成复杂的网络结构。在深度学习领域,神经网络被广泛用于图像识别、语音识别、自然语言处理等任务,因其强大的非线性建模能力而备受青睐。 MATLAB作为一个强大的数值计算和数据可视化环境,为构建和训练神经网络提供了便利的工具箱。MATLAB神经网络工具箱(Neural Network Toolbox)包含了各种类型的神经网络模型,如前馈网络、卷积网络、递归网络等,以及训练算法,如反向传播、遗传算法等。通过这些工具,用户可以快速构建网络结构,调整参数,进行训练和验证,并将模型应用于实际数据。 本书首先会介绍神经网络的基本概念,包括感知机、多层前馈网络和反向传播算法。然后,将详细讲解如何在MATLAB中搭建这些网络,包括网络结构的设计、权重初始
Matlab领域上传的视频是由对应的完整代码运行得来的,完整代码皆可运行,亲测可用,适合小白; 1、从视频里可见完整代码的内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2019b;若运行有误,根据提示修改;若不会,私信博主; 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可私信博主; 4.1 博客或资源的完整代码提供 4.2 期刊或参考文献复现 4.3 Matlab程序定制 4.4 科研合作
ABAQUS动,静力学模型;车辆-轨道耦合动力学;钢轨不平顺程序;批量非线性弹簧;单向弹簧(收拉不受压或受压不受拉),温度耦合等。 轨道检算(超高,超限,出报告);土木建筑有限元建模分析。
教学督导检查情况表.docx
基于springboot的逍遥大药房管理系统--论文.zip