1. What is the WebSocket protocol
The RFC6455 document is described as follows:
The WebSocket Protocol enables two-way communication between a client running untrusted code in a controlled environment T o A remote host, that have opted-in to communications from the that code. The security model used for the origin-based security model commonly used by Web browsers. The protocol consists of a opening handshake followed by basic message framing, layered over TCP. The goal of this technology are to provide a mechanism for browser-based applications that need two-way communication with Servers that does not rely on opening multiple HTTP connections.
The main idea is that WebSocket is a full-duplex application-layer protocol based on the TCP protocol, which is mainly used in Web browsers, so that Web applications that are browser-based and require full-duplex communication are no longer dependent on multiple HTTP connections.
2. Application WebSocket
Imagine such a scenario, an application based on B/s architecture, its function is the server actively to the browser to send a message periodically, what should be done? Because the HTTP protocol can only be initiated by the client request, server-side response requests to establish a connection, it is difficult to implement the server unsolicited message via the HTTP protocol, the browser client can be timed to send HTTP requests to the server to achieve, comet is based on this way, In fact, this is not really a "server initiative". However, relying on websocket, we can easily do this.
Now the WebSocket support is as follows:
1. Server-side
- IIS 7.0+
- Tomcat 7.0.5+
- Jetty t.0+
- WebLogic 12c
- WebSphere 8.0+
2. Browser-side
- Chrome 4+
- FireFox 5+
- IE + +
- Safari IOS 5+
- Android Browser Android 4.5+
Below we will implement a simple Java WebApp using WebSocket, which is the server that proactively sends three messages to the browser. The server is Tomcat 8.5.4, in addition to introducing a jar package that supports websocket---websocket-api.jar for our app. If you want to observe the effect, please go to http:/ 139.129.95.147/testwebsocket/.
The code is as follows:
Front:
1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <title>Testing WebSockets</title>5 </Head>6 <Body>7 <Div>8 <inputtype= "Submit"value= "Start"onclick= "Start ()" />9 </Div>Ten <DivID= "Messages"></Div> One <Scripttype= "Text/javascript"> A varWebSocket= - NewWebSocket ('Ws://139.129.95.147/testwebsocket/websocket'); - Websocket.onerror= function(event) { the OnError (Event) - }; - Websocket.onopen= function(event) { - OnOpen (Event) + }; - Websocket.onmessage= function(event) { + OnMessage (Event) A }; at functionOnMessage (event) { - document.getElementById ('Messages'). InnerHTML - += '<br/>' +Event.data; - } - functionOnOpen (event) { - document.getElementById ('Messages'). InnerHTML in = 'Connection established'; - } to functionOnError (event) { + alert (event.data); - } the functionstart () { * Websocket.send ('Hello'); $ return false;Panax Notoginseng } - </Script> the </Body> + </HTML>
index.html
Back end:
1 Importjava.io.IOException;2 ImportJavax.websocket.OnClose;3 ImportJavax.websocket.OnMessage;4 ImportJavax.websocket.OnOpen;5 Importjavax.websocket.Session;6 ImportJavax.websocket.server.ServerEndpoint;7@ServerEndpoint ("/websocket")8 Public classTestwebsocket {9 @OnMessageTen Public voidonMessage (String message, session session) One throwsIOException, interruptedexception { A - //Print The client message for testing purposes -System.out.println ("Received:" +message); the - //Send The first message to the client -Session.getbasicremote (). SendText ("This is the first server message"); - + //Send 3 messages to the client every 5 seconds - intSentmessages = 0; + while(Sentmessages < 3){ AThread.Sleep (5000); at session.getbasicremote (). -SendText ("This was an intermediate server message. Count: " -+sentmessages); -sentmessages++; - } - in //Send A final message to the client -Session.getbasicremote (). SendText ("The Last Server message"); to } + - @OnOpen the Public voidOnOpen () { *System.out.println ("Client Connected"); $ }Panax Notoginseng @OnClose - Public voidOnClose () { theSystem.out.println ("Connection closed"); + } A}
View Code
Using WebSocket in Tomcat, you first need to establish a endpoint on the server side, with the syntax
@ServerEndpoint ("/websocket")
Then get a WebSocket object from the front end based on the endpoint URL, and then call its related methods. Because the code is relatively simple, in this article does not repeat, I will in the subsequent article detailed analysis.
3.WebSocket and TCP, HTTP relationships
WebSocket is a standalone , TCP protocol-based application layer protocol, and the only relationship with the HTTP protocol is that the WebSocket protocol handshake is implemented by the HTTP server. and the HTTP server treats it as an upgrade version of the HTTP protocol.
Initial knowledge of WebSocket protocol