|
@@ -7,6 +7,7 @@ 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.concurrent.ConcurrentHashMap;
|
|
|
|
|
@@ -136,7 +137,7 @@ public class WebSocket {
|
|
|
synchronized (session){
|
|
|
if(session.isOpen())
|
|
|
{
|
|
|
- session.getBasicRemote().sendObject(message);
|
|
|
+ session.getBasicRemote().sendText(message);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -147,50 +148,78 @@ public class WebSocket {
|
|
|
/**
|
|
|
* 向客户端发送消息
|
|
|
*/
|
|
|
- public synchronized void sendMessage(Object message) throws IOException, EncodeException {
|
|
|
+ public void sendMessage(Object message) throws IOException, EncodeException {
|
|
|
|
|
|
try {
|
|
|
- if(StringUtils.notEmp(session))
|
|
|
+ if(StringUtils.notEmp(session) && session.isOpen())
|
|
|
{
|
|
|
|
|
|
synchronized (session){
|
|
|
|
|
|
- if( session.isOpen())
|
|
|
- {
|
|
|
- session.getBasicRemote().sendObject(message);
|
|
|
- }
|
|
|
+ session.getBasicRemote().sendObject(message);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}catch (Exception e)
|
|
|
{
|
|
|
- logger.error("用户错误:" + this.userId + ",原因:" + e.getStackTrace());
|
|
|
-// if(StringUtils.notEmp(userId) && webSocketMap.containsKey(userId))
|
|
|
-// {
|
|
|
-// webSocketMap.remove(userId);
|
|
|
-// subOnlineCount(); //在线数减1
|
|
|
-// logger.info("用户{}关闭连接!当前在线人数为{}", userId, getOnlineCount());
|
|
|
-// }
|
|
|
-//
|
|
|
+ logger.error("原因:" +getStackMsg(e));
|
|
|
// try {
|
|
|
-// if(StringUtils.notEmp(session))
|
|
|
+// if(StringUtils.notEmp(session) )
|
|
|
// {
|
|
|
-// session.close();
|
|
|
-// logger.info("用户错误:" + this.userId + ",关闭session:" );
|
|
|
+// if( session.isOpen())
|
|
|
+// {
|
|
|
+// session.close();
|
|
|
+// logger.info( "关闭session:" );
|
|
|
+// }else
|
|
|
+// {
|
|
|
+// logger.info( "session:已经关闭" );
|
|
|
+// }
|
|
|
// }else
|
|
|
// {
|
|
|
-// logger.info( ",session:null" );
|
|
|
+// logger.info( "session:null" );
|
|
|
// }
|
|
|
// }catch (IOException io)
|
|
|
// {
|
|
|
-// logger.error("用户错误:" + this.userId + ",原因:" + io.getMessage());
|
|
|
+// logger.error("原因:" +getStackMsg(io));
|
|
|
// }
|
|
|
+// logger.error("原因:" + getStackMsg(e));
|
|
|
+//
|
|
|
+// if(StringUtils.notEmp(userId) && webSocketMap.containsKey(userId))
|
|
|
+// {
|
|
|
+// webSocketMap.remove(userId);
|
|
|
+// subOnlineCount(); //在线数减1
|
|
|
+// logger.info("用户{}关闭连接!当前在线人数为{}", userId, getOnlineCount());
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|
|
|
//this.session.getAsyncRemote().sendText(message);
|
|
|
}
|
|
|
|
|
|
+ private static String getStackMsg(Exception e) {
|
|
|
+
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ StackTraceElement[] stackArray = e.getStackTrace();
|
|
|
+ for (int i = 0; i < stackArray.length; i++) {
|
|
|
+ StackTraceElement element = stackArray[i];
|
|
|
+ sb.append(element.toString() + "\n");
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String getStackMsg(Throwable e) {
|
|
|
+
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ StackTraceElement[] stackArray = e.getStackTrace();
|
|
|
+ for (int i = 0; i < stackArray.length; i++) {
|
|
|
+ StackTraceElement element = stackArray[i];
|
|
|
+ sb.append(element.toString() + "\n");
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 通过userId向客户端发送消息
|
|
|
*/
|