????Java Web??????

1 package me.gacl.websocket;
2
3 import java.io.IOException;
4 import java.util.concurrent.CopyOnWriteArraySet;
5
6 import javax.websocket.*;
7 import javax.websocket.server.ServerEndpoint;
8
9 /**
10  * @ServerEndpoint ???????????ε?????????????????????????????websocket?????????
11  * ??????????????????????????????URL???????????????????URL???????WebSocket????????
12  */
13 @ServerEndpoint("/websocket")
14 public class WebSocketTest {
15     //?????????????????????????????????e???????????????
16     private static int onlineCount = 0;
17
18     //concurrent?????????Set?????????????????????MyWebSocket????????????????????????????????????Map??????????Key???????????
19     private static CopyOnWriteArraySet<WebSocketTest> webSocketSet = new CopyOnWriteArraySet<WebSocketTest>();
20
21     //??????????????????????????????????????????
22     private Session session;
23
24     /**
25      * ??????????????????
26      * @param session  ??????????session???????????????????????????????????????????
27      */
28     @OnOpen
29     public void onOpen(Session session){
30         this.session = session;
31         webSocketSet.add(this);     //????set??
32         addOnlineCount();           //????????1
33         System.out.println("???????????????????????" + getOnlineCount());
34     }
35
36     /**
37      * ?????????????
38      */
39     @OnClose
40     public void onClose(){
41         webSocketSet.remove(this);  //??set?????
42         subOnlineCount();           //????????1
43         System.out.println("??????????????????????" + getOnlineCount());
44     }
45
46     /**
47      * ????????????????????
48      * @param message ????????????????
49      * @param session ????????
50      */
51     @OnMessage
52     public void onMessage(String message?? Session session) {
53         System.out.println("????????????:" + message);
54         //??????
55         for(WebSocketTest item: webSocketSet){
56             try {
57                 item.sendMessage(message);
58             } catch (IOException e) {
59                 e.printStackTrace();
60                 continue;
61             }
62         }
63     }
64
65     /**
66      * ?????????????
67      * @param session
68      * @param error
69      */
70     @OnError
71     public void onError(Session session?? Throwable error){
72         System.out.println("????????");
73         error.printStackTrace();
74     }
75
76     /**
77      * ????????????漸?????????????????????????????????????????
78      * @param message
79      * @throws IOException
80      */
81     public void sendMessage(String message) throws IOException{
82         this.session.getBasicRemote().sendText(message);
83         //this.session.getAsyncRemote().sendText(message);
84     }
85
86     public static synchronized int getOnlineCount() {
87         return onlineCount;
88     }
89
90     public static synchronized void addOnlineCount() {
91         WebSocketTest.onlineCount++;
92     }
93
94     public static synchronized void subOnlineCount() {
95         WebSocketTest.onlineCount--;
96     }
97 }

????2.2.????Ч??
????????Google?????????????????ж????????????????Ч??????: