|
|
|
@ -1,18 +1,14 @@
|
|
|
|
|
package cn.estsh.i3plus.core.apiservice.websocket;
|
|
|
|
|
|
|
|
|
|
import cn.estsh.i3plus.core.api.iservice.busi.ISysMessageService;
|
|
|
|
|
import cn.estsh.i3plus.pojo.base.enumutil.ImppEnumUtil;
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
import cn.estsh.impp.framework.base.controller.CoreBaseController;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import javax.websocket.*;
|
|
|
|
|
import javax.websocket.server.PathParam;
|
|
|
|
|
import javax.websocket.server.ServerEndpoint;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
import java.util.concurrent.ConcurrentMap;
|
|
|
|
|
|
|
|
|
@ -23,38 +19,25 @@ import java.util.concurrent.ConcurrentMap;
|
|
|
|
|
* @CreateDate : 2018-11-24 16:57
|
|
|
|
|
* @Modify:
|
|
|
|
|
**/
|
|
|
|
|
@ServerEndpoint(value="/message-websocket/{userId}")
|
|
|
|
|
@ServerEndpoint(value= CoreBaseController.BASE_URL + "/message-websocket/{userId}")
|
|
|
|
|
@Component
|
|
|
|
|
public class MessageWebSocket {
|
|
|
|
|
|
|
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(MessageWebSocket.class);
|
|
|
|
|
|
|
|
|
|
private Long userId = 1L;
|
|
|
|
|
|
|
|
|
|
//在线连接数
|
|
|
|
|
private static int onlineCount = 0;
|
|
|
|
|
|
|
|
|
|
private long userId = 1L;
|
|
|
|
|
//websocket会话
|
|
|
|
|
private Session session; // 当前对象会话
|
|
|
|
|
private static int sendCount = 1;
|
|
|
|
|
//concurrent线程安全集合,存放客户端websocket对象
|
|
|
|
|
private static ConcurrentMap<Long,MessageWebSocket> webSocketSet = new ConcurrentHashMap<Long,MessageWebSocket>();
|
|
|
|
|
|
|
|
|
|
//websocket会话
|
|
|
|
|
private Session session;
|
|
|
|
|
|
|
|
|
|
public static ConcurrentMap<Long, MessageWebSocket> getWebSocketSet() {
|
|
|
|
|
return webSocketSet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private ISysMessageService sysMessageService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@OnOpen
|
|
|
|
|
public void onOpen(@PathParam("userId")Long userId, Session session){
|
|
|
|
|
this.session = session;
|
|
|
|
|
public void onOpen(@PathParam("userId")long userId, Session session){
|
|
|
|
|
this.userId = userId;
|
|
|
|
|
this.session = session;
|
|
|
|
|
|
|
|
|
|
webSocketSet.put(userId,this); //加入set中
|
|
|
|
|
addOnlineCount(); //在线数加1
|
|
|
|
|
webSocketSet.put(userId,this); //在线人数添加
|
|
|
|
|
LOGGER.info("{}加入!当前在线人数为{}",userId,getOnlineCount());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -63,8 +46,7 @@ public class MessageWebSocket {
|
|
|
|
|
*/
|
|
|
|
|
@OnClose
|
|
|
|
|
public void onClose() {
|
|
|
|
|
webSocketSet.remove(this.userId); //从set中删除
|
|
|
|
|
subOnlineCount(); //在线数减1
|
|
|
|
|
subOnlineUser(this.userId);
|
|
|
|
|
LOGGER.info("有一连接关闭!当前在线人数为" + getOnlineCount());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -73,8 +55,13 @@ public class MessageWebSocket {
|
|
|
|
|
*
|
|
|
|
|
* @param message 客户端发送过来的消息*/
|
|
|
|
|
@OnMessage
|
|
|
|
|
public void onMessage(String message, Session session) {
|
|
|
|
|
LOGGER.info("来自客户端的消息:" + message);
|
|
|
|
|
public void onMessage(@PathParam("userId")Long userId,String message) {
|
|
|
|
|
// 心跳
|
|
|
|
|
if("heartBit".equals(message)){
|
|
|
|
|
this.sendMessage(userId,"heartBit");
|
|
|
|
|
}else{
|
|
|
|
|
LOGGER.info("来自客户端的消息:" , message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -87,21 +74,28 @@ public class MessageWebSocket {
|
|
|
|
|
error.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void sendMessage() throws IOException {
|
|
|
|
|
List userMessageList = sysMessageService.findSysRefUserMessageByUserIdAndStatus(this.userId,
|
|
|
|
|
ImppEnumUtil.MESSAGE_STATUS.UNREAD.getValue());
|
|
|
|
|
this.session.getBasicRemote().sendText(JSON.toJSONString(userMessageList));
|
|
|
|
|
/**
|
|
|
|
|
* 发送消息
|
|
|
|
|
* @param message
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
public static void sendMessage(Long userId, String message){
|
|
|
|
|
try {
|
|
|
|
|
MessageWebSocket websocket = webSocketSet.get(userId);
|
|
|
|
|
if (websocket != null){
|
|
|
|
|
websocket.session.getBasicRemote().sendText(message + "=" + sendCount);
|
|
|
|
|
sendCount++;
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static synchronized int getOnlineCount() {
|
|
|
|
|
return onlineCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static synchronized void addOnlineCount() {
|
|
|
|
|
MessageWebSocket.onlineCount++;
|
|
|
|
|
return webSocketSet.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static synchronized void subOnlineCount() {
|
|
|
|
|
MessageWebSocket.onlineCount--;
|
|
|
|
|
public synchronized void subOnlineUser(long userId) {
|
|
|
|
|
webSocketSet.remove(userId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|