diff --git a/src/main/java/com/nmgs/config/FilterConfig.java b/src/main/java/com/nmgs/config/FilterConfig.java index da83fe0..f5144e4 100644 --- a/src/main/java/com/nmgs/config/FilterConfig.java +++ b/src/main/java/com/nmgs/config/FilterConfig.java @@ -24,10 +24,8 @@ public class FilterConfig implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if(request.getHeader("Origin")==null){ - System.out.println("request.getHeader(\"Origin\")=======>为空"); response.setHeader("Access-Control-Allow-Origin", "*");//支持跨域请求 }else{ - System.out.println("request.getHeader(\"Origin\")=======>" + request.getHeader("Origin")); response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));//支持跨域请求 } response.setHeader("Access-Control-Allow-Credentials", "true");//是否支持cookie跨域 diff --git a/src/main/java/com/nmgs/controller/WebSocket.java b/src/main/java/com/nmgs/controller/WebSocket.java index d8dea72..d7c6d00 100644 --- a/src/main/java/com/nmgs/controller/WebSocket.java +++ b/src/main/java/com/nmgs/controller/WebSocket.java @@ -26,24 +26,20 @@ public class WebSocket { @OnOpen public void onOpen(Session session,@PathParam(value = "userId") String userId) throws IOException { - LogUtil.WriteLog_Info("连接websocket的人员======" + userId, "WebSocket"); WebsocketUtil.addSession(userId, session); JSONObject retJo=new JSONObject(); retJo.put("code",1); retJo.put("msg","连接成功"); - retJo.put("data",""); + retJo.put("data", "创建连接"); WebsocketUtil.sendMessage(userId,JSONObject.toJSONString(retJo)); } @OnClose public void onClose(Session session,@PathParam(value = "userId") String userId) { - LogUtil.WriteLog_Info("关闭连接的人员======" + userId, "WebSocket"); - String socketId = userId; - WebsocketUtil.removeSession(socketId); + WebsocketUtil.removeSession(userId); } @OnMessage public void onMessage(Session session,@PathParam(value = "userId") String userId, String message) { - System.out.println("发送消息的人员======"+userId); JSONObject retJo = new JSONObject(); retJo.put("code", 1); retJo.put("msg", "连接成功"); @@ -53,15 +49,13 @@ public class WebSocket { @OnError public void onError(Session session,@PathParam(value = "userId") String userId, Throwable throwable) { - String socketId = session.getId(); try { - WebsocketUtil.removeSession(socketId); + WebsocketUtil.removeSession(userId); if (session.isOpen()) { session.close(); } } catch (IOException e) { - e.printStackTrace(); + LogUtil.WriteLog_Error("websocket连接出错====>" + e.getMessage(), "Websocket"); } - throwable.printStackTrace(); } } \ No newline at end of file diff --git a/src/main/java/com/nmgs/controller/WebSocketRedis.java b/src/main/java/com/nmgs/controller/WebSocketRedis.java index a39fbac..3f5eb94 100644 --- a/src/main/java/com/nmgs/controller/WebSocketRedis.java +++ b/src/main/java/com/nmgs/controller/WebSocketRedis.java @@ -4,6 +4,7 @@ import cn.hutool.extra.spring.SpringUtil; import com.alibaba.fastjson.JSONObject; import com.nmgs.config.SubscribeListener; import com.nmgs.entity.Consistant; +import com.nmgs.util.LogUtil; import com.nmgs.util.WebsocketUtil; import com.nmgs.util.redisConfigUtil; import org.apache.commons.logging.Log; @@ -60,13 +61,11 @@ public class WebSocketRedis { */ @OnOpen public void onOpen(@PathParam("userId") String userId, Session session) throws IOException { - System.out.println("连接websocket的人员======"+userId); this.session = session; //加入set中 webSocketSet.add(this); //在线数加1 addOnlineCount(); - System.out.println("有新连接[" + userId + "]加入!当前在线人数为"+this.getOnlineCount()); subscribeListener = new SubscribeListener(); subscribeListener.setSession(session); //设置订阅topic @@ -87,7 +86,7 @@ public class WebSocketRedis { * 连接关闭调用的方法 */ @OnClose - public void onClose(Session session) throws IOException { + public void onClose(Session session, @PathParam("userId") String userId) throws IOException { //从set中删除 webSocketSet.remove(this); //在线数减1 @@ -105,7 +104,6 @@ public class WebSocketRedis { */ @OnMessage public void onMessage(String message, Session session) throws IOException { - System.out.println("来自客户端的消息:{}"+message); JSONObject retJo=new JSONObject(); retJo.put("code",1); retJo.put("msg",message); @@ -121,7 +119,15 @@ public class WebSocketRedis { */ @OnError 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"); + } } /**