去除一些不必要日志

master
白美平 7 months ago
parent 288b7c7156
commit a59b5d384c

@ -24,10 +24,8 @@ public class FilterConfig implements HandlerInterceptor {
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if(request.getHeader("Origin")==null){ if(request.getHeader("Origin")==null){
System.out.println("request.getHeader(\"Origin\")=======>为空");
response.setHeader("Access-Control-Allow-Origin", "*");//支持跨域请求 response.setHeader("Access-Control-Allow-Origin", "*");//支持跨域请求
}else{ }else{
System.out.println("request.getHeader(\"Origin\")=======>" + request.getHeader("Origin"));
response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));//支持跨域请求 response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));//支持跨域请求
} }
response.setHeader("Access-Control-Allow-Credentials", "true");//是否支持cookie跨域 response.setHeader("Access-Control-Allow-Credentials", "true");//是否支持cookie跨域

@ -26,24 +26,20 @@ public class WebSocket {
@OnOpen @OnOpen
public void onOpen(Session session,@PathParam(value = "userId") String userId) throws IOException { public void onOpen(Session session,@PathParam(value = "userId") String userId) throws IOException {
LogUtil.WriteLog_Info("连接websocket的人员======" + userId, "WebSocket");
WebsocketUtil.addSession(userId, session); WebsocketUtil.addSession(userId, session);
JSONObject retJo=new JSONObject(); JSONObject retJo=new JSONObject();
retJo.put("code",1); retJo.put("code",1);
retJo.put("msg","连接成功"); retJo.put("msg","连接成功");
retJo.put("data",""); retJo.put("data", "创建连接");
WebsocketUtil.sendMessage(userId,JSONObject.toJSONString(retJo)); WebsocketUtil.sendMessage(userId,JSONObject.toJSONString(retJo));
} }
@OnClose @OnClose
public void onClose(Session session,@PathParam(value = "userId") String userId) { public void onClose(Session session,@PathParam(value = "userId") String userId) {
LogUtil.WriteLog_Info("关闭连接的人员======" + userId, "WebSocket"); WebsocketUtil.removeSession(userId);
String socketId = userId;
WebsocketUtil.removeSession(socketId);
} }
@OnMessage @OnMessage
public void onMessage(Session session,@PathParam(value = "userId") String userId, String message) { public void onMessage(Session session,@PathParam(value = "userId") String userId, String message) {
System.out.println("发送消息的人员======"+userId);
JSONObject retJo = new JSONObject(); JSONObject retJo = new JSONObject();
retJo.put("code", 1); retJo.put("code", 1);
retJo.put("msg", "连接成功"); retJo.put("msg", "连接成功");
@ -53,15 +49,13 @@ public class WebSocket {
@OnError @OnError
public void onError(Session session,@PathParam(value = "userId") String userId, Throwable throwable) { public void onError(Session session,@PathParam(value = "userId") String userId, Throwable throwable) {
String socketId = session.getId();
try { try {
WebsocketUtil.removeSession(socketId); WebsocketUtil.removeSession(userId);
if (session.isOpen()) { if (session.isOpen()) {
session.close(); session.close();
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); LogUtil.WriteLog_Error("websocket连接出错====>" + e.getMessage(), "Websocket");
} }
throwable.printStackTrace();
} }
} }

@ -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");
}
} }
/** /**

Loading…
Cancel
Save