WebSocket development based on embedded jetty server
WebSocket provides a full-duplex communication (Full-duplex) between the browser and the server, replacing the traditional polling (polling) practice. At present, in some web-base Rich text editor has a wide range of applications, such as Etherpad, graphite documents, Youdao cloud notes and so on. For a specific definition of websocket, see:http://baike.baidu.com/view/3623887.htmAndHttps://en.wikipedia.org/wiki/WebSocket。 The following is an example of a websocket development based on the embedded jetty server for your reference.
Basic Architecture:
Browser(Javasscript)--(WebSocket with WS/WSS)--
Nginx--(WebSocket with WS/WSS)--
Embedded Jetty Server
Service-Side development:For an introduction to jetty server, seehttp://www.eclipse.org/jetty/。 Here are some practical usage experiences and techniques, including: establishing a server-side link listener (including WS and WSS), creating a message handling handle (which includes Websocketservlet and Websocketadapter)
- Building a server-side websocket link
This example uses Serverconnector to establish a server-side link listener. The code example is as follows: httpconfiguration http_config = new Httpconfiguration (); Serverconnector http = new Serverconnector (Websocketserver, New Httpconnectionfactory (Http_config)); Http.setHost ( ServerName); Http.setport (ServerPort); Websocketserver.setconnectors (new connector[]{http});
The above code is based on WS (equivalent to HTTP), if you need to establish a WSS link (equivalent to HTTPS), you need to set KeyStore, the code example is as follows: httpconfiguration https_config = new Httpconfiguration (); Https_config.setsecurescheme ("https"); Https_config.setsecureport (securityport); https_ Config.setsendserverversion (True); Https_config.setsenddateheader (false); Https_config.addcustomizer (new Securerequestcustomizer ());
Sslcontextfactory sslcontextfactory = new Sslcontextfactory (); Sslcontextfactory.setkeystorepath (". \\ssl\\keystore" ); Sslcontextfactory.settruststorepath (". \\ssl\\keystore");//Key store password when generate the Storesslcontextfactory.setkeystorepassword ("Testpassword"); Sslcontextfactory.setexcludeciphersuites ("SSL_RSA_ With_des_cbc_sha "," Ssl_dhe_rsa_with_des_cbc_sha "," Ssl_dhe_dss_with_des_cbc_sha "," ssl_rsa_export_with_rc4_40_ MD5 "," Ssl_rsa_export_with_des40_cbc_sha "," Ssl_dhe_rsa_export_with_des40_cbc_sha "," ssl_dhe_dss_export_with_ Des40_cbc_sha "); Serverconnector sslconnector = new Serverconnector (websocketserver,new sslconnectionfactory (SslContextFactory, HttpVersion.HTTP_1_1.asString ()), New Httpconnectionfactory (Https_config)); Sslconnector.setport (Securityport); Websocketserver.setconnectors (New Connector[]{sslconnector});
- To establish a client message handling handle
This example uses Servletcontexthandler to process messages originating from the client (JavaScript). Note: At the same time introduce contexthandlercollection, add a healthstatus handle, to monitor jettyserver running state. The sample code is as follows://ADD health Contexthandlercontexthandler Healthhandler = Createhealthstatushandler ();
Add websocket Contexthandlerservletcontexthandler websockethandler = new Servletcontexthandler ( servletcontexthandler.sessions); Websockethandler.setcontextpath ("/"); Servletholder Wsholder = new Servletholder ("Collabwebsocket", New Testcollabsocketservlet ()); Websockethandler.addservlet (Wsholder, "/");
Merge contexthandlerscontexthandlercollection handlers = new Contexthandlercollection (); Handlers.sethandlers ( New Handler[]{websockethandler, Healthhandler}); Websocketserver.sethandler (handlers);
- Implement WebSocket Serverlet and adapter:
public class Testcollabsocketservlet extends Websocketservlet {
Public Testcollabsocketservlet () {}
@Overridepublic void Configure (Websocketservletfactory factory) {factory.register (Testcollabsocketadapter.class);}} public class Testcollabsocketadapter extends websocketadapter{public void onwebsocketclose (int statusCode, String Reason) {//handle shutdown event}public void Onwebsocketconnect (Session websocketsession) {//Process link event}public void Onwebsockettext ( String msgtext) {//processing the received message}}
Client (JavaScript development):
- To establish a link to the server:
This URL is similar to Ws://richeditor.com/collab or wss://richeditor.com/collabthat._websocket = new WebSocket (Websocketurl);
- Registering WebSocket events, including Open,close,error and message
That._websocket.onopen = That.onOpen.bind (that), That._websocket.onclose = That.onClose.bind (that); that._ Websocket.onmessage = That.onMessage.bind (that), That._websocket.onerror = That.onError.bind (that); Onopen:function ( {//Handle link Open Event},onclose:function () {//Handle link Close Event},onmessage:function () {//Handle accepted message},onerror:function () {//Handle link failure event},Summary:Overall, this set of code is relatively simple, for the hundred-person level of concurrency can also be handled calmly. Recommendation: 1, for WSS support, only need to implement from the client to nginx this layer, Nginx and jetty Sever basic with the intranet, you can use WS connection. 2, can be compressed to transmit the message, to reduce the dependency on the bandwidth 3, to implement a custom Ping/pong message, to ensure the continued validity of the web socket Long connection (nginx default will be closed for 1 minutes without the message communication link)
WebSocket development based on embedded jetty server