As a WebSocket component of HTML5 new features, WEB application development that requires certain real-time performance is still useful. Based on Websocket, web chat programs can be easily developed, various webpage message notifications and push notifications ....,.
I. Overview
As one of the new HTML5 features, WebSocket components are useful in WEB application development with certain real-time requirements. WEB socket is supported in the later versions of IE, Chrome, and FF browsers, the standard Websocket communication is based on RFC6455 to implement handshake and message sending between the server and the client. If you are not familiar with Websocket communication, you can view the RFC documentation. Simply put, you can send an HTTP request to shake hands between the two parties, the stateless HTTP Communication Protocol is further upgraded to a stateful communication protocol, and Websocket also supports sub-protocol options and secure transmission. The standard websocket URL starts with ws. If it is TLS-based, it starts with wss. Based on Websocket, you can easily develop web chat programs and various webpage message notifications and push notifications.
If you have to dig a piece of Websocket, remember the earliest way to implement instant webpage Communication Based on HTTP polling, this method is resource-consuming, so someone has improved the programming CometD persistent connection method, but in essence it is still not changing, and the emergence of websocket just solves these problems, however, the earlier versions of many browsers still do not support websocket. Therefore, some JS communication frameworks based on the websocket concept have been developed. Among them, SockJS and socket have been compared. io, they all claim to support websocket, and if the browser does not support native websocket, they will automatically enable the fallback option to use other mechanisms, such as ajax, Http polling, long polling/connection, and even flash socket, to simulate websocket working methods, but their biggest drawback is that if the client uses these frameworks, the server must use them. Otherwise, waiting for developers is a lot of unavoidable problems, and many of them are unsolved. The main reason is that they implement their own protocol sets and cannot process data without their format. There are a lot of gossip.
II. Implementation steps
In the later version of Tomcat 7, the RFC6455 standard protocol on the Websocket server can be implemented to communicate with websocket on the browser. The following steps must be done first:
1. Install JDK-JDK 8 with a higher version.
2. install Tomcat 7.0.64
3. Create a dynamic web project in eclipse
According to the JSR standard, the standard interface for implementing websocket in Java can be implemented based on the annotation method, and tomcat is also well done. Only by implementing the following code can we create a websocket echo server:
package com.websocket.demo; import java.io.IOException; import java.nio.ByteBuffer; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.server.ServerEndpoint; @ServerEndpoint(value = "/echo") public class EchoExample { @OnMessage public void echoTextMessage(Session session, String msg, boolean last) { try { if (session.isOpen()) { System.out.println("received from client message = " + msg); session.getBasicRemote().sendText(msg, last); } } catch (IOException e) { try { session.close(); } catch (IOException e1) { } } } @OnOpen public void openConn(Session session) throws IOException { session.getBasicRemote().sendText("hello web socket"); // means open it } @OnMessage public void echoBinaryMessage(Session session, ByteBuffer bb, boolean last) { System.out.println("send binary message..."); try { if (session.isOpen()) { System.out.println("byte buffer lenghth : " + bb.array().length); System.out.println("byte buffer content: " + ((bb.array()[0]) & 0xff)); System.out.println("byte buffer content: " + ((bb.array()[1]) & 0xff)); System.out.println("byte buffer content: " + ((bb.array()[2]) & 0xff)); session.getBasicRemote().sendBinary(bb, last); } } catch (IOException e) { try { session.close(); } catch (IOException e1) { // Ignore } } } }
To start the websocket server in tomcat, add the following configuration in web. xml:
[listener] [listener-class]org.apache.tomcat.websocket.server.WsContextListener[/listener-class] [/listener]
Then implement the ServerApplicationConfig interface as follows:
Create a Web page echo.html with the following content:
[html] [head] [title>Web Socket Echo Test[/title] [script] var ws = null; var count = 0; function setConnected(connected) { document.getElementById('connect').disabled = connected; document.getElementById('disconnect').disabled = !connected; document.getElementById('echo').disabled = !connected; } function connect() { var target = document.getElementById('target').value; if (target == '') { alert('Please select server side connection implementation.'); return; } if ('WebSocket' in window) { ws = new WebSocket(target); } else if ('MozWebSocket' in window) { ws = new MozWebSocket(target); } else { alert('WebSocket is not supported by this browser.'); return; } ws.onopen = function () { setConnected(true); log('Info: WebSocket connection opened.'); }; ws.onmessage = function (event) { log('Received: ' + event.data);
The above is a detailed description of running the HTML5 WebSocket echo instance based on Tomcat. For more information, see PHP Chinese Network (www.php1.cn )!