Use Web Socket technology to connect the client to the server (using Node. js as the server)

Source: Internet
Author: User

After almost one working day, I found a lot of materials, learned and debugged, and finally used the web socket method in node. as a server, js enables interaction between the client and the server. The learning process is like this. After suffering, you will be able to live ~~

Although the web socket technology has already come out, someone has been studying it for about 10 years abroad. In China, I think it may be earlier in 11 years, I didn't see any relevant materials.) But I did know such a thing during the past few days because of my work, so I was prepared to study it. You need to know that I have been studying java script for only a few days. When I write this article, I am still learning what is json, oh, my god! Okay, I used to do C ++ ).

In fact, I don't really want to let everyone go through my pain, so I am sending out the most primitive problems that people may encounter.

If you have installed node. after js, and according to most of the materials on the Internet about web socket technology, after using js to write both the server side and the client side, I believe that everyone should be easier to master than me. After all, I just got in touch with js ), in fact, you can directly run node xxx in the command line. the js program is OK, and then you use your client to connect to this server address. If the following error occurs: TypeError: Object # <WriteStream> has no method 'flush ', the flush method cannot be found. We found that process. stdou. flush (); it is not difficult to find that this is something similar to a print statement, so // drop it, the result is more confusing, the most important thing is that it has never been successfully connected, so I don't know what it will look like after the success.

This problem may be caused by a third-party web socket library. The download address may be:

Https://github.com/remy/html5demos/tree/master/server/ this web site is really harmful, read the post from the Internet said the reason is that this library is not applicable to node. after js xx. js is constantly updated, and this library is no longer followed. This is a bit difficult! Therefore, you can download the latest third-party library on this website:

Https://github.com/Worlize/WebSocket-Node is a try, good stuff. Then all the things are solved. You can check the Code with peace of mind and then modify it based on it.

After downloading and unzipping, run the echo-server.js under the WebSocket-Node-master \ test directory, and then directly connect http: // localhost: 8080 with the client you write, this 8080 is defined in the echo-server.js file. Then you can connect it. It's so exciting!

I hope that people who have studied web socket can communicate with each other and learn from each other.


Here are some good articles found during the learning process.

1-http: // ued.sina.com.cn /? P = 900 # comment-7448

2-http: // www.html5china.com/HTML5features/WebSocket/20110926_2044.html


This article is from the "typical lion man" blog, please be sure to keep this source http://zhouhongyu1989.blog.51cto.com/2931598/1290088

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.