[Experience] 8 years of long web socket journey

Source: Internet
Author: User

Eight years ago, I was a newbie who just stepped into the Internet. I remember that it was still an Internet Explorer 5 era. Win98's classic style and friendly blue screen are still fresh in our memory. But with curiosity about this unknown world, I soon learned the beginner's favorite open-door language-VB and HTML. Along with the familiar feeling of VB, VBScript was inadvertently used up. At the time that IE took over the world, VBScript was still a popular web script language, while JavaScript, like the strange Java style, made me feel awkward.

 

At that time, the website was relatively simple. the foreground of the table layout, with the help of ASP and access, was a typical website. Web scripts are also simple verification forms, or some webpage special effects, such as the mouse. However, unlike everyone, my webpage is purely for script writing. The origin was very simple. At that time, I was planning to use VB for a mini-game of mine clearance. Although the game code is simple, it is difficult to draw and manage the game interface. After careful consideration, isn't the main interface of mine clearance A nxn table? If it is changed to VBScript, it can be done in just a few lines. It is not only easy to generate, but also requires only a few characters to change the table size, border size, and other styles. This is the first time that we have experienced the power of web scripts --Using a simple method to create a complex interface, you can spend more energy on the content instead of implementation.

 

With the familiarity with the script, I plan to make something interactive, rather than a simple lattice game. I have previously seen ASP online chat rooms on the Internet. Although I am not doing well in work, I envy this idea and hope I can do it more perfectly. Because ASP is used in this chat room, it is obviously subject to various constraints. His principle is actually very simple. every 10 seconds, self-Refresh gives people a feeling of instability, and the message is either not sent for 10 seconds, there is no instant feeling. At that time, I couldn't think of a better way to limit the technology at that time until one day in July...

I am very excited by the arrival of WinSock. Although VB has been used for more than half a year, all previous items were stand-alone and there was no external connection. Now we can use the network as part of the program. Every network program is excited. The post refresh device, port scanner, simple multi-chat room, and UDP Bomb (in the qq2003 version, the friend computer can be stuck )... I learned about IP, TCP, port, route... After the basic principles of the network, we gradually began to consider the previous problem.

 

Because ASP is based on the HTTP protocol, it requests it to respond immediately. If you want the chat room to ensure the instant messaging, the web page must constantly access it. If there are more than one person, the server certainly cannot withstand it. The multi-user chat room created with VB is so simple that a Winsock serves as a server, and other most clients can connect to it with just a few pieces of code. So I came up with an idea:Create a Winsock for VBScript, Communicate with my server!

 

This idea is meaningful. Once successful, the page will no longer need to be refreshed. You only need to pass data in the background using scripts, after receiving the data, a script is used to write the message into the DIV of the information box. (at that time, you do not know the concept of Dom creation, but you can set innerhtml to modify the existing content ). I started my attempt with confidence. Since VB can use Winsock as a control, the <Object> label can also be used in the webpage to generate Winsock or create ActiveX objects. However, it was a pity that I tried a variety of methods, searched the Google website, and did not use Winsock on the webpage. Maybe this idea is too naive. After trying for a few weeks, I planned to give up. Instead of giving up this idea, I used the Winsock method.

 

After some research, we decided to use a curve to save the country: simulate a Winsock. Data transmission in Winsock is performed in the background, which is invisible to users. It is definitely not feasible to refresh the homepage on a webpage. However, we can refresh the framework on the page without refreshing the homepage, and make the framework small enough, it can even be hidden, and the framework and the home page can be called by scripts, so that real-time interaction can be achieved without Winsock?

I created a simple HTTP server using VB and bound it to port 8080. The client web allows the framework on the page to access 127.0.0.1: 8080/+ chat parameters through VBScript; the server program received the HTTP header and subsequent chat information, so it returned <script language = VBScript> window. parent. callback "hello" </SCRIPT>: I can't remember how the code was at the time. It is called a function on the parent page of the Framework. At this time, the msgbox box in the page jumps out of hello.

 

Because VB is based on message events, Winsock does not need to reply immediately after receiving the data. You can cache the data in the timer and wait for the accumulated data or wait for a certain amount of time before replying. This is essentially a world different from the traditional HTTP protocol, which makes this instant chat room possible.

 

After some sorting, the chat room has the following event mechanism:

1. script to modify the framework path --> 2. data is transmitted to my server through the path value --> 3. server processing data --> 4. server timer distribution data --> 5. client script receives callback data --> 6. script analysis and display data --> 1 .....

 

The page and server are in such a round of message loops. Messages can be synchronized to all users in a short period of time, almost instantly implemented.

 

In the subsequent study, it was found that the use of the framework to transmit data is completely redundant. In fact, you only need to change the SRC value of a script element to achieve this effect. The data returned by the server can be much simpler. Only the format of X "..." is enough. X is the callback function name on the page.

 

After packaging on the interface, this chat room is a success. We cannot implement socket, but we can simulate it. Of course, the disadvantage is also obvious. From time to time, a script file is loaded on the page, and the mouse flashes into an hourglass cursor from time to time. The browser status is always loading, the user can still feel the data transmission. Of course, I also tried my best to make improvements.

 

----------

One day in 163, when surfing the Internet, I accidentally found that many websites do not have page switching. For example, of mailboxes are in a single page mode. At that time, I had not studied web for a long time. When I open the source code of the webpage, I used to use an XMLHTTP Control. Is it rare that this is the legendary Winsock? Start the test now. However, the results are disappointing. Even though the user cannot feel the data transmission, he has a fatal drawback: He can only access the URL of the same port of the same domain name, isn't this a violation of the original separation of resource services and data services? This is still not what I want, although most websites now use this plug-in.

 

----------

In, when learning flash, he found that he had already supported socket. Since flash is already a control on most computers, why not use it? I wrote a socket with only basic functions. Its SWF is only over 300 bytes for JS calls. JS is encapsulated into a socket class, which can be connected, sent, and disconnected... At this time, a socket was born, but after some use, it was discovered that he was not a real socket-he was still limited by the Sandbox Model, hosts that want to access external domains must also have cross-Origin Services. However, in any case, the wishes of those years have been fulfilled.

 

----------

In, as3.0 was in full swing. The socket not only sends text, but also binary data streams. At this time, Flash is far from the control that played the vector animation. Similar to Java's powerful packages, efficient computing speed, complex image processing, and almost all Ria. Similar to the emergence of flash in the original instant chat room, the results have a good experience. There seems to be no need for a separate web-based socket.

 

----------

Today, we upgraded opera to version 11. The updated content contains a websocket that supports HTML5.

 

Previously, only WebKit kernel browsers that support websocket are now available. HTML5 is becoming more and more standardized, and more useful functions and features have been added to HTML5. It seems that a wave of pure web craze is coming soon. When HTML5 can be perfected by all browsers, I may start writing socket scripts again to create more exciting interactions and games. However, the original message model was designed purely for fun, but now it has become the mainstream of Web, which I did not think... )

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.