- 浏览: 203934 次
- 来自: ...
-
文章分类
最新评论
-
赤道螞蟻:
如果是數據庫有定時任務,定時更新表的數據。 表中數據變化時,主 ...
用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 1600首先安装prototype for nodejs 引用npm ... -
Underscore.js与nodejs相结合
2011-12-14 11:18 10461今天发现这个Underscore.js,文档说 http:// ... -
NowJS and Reconnects
2011-12-14 09:03 1148NowJS and Reconnects 保留 原文http: ... -
Nodejs中的EventEmitter
2011-12-14 04:57 26820EventEmitter是nodejs核心的一部分。很多nod ... -
nodejs+nowjs实现聊天室
2011-12-13 05:05 9464支持多房间,支持私聊,使用nowjs库,服务器端代码不到100 ... -
nowjs和nodejs结合的简单示例
2011-12-10 07:55 2717功能:把服务器端的时间new Date().getTime() ... -
nodejs运行coffeescript
2011-12-10 03:05 2756首先安装coffee-script 引用npm install ... -
用socket.io实现WebSocket的一个简单例子
2011-12-07 10:47 40033用socket.io实现WebSocket的一个简单例子 客 ...
相关推荐
如果一个块落下的值加上水平或垂直块序列的值等于 7,那么下落的块和该块序列将消失。 这些方块上方的任何方块都会掉落并且可能会触发连击。 7 的块本身不会清除,因为总和中必须涉及 2 个或更多块。 这个规则只有...
通过Web应用程序轻松构建vimrc为什么构建vimrc文件应该并不困难。 毕竟,您要配置的代码编辑器是1991年发明的。 没关系,您的经验水平是不变的,总有新的方法可以改变vim的经验,但是由于提示分散在Internet上,因此...
重点:所有项目均附赠详尽的SQL文件,这一细节的处理,让我们的项目相比其他博主的作品,严谨性提升了不止一个量级!更重要的是,所有项目源码均经过我亲自的严格测试与验证,确保能够无障碍地正常运行。 1.项目适用场景:本项目特别适用于计算机领域的毕业设计课题、课程作业等场合。对于计算机科学与技术等相关专业的学生而言,这些项目无疑是一个绝佳的选择,既能满足学术要求,又能锻炼实际操作能力。 2.超值福利:所有定价为9.9元的项目,均包含完整的SQL文件。如需远程部署可随时联系我,我将竭诚为您提供满意的服务。在此,也想对一直以来支持我的朋友们表示由衷的感谢,你们的支持是我不断前行的动力! 3.求关注:如果觉得我的项目对你有帮助,请别忘了点个关注哦!你的支持对我意义重大,也是我持续分享优质资源的动力源泉。再次感谢大家的支持与厚爱! 4.资源详情:https://blog.csdn.net/2301_78888169/article/details/144929660 更多关于项目的详细信息与精彩内容,请访问我的CSDN博客!
2024年AI代码平台及产品发展简报-V11
蓝桥杯算法学习冲刺(主要以题目为主)
QPSK调制解调技术研究与FPGA实现:详细实验文档的探索与实践,基于FPGA实现的QPSK调制解调技术:实验文档详细解读与验证,QPSK调制解调 FPGA设计,有详细实验文档 ,QPSK调制解调; FPGA设计; 详细实验文档,基于QPSK调制的FPGA设计与实验文档
PID、ADRC和MPC轨迹跟踪控制器在Matlab 2018与Carsim 8中的Simulink仿真研究,PID、ADRC与MPC轨迹跟踪控制器在Matlab 2018与Carsim 8中的仿真研究,PID, ADRC和MPC轨迹跟踪控制器Simulink仿真模型。 MPC用于跟踪轨迹 ADRC用于跟踪理想横摆角 PID用于跟踪轨迹 轨迹工况有双移线,避障轨迹,正弦轨迹多种 matlab版本为2018,carsim版本为8 ,PID; ADRC; MPC; 轨迹跟踪控制器; Simulink仿真模型; 双移线; 避障轨迹; 正弦轨迹; MATLAB 2018; CarSim 8,基于Simulink的PID、ADRC与MPC轨迹跟踪控制器仿真模型研究
重点:所有项目均附赠详尽的SQL文件,这一细节的处理,让我们的项目相比其他博主的作品,严谨性提升了不止一个量级!更重要的是,所有项目源码均经过我亲自的严格测试与验证,确保能够无障碍地正常运行。 1.项目适用场景:本项目特别适用于计算机领域的毕业设计课题、课程作业等场合。对于计算机科学与技术等相关专业的学生而言,这些项目无疑是一个绝佳的选择,既能满足学术要求,又能锻炼实际操作能力。 2.超值福利:所有定价为9.9元的项目,均包含完整的SQL文件。如需远程部署可随时联系我,我将竭诚为您提供满意的服务。在此,也想对一直以来支持我的朋友们表示由衷的感谢,你们的支持是我不断前行的动力! 3.求关注:如果觉得我的项目对你有帮助,请别忘了点个关注哦!你的支持对我意义重大,也是我持续分享优质资源的动力源泉。再次感谢大家的支持与厚爱! 4.资源详情:https://blog.csdn.net/2301_78888169/article/details/144486173 更多关于项目的详细信息与精彩内容,请访问我的CSDN博客!
内容概要:本文档详细介绍了一个利用Matlab实现Transformer-Adaboost结合的时间序列预测项目实例。项目涵盖Transformer架构的时间序列特征提取与建模,Adaboost集成方法用于增强预测性能,以及详细的模型设计思路、训练、评估过程和最终的GUI可视化。整个项目强调数据预处理、窗口化操作、模型训练及其优化(包括正则化、早停等手段)、模型融合策略和技术部署,如GPU加速等,并展示了通过多个评估指标衡量预测效果。此外,还提出了未来的改进建议和发展方向,涵盖了多层次集成学习、智能决策支持、自动化超参数调整等多个方面。最后部分阐述了在金融预测、销售数据预测等领域中的广泛应用可能性。 适合人群:具有一定编程经验的研发人员,尤其对时间序列预测感兴趣的研究者和技术从业者。 使用场景及目标:该项目适用于需要进行高质量时间序列预测的企业或机构,比如金融机构、能源供应商和服务商、电子商务公司。目标包括但不限于金融市场的波动性预测、电力负荷预估和库存管理。该系统可以部署到各类平台,如Linux服务器集群或云计算环境,为用户提供实时准确的预测服务,并支持扩展以满足更高频率的数据吞吐量需求。 其他说明:此文档不仅包含了丰富的理论分析,还有大量实用的操作指南,从项目构思到具体的代码片段都有详细记录,使用户能够轻松复制并改进这一时间序列预测方案。文中提供的完整代码和详细的注释有助于加速学习进程,并激发更多创新想法。
液滴穿越障碍:从文献到案例的复现研究,液滴破裂与障碍物穿越:文献复现案例研究,液滴生成并通过障碍物破裂。 该案例是文献复现,文献与案例一起。 ,液滴生成; 障碍物破裂; 文献复现; 案例研究,液滴破裂:障碍挑战的文献复现案例
蓝桥杯算法学习冲刺(主要以题目为主)
蓝桥杯算法学习冲刺(主要以题目为主)
基于最小递归二乘法的MPC自适应轨迹跟踪控制优化 针对轮胎刚度时变特性提升模型精度与鲁棒性,仿真验证满足车辆低速高精度跟踪与高速稳定性提升。,基于变预测时域MPC自适应轨迹跟踪控制与轮胎侧偏刚度优化提升模型精度和鲁棒性,基于变预测时域的MPC自适应轨迹跟踪控制,针对轮胎刚度时变的特点造成控制模型精度降低,基于最小递归二乘法(RLS)估算的轮胎侧偏刚度,提升了模型的控制精度和鲁棒性,通过carsim与simulink联合仿真结果发现,改进后的轨迹跟踪控制器既满足了车辆低速行驶下的轨 迹跟踪精度,也一定程度上克服了高速下车辆容易失去稳定性的问题。 有详细的lunwen分析说明和资料,以及本人的,仿真包运行。 ,基于变预测时域的MPC; 自适应轨迹跟踪控制; 轮胎刚度时变; 控制模型精度降低; 最小递归二乘法(RLS)估算; 模型控制精度和鲁棒性提升; carsim与simulink联合仿真; 轨迹跟踪控制器; 车辆稳定性。,基于变预测时域MPC的轮胎刚度自适应轨迹跟踪控制策略研究
GMSK调制解调技术研究:基于FPGA设计与实验详解,GMSK调制解调技术详解:基于FPGA设计的实验文档与实践应用,GMSK调制解调 FPGA设计,有详细实验文档 ,GMSK调制解调; FPGA设计; 详细实验文档; 实验结果分析,GMSK调制解调技术:FPGA设计与实验详解
# 基于Arduino和Python的Cansat卫星系统 ## 项目简介 本项目是一个Cansat卫星系统,旨在设计和实现一个小型卫星模型,通过火箭发射至1公里高空,并使用地面站接收其传输的数据。项目涉及Arduino编程、Python数据处理和可视化。 ## 主要特性和功能 1. 硬件组件 使用Arduino Nano作为Cansat的微控制器。 搭载BMP 280温度和压力传感器、ATGM336H GPS模块、LoRa通信模块等。 地面站使用Arduino Uno和LoRa通信模块接收数据。 2. 数据处理 使用Python进行数据处理和可视化,包括数据清洗、计算风速、绘制温度、压力、风速和海拔随时间变化的图表等。 3. 通信与控制 通过LoRa模块实现Cansat与地面站之间的数据传输。 提供实时监视和记录数据的脚本。 ## 安装和使用步骤 ### 1. 硬件准备
U9300C 龙尚4G模块安装后模块才能正常使用,win7 win10驱动程序,支持USB转接板。
# 基于Arduino平台的物联网温湿度监控系统 ## 项目简介 这是一个基于Arduino平台的物联网温湿度监控项目,旨在通过简单的硬件设备实现环境数据的实时监测与远程管理。该项目适用于智能家居、农业种植等领域。 ## 项目的主要特性和功能 1. 温湿度数据采集通过Arduino板连接温湿度传感器,实时采集环境数据。 2. 数据传输将采集到的数据通过无线网络模块发送到服务器或远程终端。 3. 数据可视化可在电脑或移动设备端展示实时的温湿度数据。 4. 报警功能当温湿度数据超过预设阈值时,自动触发报警通知。 ## 安装使用步骤 前提假设用户已经下载了本项目的源码文件。以下是简单明了的安装使用步骤 1. 环境准备安装Arduino开发环境,配置必要的硬件接口。 2. 硬件连接将Arduino板与温湿度传感器、无线网络模块连接。 3. 代码上传将本项目提供的Arduino代码上传至Arduino板。
基于需求响应与清洁能源接入的配电网重构优化:综合成本与混合整数凸规划模型分析(matlab实现),基于需求响应与清洁能源接入的配电网重构算法研究:网损与成本优化的仿真分析,高比例清洁能源接入下计及需求响应的配电网重构(matlab代码) 该程序复现《高比例清洁能源接入下计及需求响应的配电网重构》,以考虑网损成本、弃风弃光成本和开关操作惩罚成本的综合成本最小为目标,针对配电网重构模型的非凸性,引入中间变量并对其进行二阶锥松弛,构建混合整数凸规划模型,采用改进的 IEEE33 节点配电网进行算例仿真,分析了需求响应措施和清洁能源渗透率对配电网重构结果的影响。 该程序复现效果和出图较好(详见程序结果部分),注释清楚,方便学习 ,高比例清洁能源; 需求响应; 配电网重构; 二阶锥松弛; 综合成本最小化; MATLAB代码; IEEE33节点配电网; 复现效果; 出图; 注释清楚。,Matlab代码复现:高比例清洁能源接入下的配电网重构模型与需求响应分析
# 基于C++的RapidJSON库测试项目 ## 项目简介 本项目是一个基于C++的RapidJSON库测试项目,主要用于测试RapidJSON库的功能正确性、性能以及稳定性。RapidJSON是一个高效的C++ JSON解析生成库,广泛应用于各种场景。本项目通过编写一系列的单元测试,覆盖了RapidJSON库的主要功能点,包括JSON解析、生成、内存管理、编码转换等,以确保RapidJSON库在各种情况下都能正确、稳定地工作。 ## 项目的主要特性和功能 1. 单元测试框架使用Google Test测试框架进行单元测试,确保测试的可靠性和可扩展性。 2. 全面测试覆盖覆盖了RapidJSON库的主要功能点,包括JSON解析、生成、内存管理、编码转换等,以及针对各种输入数据的测试。 3. 性能测试通过性能基准测试,评估RapidJSON库在处理不同规模和类型的JSON数据时的性能表现。
蓝桥杯算法学习冲刺(主要以题目为主)