http://blog.csdn.net/shangmingtao/article/details/75810099
Directory:
Catalog WebSocket Introduction Project Background hardware environment and client support this article is based on the Javaxwebsocket service-side code source to follow up the GIT Connection client code problem Exploration August 3 supplemental Intermediate line disconnection situation how to support thousands of client simultaneous online people Follow-up August 3 supplementary test
1.WebSocket Introduction
websocket_ Baidu Encyclopedia 2. Project background, hardware environment and client support
This project through WebSocket to achieve simultaneous online user volume of thousands of of the push server (intranet operation). And the user's online status can be viewed in real time. Server: CentOS 6.5, Tomcat 7 client: Mobile (Android, IOS), web-side. Server-side third-party libraries: Javax.websocket 3. Research contents in this paper
Various exception handling after the application of the online environment: When using WebSocket, relying on TCP keepalive or doing Business layer heartbeat server How to sense the client disconnects (to see the real-time user online status) how the client perceives a service-side exception (to determine when the client is re-connected) 4. Based on the Javax.websocket service-side code (source next to add git connection)
Websocketserver.java
Package Cn.milo.wsdemo;
Import Org.apache.log4j.Logger;
Import javax.websocket.*;
Import Javax.websocket.server.PathParam;
Import Javax.websocket.server.ServerEndpoint; /* * CREATE:17-07-21 * Auth:milo */@ServerEndpoint ("/connect/{userid}") public class Websocketserver {privat
e static Logger log = Logger.getlogger (Websocketserver.class);
/* New Connected */@OnOpen public void OnOpen (@PathParam ("userid") String userId,
Session session) {Log.info ("[websocketserver] Connected:userid =" + userId);
Websocketutils.add (UserId, session);
}/* Send Message * * @OnMessage public string OnMessage (@PathParam ("userid") string userId, String message) {Log.info ("[websocketserver] Received Message:userid =" + UserId + ", mess
Age = "+ message");
if (Message.equals ("&")) {return "&"; }else{websocketutils.receive (userId, message);
Return "Got your message (" + Message + ").";
}}//Errot */@OnError public void OnError (@PathParam ("userid") String userId, Throwable Throwable, Session session) {Log.info ("[Websocketserver] Connection Ex
Ception:userid = "+ UserId +", Throwable = "+ throwable.getmessage ());
Websocketutils.remove (USERID);
}/* Close Connection */@OnClose public void OnClose (@PathParam ("userid") String userId,
Session session) {Log.info ("[websocketserver] Close Connection:userid =" + userId);
Websocketutils.remove (USERID); }
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
Websocketutils.java
Package Cn.milo.wsdemo;
Import Cn.milo.FileUtils.CreateFile;
Import Org.apache.log4j.Logger;
Import javax.websocket.Session;
Import Java.util.Map;
Import Java.util.concurrent.ConcurrentHashMap;
public class Websocketutils {private static Logger log = Logger.getlogger (Websocketutils.class);
public static map<string, session> clients = new concurrenthashmap<string, session> (); /* Add Session */public static void Add (String userId, session session) {Clients.put (userid,session
);
Log.info ("current number of connections =" + clients.size ()); }/* Receive Message */public static void receive (String userId, String Message) {Log.info ("received
Message: UserId = "+ userid +", message = "+ Message";
Log.info ("current number of connections =" + clients.size ());
}/* Remove Session */public static void Remove (String userid) {clients.remove (userid);
Log.info ("current number of connections =" + clients.size ()); }/* GeT Session */public static Boolean SendMessage (string userId, String message) {Log.info ("current number of connections =" + CL
Ients.size ());
if (Clients.get (userId) = = null) {return false;
}else{Clients.get (userId). Getasyncremote (). SendText (message);
return true; }
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
5. Client Code
<body> server address: <input id = "ServerAddress" type= "text"/><br/> your User id: <input id = "UserId" type= "Text"/><br/> <button onclick= "Initsocket ()" > Connections </button><br/> ======================== =============================<br/> message: <input id = "message" type= "text"/><br/> <button onclick= " Send () > Send </button><br/> =====================================================<br/> Connection status: <button onclick= "Clearconnectstatu ()" > Emptying </button><br/> <div id= "Connectstatu" ></div ><br/> =====================================================<br/> received the message:<br/> <div id= " Receivedmessage "></div><br/> =====================================================<br/> Heartbeat: <br/> <div id= "Heartdiv" ></div><br/> </body> <script src= "<%=basepath%>/ Resources/jquery-1.7.2.min.js "></script> <script type=" Text/javascript "> var heartflag = false;
var webSocket = null;
var trytime = 0;
$ (function () {//Initsocket ();
Window.onbeforeunload = function () {};
});
/** * Initialize WebSocket, establish connection */function Initsocket () {var serveraddress = $ ("#serveraddress"). Val ();
var userId = $ ("#userId"). Val (); if (!window.
WebSocket) {$ ("#connectStatu"). Append (Getnowformatdate () + "Your browser does not support ws<br/>"); Return