- 浏览: 204212 次
- 来自: ...
-
文章分类
最新评论
-
赤道螞蟻:
如果是數據庫有定時任務,定時更新表的數據。 表中數據變化時,主 ...
用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 1603首先安装prototype for nodejs 引用npm ... -
Underscore.js与nodejs相结合
2011-12-14 11:18 10464今天发现这个Underscore.js,文档说 http:// ... -
NowJS and Reconnects
2011-12-14 09:03 1153NowJS and Reconnects 保留 原文http: ... -
Nodejs中的EventEmitter
2011-12-14 04:57 26826EventEmitter是nodejs核心的一部分。很多nod ... -
nodejs+nowjs实现聊天室
2011-12-13 05:05 9472支持多房间,支持私聊,使用nowjs库,服务器端代码不到100 ... -
nowjs和nodejs结合的简单示例
2011-12-10 07:55 2719功能:把服务器端的时间new Date().getTime() ... -
nodejs运行coffeescript
2011-12-10 03:05 2766首先安装coffee-script 引用npm install ... -
用socket.io实现WebSocket的一个简单例子
2011-12-07 10:47 40044用socket.io实现WebSocket的一个简单例子 客 ...
相关推荐
如果一个块落下的值加上水平或垂直块序列的值等于 7,那么下落的块和该块序列将消失。 这些方块上方的任何方块都会掉落并且可能会触发连击。 7 的块本身不会清除,因为总和中必须涉及 2 个或更多块。 这个规则只有...
通过Web应用程序轻松构建vimrc为什么构建vimrc文件应该并不困难。 毕竟,您要配置的代码编辑器是1991年发明的。 没关系,您的经验水平是不变的,总有新的方法可以改变vim的经验,但是由于提示分散在Internet上,因此...
【清华大学】DeepSeek从入门到精通(视频课程+PDF)
自2019年以来,教育部启动实施“双高计划”,遴选确定首批“双高计划”建设单位197所,其中高水平学校建设单位56所,高水平专业群建设单位141所,河南省有黄河水利职业技术学院、河南工业职业技术学院等6所职业学校入选。2022年,教育部开展国家“双高计划”中期绩效评价,从评价结果看,国家“双高计划”任务进展顺利,建设成效突出,形成了一批先进经验做法和典型案例,在引领职业教育改革、服务国家战略和支撑区域发展方面形成示范势头。 今天,我们给大家分享一些“双高计划”专业群完整申报书与建设方案和中期评估报告。 ## 一、专业群完整申报书与建设方案 ## 二、“双高计划”中期报告 (100多份)
内容概要:本文详细探讨了电商平台上秒杀系统中减库存的设计逻辑和技术优化方法。首先,文中阐述了‘下单减库存’、‘付款减库存’和‘预扣库存’三种常见方式及其各自面临的问题和局限性,尤其是面对高并发流量冲击下的系统稳定性与数据准确性保障挑战。接着讨论了适用于大规模促销活动中快速而精准地扣除存货的方法,提出了诸如应用本地缓存(Local Cache)、引入高性能持久化键值存储(如Redis),甚至修改数据库引擎源代码(InnoDB 层面排队机制)等一系列先进解决方案来确保交易流程顺畅。此外,还提到了在极端情况发生(例如超卖)时如何借助补救措施挽回损失的具体实例。 适合人群:电商平台开发运维技术人员;有兴趣深入了解电商业务架构和技术优化的开发者和IT管理人员。 使用场景及目标:①帮助设计师理解不同减库存策略的应用时机及其利弊;②指导程序员针对特定业务需求选择最适合的技术路径进行项目构建;③提供给运维专家关于改善在线交易平台响应速度和服务质量的专业见解。 其他说明:本篇文章对于构建高效的电子商贸系统有着极高的参考价值,尤其是那些准备应对瞬息万变市场环境下的企业来说尤为重要。它不仅限于理论探讨层面,
动态表单,VUE动态表单。基于vue+elementplus实现动态表单组件,通过拖拽组件到面板即可实现一个表单。支持各个组件的动态隐藏显示,动态表格弹窗式维护。
【毕业设计】java-springboot-vue家居日用小百货交易网站实现源码(完整前后端+mysql+说明文档+LunW).zip
【毕业设计】java-springboot+vue火锅店管理系统源码(完整前后端+mysql+说明文档+LunW).zip
随着信息技术在管理上越来越深入而广泛的应用,管理信息系统的实施在技术上已逐步成熟。本文介绍了微服务在线教育系统的开发全过程。通过分析微服务在线教育系统管理的不足,创建了一个计算机管理微服务在线教育系统的方案。文章介绍了微服务在线教育系统的系统分析部分,包括可行性分析等,系统设计部分主要介绍了系统功能设计和数据库设计。 本微服务在线教育系统有管理员,用户两个角色。管理员功能有个人中心,用户管理,课程信息管理,课程类型管理,学科管理,购买的课程管理,职业规划管理,视频点播管理,我的笔记管理,我的课程管理,消息通知管理,学习交流,试卷管理,留言板管理,试题管理,系统管理,考试管理。用户功能有个人中心,用户管理,购买的课程管理,我的笔记管理,我的课程管理,消息通知管理。因而具有一定的实用性。 本站是一个B/S模式系统,采用SSM框架,MYSQL数据库设计开发,充分保证系统的稳定性。系统具有界面清晰、操作简单,功能齐全的特点,使得微服务在线教育系统管理工作系统化、规范化。本系统的使用使管理人员从繁重的工作中解脱出来,实现无纸化办公,能够有效的提高微服务在线教育系统管理效率。 关键词:微服务在线教育系统;SSM框架;MYSQL数据库;Spring Boot
javascript 基于Javascript实现,强化学习QLearning的一个贪吃蛇实例.
python教程学习
我国科学技术的不断发展,计算机的应用日渐成熟,其强大的功能给人们留下深刻的印象,它已经应用到了人类社会的各个层次的领域,发挥着重要的不可替换的作用。信息管理作为计算机应用的一部分,使用计算机进行管理,具有非常明显的优点,利用网络的优势特开发了本基于Spring Boot的IT技术交流和分享平台。 本IT技术交流和分享平台是基于Spring Boot框架,采用Java技术,MYSQL数据库进行开发的。系统具有灵活的一体化设计方式,圆满完成了整个系统的界面设计。本系统实现了用户功能模块和管理员功能模块两大部分,通过该系统用户可以快速进行IT技术交流和分享,管理员可登录系统后台对系统进行全面管理,确保系统正常稳定的运行。系统功能齐全,符合用户IT技术交流和分享的需求。 本文主要首先介绍了课题背景、设计原则和研究内容,系统采用的相关技术及开发平台,接着对本基于Spring Boot的IT技术交流和分享平台进行系统需求分析和设计,包括系统的功能模块,数据库的设计,系统结构以及系统界面设计等,最后对进行系统测试,完成本篇论文。 关键词:IT技术交流, Spring Boot框架, Java技术,MYSQL数据库
疲劳检测yawn图片数据集
JDK7通过java-jwt验证
【毕业设计】java-springboot+vue会议管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip
python学习资源
51CTO 1、技术解析篇-DeepSeek入门宝典 2、开发实战篇-DeepSeek入门宝典 3、行业应用篇-DeepSeek入门宝典 4、个人使用篇-DeepSeek入门宝典
内容概要:本文档是由高正奇编辑的针对模式识别和机器学习(PRML)教科书的一份详细的解答手册。文档覆盖了从基本概念如误差函数求导、贝叶斯定理应用到多元高斯分布计算、Gamma函数积分及其性质等一系列复杂问题的解决方案,以及涉及线性模型分类的基础练习题、条件概率和联合概率计算等入门级习题。每一题都经过细致推导,帮助学生加深对机器学习相关概念的理解并掌握具体的数学方法。 适合人群:主要适用于正在攻读机器学习、模式识别相关课程的学生,以及从事数据科学工作的专业人士作为深入理解和实践指南。 使用场景及目标:本手册旨在辅助教学过程中遇到的具体难题解析,在研究和实践中作为参考资料进行理论验证和技术难点突破,尤其有助于准备考试或者项目实施时需要巩固知识的应用场合。 其他说明:书中题目涵盖广泛,既有直观的概率论应用,也有复杂的积分变换技巧和最优化思路展示,对于希望提高自身计算能力和解决实际问题能力的学习者非常有价值。但要注意的是,部分内容较为深奥,可能不适合初学者自学使用,最好配合课堂讲解或其他教材一起学习效果更佳。
python学习资源
RFID-MATLAB的高等数学-CH06.rar