Project overall structure diagram:
Xml
<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_3_0.xsd "id=" webapp_id "version=" 3.0 "> <display-name>web</ display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list></web-app>
index.htm WS Client
<! DOCTYPE html>
Socketserver.java Core class, WS service side
1 PackageCom.websocket;2 3 Importjava.io.IOException;4 ImportJava.util.HashMap;5 ImportJava.util.Map;6 ImportJavax.websocket.OnClose;7 ImportJavax.websocket.OnError;8 ImportJavax.websocket.OnMessage;9 ImportJavax.websocket.OnOpen;Ten Importjavax.websocket.Session; One ImportJavax.websocket.server.ServerEndpoint; A - /** - * Environment the * Browser: Firefox,google Chrome - * tomcat7.0.69 - * jdk7.0.79 - * 1 Each browser represents a user, and after establishing a connection with the server, it realizes the interaction between the server and the browser. + * 2 expose WebSocket push interface, other server or business class call this interface, send message to the specified user - * @authorCaihao + * A */ at //URI annotations, which do not need to be configured in Web. Xml. -@ServerEndpoint ("/websocket") - Public classSocketserver { - - - //browser and server to reply, browser every new WebSocket create a session, close or refresh the browser, session closed in PrivateSession session; - //On behalf of the browser to PrivateString userid; + - /** the * Push Message interface * * External calls can be made $ * @paramsendmsgPanax Notoginseng * @throwsIOException - */ the Public voidSendmsg (String sendmsg)throwsioexception{ +System.out.println ( This. session+ ";" + This. userid+ ";" +sendmsg); A This. Session.getbasicremote (). SendText (sendmsg); the } + - //set up a map to store each user's connection $ Public StaticMap<string,socketserver> Websocketset =NewHashmap<string,socketserver>(); $ - - the @OnOpen - Public voidOnOpen (Session session)throwsIOException {Wuyi This. session =session; theSystem.out.println ( This+ "There is a new connection, session=" +session+ "; userid=" +userid); - } Wu - @OnClose About Public voidOnClose () { $Websocketset.remove ( This. userid); -System.out.println ( This+ "; Connection off"); - } - A @OnMessage + Public voidOnMessage (String info)throwsIOException { theSystem.out.println ( This+ "; Message from client:" +info); -String msg = "The server received a message from the client:" +info; $ if(Info.contains ("userid")){ the This. UserID = Info.split ("userid=") [1]; theSystem.out.println ( This+ ", this.session=" + This. session+ "; this.userid=" + This. userid); theWebsocketset.put (UserID, This); the } - } in the @OnError the Public voidonError (throwable error) { AboutSystem.out.println ( This+ "; Error occurred"); the error.printstacktrace (); the } the + -}
External classes, invoking push interfaces on the server side
1 PackageCom.websocket;2 3 Importjava.io.IOException;4 ImportJava.util.Map;5 Importjavax.servlet.ServletException;6 ImportJavax.servlet.annotation.WebServlet;7 ImportJavax.servlet.http.HttpServlet;8 Importjavax.servlet.http.HttpServletRequest;9 ImportJavax.servlet.http.HttpServletResponse;Ten One /** A * External class, invoke exposed push interface - */ -@WebServlet ("/servleta") the Public classServletaextendsHttpServlet { - Private Static Final LongSerialversionuid = 1L; - - protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { + This. DoPost (request, response); - } + A protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { atMap<string,socketserver> Websocketset =Socketserver.websocketset; - //traverse the user to send the specified content to the user based on the user's ID - for(Map.entry<string, socketserver>Entry:webSocketSet.entrySet ()) { -System.out.println (Entry.getkey () + "--->" +Entry.getvalue ()); -String key =Entry.getkey (); -Socketserver SS =Websocketset.get (key); inString sendmsg = "Send Message to" +key+ "; - ss.sendmsg (sendmsg); to } + - } the}
WebSocket Introductory Example