Socket.IO homepage
Socket.IO is also incredibly easy to use. Once it is installed, it needs to be required within the server-side files (similar to requiring a file in PHP, but node automatically finds the location that it is installed) and then set it to listen to the server.
Of course, we have not yet made a server. Languages such as PHP execute once per file and translate the file’s contents into an output, Node.js is different in that it is a long-standing process that will serve many files many times. Node.js includes an HTTP module, although you can use apache or nginx if you want, that gives you the capability of creating a simple server yourself—but for most situations you don’t want to be mucking around with low-level server code.
Express.js makes it easy to get to the actual code-writing part of developing a website, rather than worrying about how each file can access the rest of the file structure and so on. However, it isn’t very opinionated; with Express.js you can use any paradigm (or way of working) that you like. You can write simple websites, blogs, advanced web apps, or a REST API using the tools that Express provides. If you do prefer an opinionated approach, there is an official structuring tool for Express called express-generator that can be installed globally using npm to help you get started on a new project, although it doesn’t force you to keep using the same structure.
As a brief example of using Express to manage the low-level code, see the two code snippets that follow. They both write ‘Hello World!’ to the page if you are on localhost:8080/hello, and they write ‘Goodbye cruel world!’ if you are on any other page. The difference is that the first snippet directly uses the http and the second snippet uses Express. Of course this barely scratches the skin of what Express is capable of, but it should show you nicely how
Express does not require the lower-level code such as HTTP headers. Now let’s use Express to begin the writing the server for our chatroom. At the beginning of the chapter there was a package.json for installing Express and Socket.IO, if you haven’t yet installed those packages, then you will need to do so now. Using Socket.IO is slightly different than using Express on its own in that it requires an http.createServer Currently there is nothing to see, because we didn’t even write ‘Hello World’ to the page this time, so let’s set up a basic page using just HTML. For the most basic chatroom, all we need is two text boxes to put your name and message in (you could do name differently depending on UI choices) as well as a button to send the message and a p (you could use a list to improve semantics) to put the messages in. This file can be (although doesn’t have to be) saved as index.html in the same folder as the server code, as you would normally.
Now you need to use routers (as we did in the Hello World example) to tell the browser where to find content. For this chatroom, we want multiple rooms to be accessible and a nice way to do this is have the name of the chatroom (or a random identifier) in the URL of the room. Express has a way to easily check for this name, as a pathname segment (also known as a named parameter), but because this chatroom does not need multiple views, we can just direct every page to index.html. We could use a wildcard for this instead of a pathname segment, but it is a nice way to introduce the idea of pathnames being used as parameters. It is also a way to force a chatroom id to be included, as it will not serve the page without any id; this means that a separate route would be needed if you want to add a homepage.
Of course, this is a rather useless chatroom. Let’s write the server-side code for dealing with messages. Using Socket.IO we need to check for a connection, using io.sockets.on as an event handler for any connections, then join the room that is named in the pathname segment. Unlike Express, to get the identifier we use var room = socket.handshake.headers.referer as the Express .get call is not available once
- 上一篇:ASP英文参考文献翻译
- 下一篇:学生信息管理系统英文文献和中文翻译
-
-
-
-
-
-
-
当代大学生慈善意识研究+文献综述
java+mysql车辆管理系统的设计+源代码
十二层带中心支撑钢结构...
电站锅炉暖风器设计任务书
河岸冲刷和泥沙淤积的监测国内外研究现状
杂拟谷盗体内共生菌沃尔...
酸性水汽提装置总汽提塔设计+CAD图纸
中考体育项目与体育教学合理结合的研究
乳业同业并购式全产业链...
大众媒体对公共政策制定的影响