資源簡(jiǎn)介
websocket java 實(shí)現(xiàn)例子 tomcat 8 jdk 1.8

代碼片段和文件信息
package?tt;
import?java.io.IOException;
import?java.util.concurrent.CopyOnWriteArraySet;?
import?javax.websocket.OnClose;
import?javax.websocket.onerror;
import?javax.websocket.OnMessage;
import?javax.websocket.OnOpen;
import?javax.websocket.Session;
import?javax.websocket.server.ServerEndpoint;
//該注解用來(lái)指定一個(gè)URI,客戶(hù)端可以通過(guò)這個(gè)URI來(lái)連接到WebSocket。類(lèi)似Servlet的注解mapping。無(wú)需在web.xml中配置。
@ServerEndpoint(“/websocket“)
public?class?MyWebSocket?{
????//靜態(tài)變量,用來(lái)記錄當(dāng)前在線(xiàn)連接數(shù)。應(yīng)該把它設(shè)計(jì)成線(xiàn)程安全的。
????private?static?int?onlineCount?=?0;?????
????//concurrent包的線(xiàn)程安全Set,用來(lái)存放每個(gè)客戶(hù)端對(duì)應(yīng)的MyWebSocket對(duì)象。若要實(shí)現(xiàn)服務(wù)端與單一客戶(hù)端通信的話(huà),可以使用Map來(lái)存放,其中Key可以為用戶(hù)標(biāo)識(shí)
????private?static?CopyOnWriteArraySet?webSocketSet?=?new?CopyOnWriteArraySet();
?????
????//與某個(gè)客戶(hù)端的連接會(huì)話(huà),需要通過(guò)它來(lái)給客戶(hù)端發(fā)送數(shù)據(jù)
????private?Session?session;
????
????/**
?????*?連接建立成功調(diào)用的方法
?????*?@param?session??可選的參數(shù)。session為與某個(gè)客戶(hù)端的連接會(huì)話(huà),需要通過(guò)它來(lái)給客戶(hù)端發(fā)送數(shù)據(jù)
?????*/
????@OnOpen
????public?void?onOpen(Session?session){
????????this.session?=?session;
????????webSocketSet.add(this);?????//加入set中
????????addOnlineCount();???????????//在線(xiàn)數(shù)加1
????????System.out.println(“有新連接加入!當(dāng)前在線(xiàn)人數(shù)為“?+?getOnlineCount()+session.getId());
????}
?????
????/**
?????*?連接關(guān)閉調(diào)用的方法
?????*/
????@OnClose
????public?void?onClose(){
????????webSocketSet.remove(this);??//從set中刪除
????????subOnlineCount();???????????//在線(xiàn)數(shù)減1?????
????????System.out.println(“有一連接關(guān)閉!當(dāng)前在線(xiàn)人數(shù)為“?+?getOnlineCount());
????}
?????
????/**
?????*?收到客戶(hù)端消息后調(diào)用的方法
?????*?@param?message?客戶(hù)端發(fā)送過(guò)來(lái)的消息
?????*?@param?session?可選的參數(shù)
?????*/
????@OnMessage
????public?void?onMessage(String?message?Session?session)?{
????????System.out.println(“來(lái)自客戶(hù)端的消息:“?+?message);
?????????
????????//群發(fā)消息
????????for(MyWebSocket?item:?webSocketSet){??????????????
????????????try?{
????????????????item.sendMessage(message);
????????????}?catch?(IOException?e)?{
????????????????e.printStackTrace();
????????????????continue;
????????????}
????????}
????}
?????
????/**
?????*?發(fā)生錯(cuò)誤時(shí)調(diào)用
?????*?@param?session
?????*?@param?error
?????*/
????@onerror
????public?void?onerror(Session?session?Throwable?error){
????????System.out.println(“發(fā)生錯(cuò)誤“);
????????error.printStackTrace();
????}
?????
????/**
?????*?這個(gè)方法與上面幾個(gè)方法不一樣。沒(méi)有用注解,是根據(jù)自己需要添加的方法。
?????*?@param?message
?????*?@throws?IOException
?????*/
????public?void?sendMessage(String?message)?throws?IOException{
????????this.session.getBasicRemote().sendText(message);
????????//this.session.getAsyncRemote().sendText(message);
????}
?
????public?static?synchronized?int?getOnlineCount()?{
????????return?onlineCount;
????}
?
????public?static?synchronized?void?addOnlineCount()?{
????????MyWebSocket.onlineCount++;
????}
?????
????public?static?synchronized?void?subOnlineCount()?{
????????MyWebSocket.onlineCount--;
????}
}
?屬性????????????大小?????日期????時(shí)間???名稱(chēng)
-----------?---------??----------?-----??----
?????文件????????841??2016-04-26?16:18??testwebsocket\.classpath
?????文件??????????0??2016-05-06?14:17??testwebsocket\.me
?????文件???????5961??2016-05-13?17:47??testwebsocket\.me
?????文件????????438??2016-05-06?14:19??testwebsocket\.me
?????文件??????????1??2016-05-13?17:47??testwebsocket\.me
?????文件????????151??2016-05-13?17:47??testwebsocket\.me
?????文件??????????1??2016-05-13?17:47??testwebsocket\.me
?????文件????????185??2016-05-13?17:47??testwebsocket\.me
?????文件????????948??2016-05-13?17:47??testwebsocket\.me
?????文件????????371??2016-05-06?14:18??testwebsocket\.me
?????文件?????????58??2016-05-06?14:18??testwebsocket\.me
?????文件????????165??2016-05-06?14:18??testwebsocket\.me
?????文件???????2906??2016-05-06?14:18??testwebsocket\.me
?????文件????????565??2016-05-06?14:23??testwebsocket\.me
?????文件?????????58??2016-05-06?14:17??testwebsocket\.me
?????文件?????????73??2016-05-06?14:23??testwebsocket\.me
?????文件?????????62??2016-05-06?14:17??testwebsocket\.me
?????文件?????????97??2016-05-06?14:17??testwebsocket\.me
?????文件????????194??2016-05-06?14:23??testwebsocket\.me
?????文件?????????50??2016-05-06?14:18??testwebsocket\.me
?????文件????????195??2016-05-06?14:23??testwebsocket\.me
?????文件?????????99??2016-05-06?14:17??testwebsocket\.me
?????文件?????????69??2016-05-06?14:23??testwebsocket\.me
?????文件????????126??2016-05-06?14:23??testwebsocket\.me
?????文件?????????48??2016-05-06?14:17??testwebsocket\.me
?????文件????????371??2016-05-06?14:23??testwebsocket\.me
?????文件?????403558??2016-05-13?17:47??testwebsocket\.me
?????文件?????????20??2016-05-06?14:17??testwebsocket\.me
?????文件????????238??2016-05-06?14:17??testwebsocket\.me
?????文件?????????11??2016-05-06?14:17??testwebsocket\.me
............此處省略113個(gè)文件信息
評(píng)論
共有 條評(píng)論