First knowledge of WebSocket protocol, first knowledge of websocket

Source: Internet
Author: User

First knowledge of WebSocket protocol, first knowledge of websocket
1. What is the WebSocket protocol?

RFC6455 is described as follows:

The WebSocket Protocol enables two-way communication between a client running untrusted code in a controlled environment to a remote host that has opted-in to communications from that code. The security model used for this is the origin-based security model commonly used by web browsers. The protocol consists of an opening handshake followed by basic message framing, layered over TCP. The goal of this technology is to provide a mechanism for browser-based applications that need two-way communication with servers that does not rely on opening multiple HTTP connections.

WebSocket is a TCP-based full-duplex application layer protocol. It is mainly used in Web browsers, the purpose is to make web applications based on browsers and requiring full-duplex communication no longer rely on multiple HTTP connections.

2. Apply WebSocket

Suppose that an application based on B/S architecture has the function of serverActiveWhat should I do if I regularly send a message to the browser? As the HTTP protocol can only be used by clients to initiate requests and the server establishes a connection to respond to requests, it is difficult for the server to actively push messages through the HTTP protocol, you can use the browser client to regularly send HTTP requests to the server. Comet is based on this method. In fact, this is not really a "server Initiative ". However, relying on WebSocket, we can easily achieve this.

Currently, WebSocket supports the following:

1. Server Side

2. Browser Side

Below we will use WebSocket to implement a simple java webapp. Its function is that the server actively sends three messages to the browser. The server is Tomcat 8.5.4, in addition to this, to introduce our app to support WebSocket jar package --- websocket-api.jar. To observe the effect, go to http: // 139.129.95.147/TestWebSocket /.

The Code is as follows:

Front end:

1 <! DOCTYPE html> 2 Backend:

1 import java. io. IOException; 2 import javax. websocket. onClose; 3 import javax. websocket. onMessage; 4 import javax. websocket. onOpen; 5 import javax. websocket. session; 6 import javax. websocket. server. serverEndpoint; 7 @ ServerEndpoint ("/websocket") 8 public class TestWebSocket {9 @ OnMessage10 public void onMessage (String message, Session session) 11 throws IOException, interruptedException {12 13 // Print the client message for testing purposes14 System. out. println ("Received:" + message); 15 16 // Send the first message to the client17 session. getBasicRemote (). sendText ("This is the first server message"); 18 19 // Send 3 messages to the client every 5 seconds20 int sentMessages = 0; 21 while (sentMessages <3) {22 Thread. sleep (5000); 23 session. getBasicRemote (). 24 sendText ("This is an intermediate server message. count: "25 + sentMessages); 26 sentMessages ++; 27} 28 29 // Send a final message to the client30 session. getBasicRemote (). sendText ("This is the last server message"); 31} 32 33 @ OnOpen34 public void onOpen () {35 System. out. println ("Client connected"); 36} 37 @ OnClose38 public void onClose () {39 System. out. println ("Connection closed"); 40} 41}View Code

To use WebSocket in Tomcat, you must first create an endpoint on the server. The syntax is

@ServerEndpoint("/websocket")

Then obtain a WebSocket object based on the endpoint url at the front end, and then call the relevant method. Because the code is relatively simple, I will not repeat it in this article. I will analyze it in detail in subsequent articles.

3. Relationship between WebSocket and TCP and HTTP

WebSocket isIndependentTCP-based application layer protocol. The unique relationship between TCP and HTTP is that the process of establishing a connection through a WebSocket handshake is implemented by the HTTP server, the HTTP server regards it as an upgraded version of the HTTP protocol.

 

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.