I've been thinking about a problem lately: How does a server proactively send messages to a browser? The traditional web is a browser that actively sends requests to the server, and the server returns data to the browser after it receives the request. If you want to implement a server to proactively send messages to the browser, that is, the implementation of the Web version of instant communication should do? There seems to be no answer. Before the use of polling to implement pseudo-instant communication, CLIENT1 to send a message to CLIENT2, only the message sent to the server, The server waits until the CLIENT2 to inquire to have the new message the time only then sends the message which CLIENT1 sends to CLIENT2. This way, the client page must be timed to send a query request to the server, such as 5 seconds or 10 seconds once, to implement pseudo-instant communication.
Dragon Boat Festival holiday stroll 51cto found a better solution: Websocket, the following records a few Java write Websocket chestnut, real instant communication.
Chestnut 1
Front end as follows
<%@ page language= "java" contenttype= "Text/html; charset=utf-8" pageencoding= "UTF-8"%><! doctype html public "-//w3c//dtd html 4.01 transitional//en" "HTTP// Www.w3.org/TR/html4/loose.dtd ">
Package com.websocket;import java.io.ioexception;import javax.websocket.closereason;import javax.websocket.Endpoint;import javax.websocket.EndpointConfig;import javax.websocket.messagehandler;import javax.websocket.remoteendpoint;import javax.websocket.session; import javax.websocket.server.serverendpoint; @ServerEndpoint (value = "/websocket") public class Websocket extends Endpoint { private Session session; @Override public void onopen (Session Session, endpointconfig config) { Remoteendpoint.basic remote = session.getbasicremote (); system.out.println ("Pathparams:" + session.getpathparameters ()); system.out.println ("Requestparams" &nbSp;+ session.getrequestparametermap ()); Session.addmessagehandler (New mymessagehandle (remote)); } @ Override public void onclose (session session, closereason Closereason) { system.out.println ("OnClose"); } @Override public void onerror (Session session, java.lang.throwable throwable) { System.out.println ("OnError"); } private class mymessagehandle implements messagehandler.whole<string> { RemoteEndpoint.Basic remote = null; public mymessagehandle (REMOTEENDPOInt. Basic remote) { this.remote = remote; } @Override public void onmessage (String s) { system.out.println (s); try { remote.sendtext ("Success"); } catch (ioexception e) { E.printstacktrace (); } } }} Transport test run on TOMCAT7 normal
Chestnut 2
Front end as follows
<%@ page language= "java" contenttype= "Text/html; charset=utf-8" pageencoding= "UTF-8"%><! doctype html public "-//w3c//dtd html 4.01 transitional//en" "HTTP// Www.w3.org/TR/html4/loose.dtd ">
Package com.websocket;import java.io.ioexception;import javax.websocket.endpointconfig;import javax.websocket.onclose;import javax.websocket.onmessage;import javax.websocket.onopen;import javax.websocket.Session;import javax.websocket.server.ServerEndpoint; @ServerEndpoint (value = "/websocket1") public class websocket1 { private session session; @OnOpen public void open (Session session , endpointconfig config) { this.session = session; system.out.println ("*** WebSocket opened from sessionid " + session.getid ()); // Syslogger.info ("*** websocket opened from sessionid " + // session.getid ()); } @OnMessage Public void inmessage (session session, string message) { // syslogger.info ("*** websocket received from sessionid " + // this.session.getid () + ": " + message" system.out.println ("WebSocket received from sessionid: " + session.getid () + "--" + message); try { session.getbasicremote (). SenDtext ("Server received message:" + message); } catch ( Ioexception e) { E.printstacktrace (); } } @OnClose public void end (session session) { // syslogger.info ("*** websocket closed from sessionid " + // this.session.getid ()); system.out.println ("*** websocket closed from sessionid " + this.session.getid ()); }}Test TOMCAT7
Chestnut 3
Front end as follows
<%@ page language= "java" contenttype= "Text/html; charset=utf-8" pageencoding= "UTF-8"%><! doctype html public "-//w3c//dtd html 4.01 transitional//en" "HTTP// Www.w3.org/TR/html4/loose.dtd ">
Package com.websocket;import javax.websocket.onclose;import javax.websocket.onerror;import javax.websocket.OnMessage;import javax.websocket.OnOpen;import javax.websocket.RemoteEndpoint; import javax.websocket.session;import javax.websocket.server.serverendpoint; @ServerEndpoint (value= "/ Websocket2 ") public class Websocket2 { @OnOpen public void start (session session) { system.out.println ("session " +session.getid () + " open."); } @OnMessage public void process (session session, string message) { system.out.println ("rece:" + message); &nbSp Remoteendpoint.basic remote = session.getbasicremote (); int c = integer.valueof (message); for (int i=1; i<=c; i++) { try { remote.sendtext ("response " +i); thread.sleep ( ); } catch ( Exception e) { e.printstacktrace (); } } } @OnClose public void end (Session session) { system.out.println ("session " + Session.getid () + " close."); } @OnError public void error (session session, java.lang.throwable Throwable) { system.err.println ("session " + Session.getid () + " error:" +throwable); } }
layer has a high-value web chat interface http://sentsin.com/layui/layim/
If you want to implement web chat online, consider combining the front and websocket of the layer to achieve ...
June 30, 2015 22:15:36 By:umgsai, Yichang, Hubei
This article is from "Avatar" blog, please make sure to keep this source http://shamrock.blog.51cto.com/2079212/1669554
Java Implementation WebSocket