提交 | 用户 | 时间
|
71e81e
|
1 |
package cn.stylefeng.guns.modular.WebSocket; |
懒 |
2 |
|
|
3 |
import org.slf4j.Logger; |
|
4 |
import org.slf4j.LoggerFactory; |
|
5 |
import org.springframework.stereotype.Component; |
|
6 |
import javax.websocket.*; |
|
7 |
import javax.websocket.server.PathParam; |
|
8 |
import javax.websocket.server.ServerEndpoint; |
|
9 |
import java.io.IOException; |
|
10 |
import java.util.concurrent.ConcurrentHashMap; |
|
11 |
|
|
12 |
@ServerEndpoint(value = "/websocket/{userName}") |
|
13 |
@Component |
|
14 |
public class WebSocketService { |
|
15 |
|
|
16 |
private static final Logger log = LoggerFactory.getLogger(WebSocketService.class); |
|
17 |
|
|
18 |
//静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。 |
|
19 |
private static int onlineCount = 0; |
|
20 |
//concurrent包的线程安全Set,用来存放每个客户端对应的WebSocketServer对象。 |
|
21 |
private static ConcurrentHashMap<String, WebSocketClient> webSocketMap = new ConcurrentHashMap<>(); |
|
22 |
|
|
23 |
|
|
24 |
/**与某个客户端的连接会话,需要通过它来给客户端发送数据*/ |
|
25 |
private Session session; |
|
26 |
/**接收userName*/ |
|
27 |
private String userName=""; |
|
28 |
/** |
|
29 |
* 连接建立成功调用的方法*/ |
|
30 |
@OnOpen |
|
31 |
public void onOpen(Session session, @PathParam("userName") String userName) { |
|
32 |
if(!webSocketMap.containsKey(userName)) |
|
33 |
{ |
|
34 |
addOnlineCount(); // 在线数 +1 |
|
35 |
} |
|
36 |
this.session = session; |
|
37 |
this.userName= userName; |
|
38 |
WebSocketClient client = new WebSocketClient(); |
|
39 |
client.setSession(session); |
|
40 |
client.setUri(session.getRequestURI().toString()); |
|
41 |
webSocketMap.put(userName, client); |
|
42 |
|
|
43 |
log.info("----------------------------------------------------------------------------"); |
|
44 |
log.info("用户连接:"+userName+",当前在线人数为:" + getOnlineCount()); |
|
45 |
try { |
|
46 |
// sendMessage("来自后台的反馈:连接成功"); |
|
47 |
} catch (Exception e) { |
|
48 |
log.error("用户:"+userName+",网络异常!!!!!!"); |
|
49 |
} |
|
50 |
} |
|
51 |
|
|
52 |
/** |
|
53 |
* 连接关闭调用的方法 |
|
54 |
*/ |
|
55 |
@OnClose |
|
56 |
public void onClose() { |
|
57 |
if(webSocketMap.containsKey(userName)){ |
|
58 |
webSocketMap.remove(userName); |
|
59 |
if(webSocketMap.size()>0) |
|
60 |
{ |
|
61 |
//从set中删除 |
|
62 |
subOnlineCount(); |
|
63 |
} |
|
64 |
} |
|
65 |
log.info("----------------------------------------------------------------------------"); |
|
66 |
log.info(userName+"用户退出,当前在线人数为:" + getOnlineCount()); |
|
67 |
} |
|
68 |
|
|
69 |
/** |
|
70 |
* 收到客户端消息后调用的方法 |
|
71 |
* |
|
72 |
* @param message 客户端发送过来的消息*/ |
|
73 |
@OnMessage |
|
74 |
public void onMessage(String message, Session session) { |
|
75 |
log.info("收到用户消息:"+userName+",报文:"+message); |
|
76 |
//可以群发消息 |
|
77 |
//消息保存到数据库、redis |
|
78 |
} |
|
79 |
|
|
80 |
/** |
|
81 |
* |
|
82 |
* @param session |
|
83 |
* @param error |
|
84 |
*/ |
|
85 |
@OnError |
|
86 |
public void onError(Session session, Throwable error) { |
|
87 |
log.error("用户错误:"+this.userName+",原因:"+error.getMessage()); |
|
88 |
error.printStackTrace(); |
|
89 |
} |
|
90 |
|
|
91 |
/** |
|
92 |
* 连接服务器成功后主动推送 |
|
93 |
*/ |
|
94 |
public void sendMessage(String message) throws IOException { |
|
95 |
synchronized (session){ |
|
96 |
this.session.getBasicRemote().sendText(message); |
|
97 |
} |
|
98 |
} |
|
99 |
|
|
100 |
/** |
|
101 |
* 向指定客户端发送消息 |
|
102 |
* @param userName |
|
103 |
* @param message |
|
104 |
*/ |
|
105 |
public static void sendMessage(String userName,String message){ |
|
106 |
try { |
|
107 |
WebSocketClient webSocketClient = webSocketMap.get(userName); |
|
108 |
if(webSocketClient!=null){ |
|
109 |
webSocketClient.getSession().getBasicRemote().sendText(message); |
|
110 |
} |
|
111 |
} catch (IOException e) { |
|
112 |
e.printStackTrace(); |
|
113 |
throw new RuntimeException(e.getMessage()); |
|
114 |
} |
|
115 |
} |
|
116 |
|
|
117 |
|
|
118 |
public static synchronized int getOnlineCount() { |
|
119 |
return onlineCount; |
|
120 |
} |
|
121 |
|
|
122 |
public static synchronized void addOnlineCount() { |
|
123 |
WebSocketService.onlineCount++; |
|
124 |
} |
|
125 |
|
|
126 |
public static synchronized void subOnlineCount() { |
|
127 |
WebSocketService.onlineCount--; |
|
128 |
} |
|
129 |
|
|
130 |
public static void setOnlineCount(int onlineCount) { |
|
131 |
WebSocketService.onlineCount = onlineCount; |
|
132 |
} |
|
133 |
|
|
134 |
|
|
135 |
public static ConcurrentHashMap<String, WebSocketClient> getWebSocketMap() { |
|
136 |
return webSocketMap; |
|
137 |
} |
|
138 |
|
|
139 |
public static void setWebSocketMap(ConcurrentHashMap<String, WebSocketClient> webSocketMap) { |
|
140 |
WebSocketService.webSocketMap = webSocketMap; |
|
141 |
} |
|
142 |
|
|
143 |
public Session getSession() { |
|
144 |
return session; |
|
145 |
} |
|
146 |
|
|
147 |
public void setSession(Session session) { |
|
148 |
this.session = session; |
|
149 |
} |
|
150 |
|
|
151 |
public String getUserName() { |
|
152 |
return userName; |
|
153 |
} |
|
154 |
|
|
155 |
public void setUserName(String userName) { |
|
156 |
this.userName = userName; |
|
157 |
} |
|
158 |
|
|
159 |
} |