|
@@ -88,21 +88,25 @@ public class WebSocketServer {
|
|
|
WebSocketUsers.sendMessageToUserByText(session, "当前在线人数超过限制数:" + socketMaxOnlineCount);
|
|
|
session.close();
|
|
|
} else {
|
|
|
+ // 解析查询参数
|
|
|
+ Map<String, String> queryParams = getQueryParams(session.getRequestURI().getQuery());
|
|
|
+ String type = queryParams.get("type");
|
|
|
// 返回此会话的经过身份验证的用户,如果此会话没有经过身份验证的用户,则返回null
|
|
|
Authentication authentication = (Authentication) session.getUserPrincipal();
|
|
|
SecurityUtils.setAuthentication(authentication);
|
|
|
Long userId = SecurityUtils.getUserId();
|
|
|
this.session = session;
|
|
|
+ String sidStr = type + "_" + userId;
|
|
|
//如果存在就先删除一个,防止重复推送消息
|
|
|
for (WebSocketServer webSocket : webSocketSet) {
|
|
|
- if (webSocket.sid.equals(String.valueOf(userId))) {
|
|
|
+ if (webSocket.sid.equals(sidStr)) {
|
|
|
webSocketSet.remove(webSocket);
|
|
|
count.getAndDecrement();
|
|
|
}
|
|
|
}
|
|
|
count.getAndIncrement();
|
|
|
webSocketSet.add(this);
|
|
|
- this.sid = String.valueOf(userId);
|
|
|
+ this.sid = sidStr;
|
|
|
LOGGER.info("\n 当前人数 - {}", count);
|
|
|
WebSocketUsers.sendMessageToUserByText(session, "连接成功");
|
|
|
}
|
|
@@ -154,21 +158,8 @@ public class WebSocketServer {
|
|
|
* 刷新定时任务,发送信息
|
|
|
*/
|
|
|
private void refresh(String userId, Authentication authentication, String message) {
|
|
|
-// this.start(5000L, task -> {
|
|
|
-// // 判断用户是否在线,不在线则不用处理,因为在内部无法关闭该定时任务,所以通过返回值在外部进行判断。
|
|
|
-// if (WebSocketServer.isConn(userId)) {
|
|
|
-// // 因为这里是长链接,不会和普通网页一样,每次发送http 请求可以走拦截器【doFilterInternal】续约,所以需要手动续约
|
|
|
-// SecurityUtils.setAuthentication(authentication);
|
|
|
-// // 从数据库或者缓存中获取信息,构建自定义的Bean
|
|
|
-// CommunityChatMsg chatMsg = saveChat(message);
|
|
|
-// // TODO判断数据是否有更新
|
|
|
-// // 发送最新数据给前端
|
|
|
-// WebSocketServer.sendInfo("JSON", chatMsg, String.valueOf(chatMsg.getReceiverId()));
|
|
|
-// // 设置返回值,判断是否需要继续执行
|
|
|
-// return true;
|
|
|
-// }
|
|
|
-// return false;
|
|
|
-// });
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(message);
|
|
|
+ String type = jsonObject.getString("type");
|
|
|
// 判断用户是否在线,不在线则不用处理,因为在内部无法关闭该定时任务,所以通过返回值在外部进行判断。
|
|
|
if (WebSocketServer.isConn(userId)) {
|
|
|
// 因为这里是长链接,不会和普通网页一样,每次发送http 请求可以走拦截器【doFilterInternal】续约,所以需要手动续约
|
|
@@ -177,8 +168,9 @@ public class WebSocketServer {
|
|
|
CommunityChatMsg chatMsg = saveChat(message);
|
|
|
// TODO判断数据是否有更新
|
|
|
// 发送最新数据给前端
|
|
|
- if (WebSocketServer.isConn(String.valueOf(chatMsg.getReceiverId()))) {
|
|
|
- WebSocketServer.sendInfo("JSON", chatMsg, String.valueOf(chatMsg.getReceiverId()));
|
|
|
+ String receiverSid = type + "_" + chatMsg.getReceiverId();
|
|
|
+ if (WebSocketServer.isConn(receiverSid)) {
|
|
|
+ WebSocketServer.sendInfo("JSON", chatMsg, receiverSid);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -260,6 +252,7 @@ public class WebSocketServer {
|
|
|
chatMsg.setReceiverId(receiverId);
|
|
|
chatMsg.setCreateBy(senderId);
|
|
|
chatMsg.setRead(false);
|
|
|
+ chatMsg.setType(type);
|
|
|
chatMsg.setMessageType(messageType);
|
|
|
chatMsg.setCreateTime(DateUtils.parseDate(DateUtils.getTime()));
|
|
|
switch (messageType) {
|
|
@@ -271,11 +264,11 @@ public class WebSocketServer {
|
|
|
break;
|
|
|
case 1:
|
|
|
//图片
|
|
|
- chatMsg.setUrl(jsonObject.getString("url"));
|
|
|
+ chatMsg.setFileUrl(jsonObject.getString("fileUrl"));
|
|
|
break;
|
|
|
case 2:
|
|
|
//语音
|
|
|
- chatMsg.setUrl(jsonObject.getString("url"));
|
|
|
+ chatMsg.setFileUrl(jsonObject.getString("fileUrl"));
|
|
|
chatMsg.setTime(jsonObject.getInteger("time"));
|
|
|
break;
|
|
|
case 3:
|
|
@@ -289,7 +282,7 @@ public class WebSocketServer {
|
|
|
break;
|
|
|
case 4:
|
|
|
//视频
|
|
|
- chatMsg.setUrl(jsonObject.getString("url"));
|
|
|
+ chatMsg.setFileUrl(jsonObject.getString("fileUrl"));
|
|
|
break;
|
|
|
|
|
|
}
|
|
@@ -298,4 +291,20 @@ public class WebSocketServer {
|
|
|
return chatMsg;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 工具方法:解析查询参数
|
|
|
+ */
|
|
|
+ private Map<String, String> getQueryParams(String query) {
|
|
|
+ Map<String, String> params = new HashMap<>();
|
|
|
+ if (query != null) {
|
|
|
+ String[] pairs = query.split("&");
|
|
|
+ for (String pair : pairs) {
|
|
|
+ String[] keyValue = pair.split("=");
|
|
|
+ if (keyValue.length == 2) {
|
|
|
+ params.put(keyValue[0], keyValue[1]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return params;
|
|
|
+ }
|
|
|
}
|