it has already been routed. Other ways to get the identifier would be to send the pathname segment from the client side or to initially serve it using a res.local (variables scoped to the request/response cycle) and exposing as a global variable on the client side. Using socket.handshake is quite long but in this case it is a nice way to avoid sending from the client side.
With the identifier stored in a variable called room, we can now use socket.join(room); to join a Socket.IO room with the same name as the ID. These rooms are basically a way to send the right messages to the right people, only members of the room will get the messages for that particular room. To check for messages or when the user leaves the room, we use event handlers on socket. Every communication over Socket.IO is classed as a message, not to be confused with the messages that are written in the chatroom. Sending messages is called emitting. For messages in the chatroom to be sent to each other, we first emit the message on the client side to the server, which then sends it to the other clients connected to the room.
To emit a message to the room from the server we use io.sockets.in(room).emit(key, value);. For a chatroom message we use the string 'message' as the key and the data is passed through as the value from a data parameter on the event listener. For leaving a room, the key is 'leave'. And that’s it for the server in terms of a basic chatroom, you can see how brief the code is in Listing 7-1. All we need to do now is for the event handlers on the client-side to emit the messages when sent so that, using the power of Socket.IO rooms and the code we have just written, it will automatically send the messages to anybody in the same
room— in real time! There is no AJAX or long polling to worry about, Socket.IO uses the best method available (most modern browsers use Web Sockets but others fall back to the appropriate method). The lack of using AJAX is one of the biggest factors in my choice to not use jQuery or any similar libraries for this chapter.
B.原文的翻译实时协作
在书中到目前为止,我们所写的代码已经被客户端,虽然客户端JavaScript是非常强大的,现在常常需要有一个服务器,数据可以在线存储,逻辑从客户端保持敏感,或者这样用户可以在网上彼此交互。你可能熟悉其他服务器端语言,如PHP和Ruby(甚至可能Perl !),但节点的优势之一:js是你写服务器端在JavaScript代码,所以应该很容易因为你有客户端JS的经验去做一个聊天室。论文网
在这一章里,我们使用节点。js和套接字。IO创建一个实时聊天室web应用程序,允许多个用户彼此交谈。我指导你通过设置节点。js在本地机器上,因为最常用的服务器(共享主机)不支持节点。js,但是请注意,一些如Heroku和Amazon Web服务服务有免费的层。安装node . js显然使用节点写任何东西。js,你需要安装它。有很多方法可以安装它。你可以使用包管理器,如apt-get百胜,或者自制程序(取决于您使用的操作系统)但这些并不总是最新的。在节点上。js的网站,www.nodejs.org,您可以下载最新的版本可执行的二进制文件或从源代码构建它。或者,您可以使用节点版本经理(nvm)是特别有用如果你正在与许多并非所有的代码库使用最新的版本。节点有一个内置的包管理器称为npm,使用处理依赖关系文件包。json,而不是安装表达和套接字。IO自己,你只是把它放在该文件。当你准备安装包,运行npm package.json安装文件夹内。与传统的JavaScript,节点。js有一个模块系统,允许您在运行时加载JavaScript文件。每个模块都定义在一个文件中,通过一个模块输出变量和函数。出口对象。在另一个文件,您可以使用“要求”函数加载模块和模块。出口的内容。有三个不同的方法来加载模块。
- 上一篇:ASP英文参考文献翻译
- 下一篇:学生信息管理系统英文文献和中文翻译
-
-
-
-
-
-
-
当代大学生慈善意识研究+文献综述
java+mysql车辆管理系统的设计+源代码
十二层带中心支撑钢结构...
电站锅炉暖风器设计任务书
河岸冲刷和泥沙淤积的监测国内外研究现状
杂拟谷盗体内共生菌沃尔...
酸性水汽提装置总汽提塔设计+CAD图纸
中考体育项目与体育教学合理结合的研究
乳业同业并购式全产业链...
大众媒体对公共政策制定的影响