|
|
|
@ -4,6 +4,7 @@ import cn.hutool.extra.spring.SpringUtil;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
import com.nmgs.config.SubscribeListener;
|
|
|
|
import com.nmgs.config.SubscribeListener;
|
|
|
|
import com.nmgs.entity.Consistant;
|
|
|
|
import com.nmgs.entity.Consistant;
|
|
|
|
|
|
|
|
import com.nmgs.util.LogUtil;
|
|
|
|
import com.nmgs.util.WebsocketUtil;
|
|
|
|
import com.nmgs.util.WebsocketUtil;
|
|
|
|
import com.nmgs.util.redisConfigUtil;
|
|
|
|
import com.nmgs.util.redisConfigUtil;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
@ -60,13 +61,11 @@ public class WebSocketRedis {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
@OnOpen
|
|
|
|
@OnOpen
|
|
|
|
public void onOpen(@PathParam("userId") String userId, Session session) throws IOException {
|
|
|
|
public void onOpen(@PathParam("userId") String userId, Session session) throws IOException {
|
|
|
|
System.out.println("连接websocket的人员======"+userId);
|
|
|
|
|
|
|
|
this.session = session;
|
|
|
|
this.session = session;
|
|
|
|
//加入set中
|
|
|
|
//加入set中
|
|
|
|
webSocketSet.add(this);
|
|
|
|
webSocketSet.add(this);
|
|
|
|
//在线数加1
|
|
|
|
//在线数加1
|
|
|
|
addOnlineCount();
|
|
|
|
addOnlineCount();
|
|
|
|
System.out.println("有新连接[" + userId + "]加入!当前在线人数为"+this.getOnlineCount());
|
|
|
|
|
|
|
|
subscribeListener = new SubscribeListener();
|
|
|
|
subscribeListener = new SubscribeListener();
|
|
|
|
subscribeListener.setSession(session);
|
|
|
|
subscribeListener.setSession(session);
|
|
|
|
//设置订阅topic
|
|
|
|
//设置订阅topic
|
|
|
|
@ -87,7 +86,7 @@ public class WebSocketRedis {
|
|
|
|
* 连接关闭调用的方法
|
|
|
|
* 连接关闭调用的方法
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
@OnClose
|
|
|
|
@OnClose
|
|
|
|
public void onClose(Session session) throws IOException {
|
|
|
|
public void onClose(Session session, @PathParam("userId") String userId) throws IOException {
|
|
|
|
//从set中删除
|
|
|
|
//从set中删除
|
|
|
|
webSocketSet.remove(this);
|
|
|
|
webSocketSet.remove(this);
|
|
|
|
//在线数减1
|
|
|
|
//在线数减1
|
|
|
|
@ -105,7 +104,6 @@ public class WebSocketRedis {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
@OnMessage
|
|
|
|
@OnMessage
|
|
|
|
public void onMessage(String message, Session session) throws IOException {
|
|
|
|
public void onMessage(String message, Session session) throws IOException {
|
|
|
|
System.out.println("来自客户端的消息:{}"+message);
|
|
|
|
|
|
|
|
JSONObject retJo=new JSONObject();
|
|
|
|
JSONObject retJo=new JSONObject();
|
|
|
|
retJo.put("code",1);
|
|
|
|
retJo.put("code",1);
|
|
|
|
retJo.put("msg",message);
|
|
|
|
retJo.put("msg",message);
|
|
|
|
@ -121,7 +119,15 @@ public class WebSocketRedis {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
@OnError
|
|
|
|
@OnError
|
|
|
|
public void onError(Session session, Throwable error) {
|
|
|
|
public void onError(Session session, Throwable error) {
|
|
|
|
log.info("发生错误,{}", error);
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
webSocketSet.remove(this);
|
|
|
|
|
|
|
|
if (session.isOpen()) {
|
|
|
|
|
|
|
|
session.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
LogUtil.WriteLog_Error("websocketRedis连接出错====>" + e.getMessage(), "WebsocketRedis");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
|