|
|
package com.sunyo.wlpt.cgonms.provide.websocket;
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.log.Log;
|
|
|
import cn.hutool.log.LogFactory;
|
|
|
import com.sunyo.wlpt.cgonms.provide.controller.NmsController;
|
...
|
...
|
@@ -29,13 +28,17 @@ public class WebSocketServer { |
|
|
|
|
|
static Log log = LogFactory.get(WebSocketServer.class);
|
|
|
|
|
|
//静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
|
|
|
/**
|
|
|
* 静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
|
|
|
*/
|
|
|
private static int onlineCount = 0;
|
|
|
|
|
|
//concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。
|
|
|
private static CopyOnWriteArraySet<WebSocketServer> webSocketSet = new CopyOnWriteArraySet<WebSocketServer>();
|
|
|
|
|
|
//与某个客户端的连接会话,需要通过它来给客户端发送数据
|
|
|
/**
|
|
|
* 与某个客户端的连接会话,需要通过它来给客户端发送数据
|
|
|
*/
|
|
|
private Session session;
|
|
|
|
|
|
//接收sid
|
...
|
...
|
@@ -47,8 +50,10 @@ public class WebSocketServer { |
|
|
@OnOpen
|
|
|
public void onOpen(Session session, @PathParam("sid") String sid) {
|
|
|
this.session = session;
|
|
|
webSocketSet.add(this); //加入set中
|
|
|
addOnlineCount(); //在线数加1
|
|
|
//加入set中
|
|
|
webSocketSet.add(this);
|
|
|
//在线数加1
|
|
|
addOnlineCount();
|
|
|
log.info("有新窗口开始监听:" + sid + ",当前在线人数为" + getOnlineCount());
|
|
|
this.sid = sid;
|
|
|
try {
|
...
|
...
|
@@ -64,8 +69,10 @@ public class WebSocketServer { |
|
|
*/
|
|
|
@OnClose
|
|
|
public void onClose() {
|
|
|
webSocketSet.remove(this); //从set中删除
|
|
|
subOnlineCount(); //在线数减1
|
|
|
//从set中删除
|
|
|
webSocketSet.remove(this);
|
|
|
//在线数减1
|
|
|
subOnlineCount();
|
|
|
log.info("有一连接关闭!当前在线人数为" + getOnlineCount());
|
|
|
}
|
|
|
|
...
|
...
|
@@ -76,11 +83,6 @@ public class WebSocketServer { |
|
|
*/
|
|
|
@OnMessage
|
|
|
public void onMessage(String message, Session session) {
|
|
|
// JSONObject jsonObj = new JSONObject(message);
|
|
|
// String flightNo = jsonObj.get("flightNo").toString();
|
|
|
// String flightDate = jsonObj.get("flightDate").toString();
|
|
|
|
|
|
// nmsController.getData()
|
|
|
log.info("收到来自窗口" + sid + "的信息:" + message);
|
|
|
//群发消息
|
|
|
for (WebSocketServer item : webSocketSet) {
|
...
|
...
|
@@ -114,7 +116,6 @@ public class WebSocketServer { |
|
|
* 群发自定义消息
|
|
|
*/
|
|
|
public static void sendInfo(String message, @PathParam("sid") String sid) throws IOException {
|
|
|
//log.info("推送消息到窗口"+sid+",推送内容:"+message);
|
|
|
for (WebSocketServer item : webSocketSet) {
|
|
|
try {
|
|
|
//这里可以设定只推送给这个sid的,为null则全部推送
|
...
|
...
|
|