package com.zy.asrs.ws;
|
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import com.core.common.R;
|
import com.core.common.SpringUtils;
|
import com.zy.asrs.controller.CrnController;
|
import com.zy.asrs.controller.RgvController;
|
import com.zy.asrs.controller.BasMapController;
|
import com.zy.asrs.controller.ConsoleController;
|
import lombok.Data;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Service;
|
|
import javax.websocket.*;
|
import javax.websocket.server.ServerEndpoint;
|
import java.io.IOException;
|
import java.util.concurrent.CopyOnWriteArraySet;
|
|
@Component
|
@Slf4j
|
@Service
|
@ServerEndpoint("/console/websocket")
|
@Data
|
public class ConsoleWebSocket {
|
|
//客户端在线人数
|
private static int onlineClient = 0;
|
|
//客户端池
|
private static CopyOnWriteArraySet<ConsoleWebSocket> webSocketServers = new CopyOnWriteArraySet<>();
|
|
private Session session;
|
|
//是否鉴权,默认未鉴权
|
private boolean auth = false;
|
|
//是否为管理员
|
private boolean isAdmin = false;
|
|
//管道ID
|
private String sessionId;
|
|
@OnOpen
|
public void onOpen(Session session) {
|
this.session = session;
|
this.sessionId = session.getId();
|
|
//将websocket对象进行保存
|
webSocketServers.add(this);
|
//添加在线人数
|
addOnlineClient();
|
log.info("有新窗口开始监听:" + session.getId() + ",当前在线人数为:" + getOnlineClient());
|
}
|
|
/**
|
* 连接关闭调用的方法
|
*/
|
@OnClose
|
public void onClose() {
|
webSocketServers.remove(this); //从set中删除
|
subOnlineClient(); //在线数减1
|
log.info("关闭的连接:" + sessionId);
|
log.info("有一连接关闭!当前在线人数为" + getOnlineClient());
|
}
|
|
/**
|
* 收到客户端消息后调用的方法
|
* @ Param message 客户端发送过来的消息
|
*/
|
@OnMessage
|
public void onMessage(String message, Session session) throws IOException {
|
Object resObj = null;
|
String url = null;
|
try {
|
JSONObject req = JSON.parseObject(message);
|
url = req.getString("url");
|
if (url == null) {
|
return;
|
}
|
if (url.startsWith("/basMap/lev/") && url.endsWith("/auth")) {
|
String[] segs = url.split("/");
|
Integer lev = null;
|
for (int i = 0; i < segs.length; i++) {
|
if ("lev".equals(segs[i]) && i + 1 < segs.length) {
|
try {
|
lev = Integer.parseInt(segs[i + 1]);
|
} catch (Exception ignore) {}
|
break;
|
}
|
}
|
BasMapController basMapController = SpringUtils.getBean(BasMapController.class);
|
R r = basMapController.getByLev(lev);
|
resObj = r;
|
} else if ("/console/latest/data/station".equals(url)) {
|
ConsoleController consoleController = SpringUtils.getBean(ConsoleController.class);
|
resObj = consoleController.stationLatestData();
|
} else if ("/console/latest/data/crn".equals(url)) {
|
ConsoleController consoleController = SpringUtils.getBean(ConsoleController.class);
|
resObj = consoleController.crnLatestData();
|
} else if ("/console/latest/data/rgv".equals(url)) {
|
ConsoleController consoleController = SpringUtils.getBean(ConsoleController.class);
|
resObj = consoleController.rgvLatestData();
|
} else if ("/crn/table/crn/state".equals(url)) {
|
resObj = SpringUtils.getBean(CrnController.class).crnStateTable();
|
} else if ("/rgv/table/rgv/state".equals(url)) {
|
resObj = SpringUtils.getBean(RgvController.class).rgvStateTable();
|
}
|
} catch (Exception e) {
|
R err = R.error(e.getMessage());
|
JSONObject out = new JSONObject();
|
out.put("url", "error");
|
out.put("data", JSON.toJSONString(err));
|
this.sendMessage(out.toJSONString());
|
return;
|
}
|
if (resObj != null && url != null) {
|
JSONObject out = new JSONObject();
|
out.put("url", url);
|
out.put("data", JSON.toJSONString(resObj));
|
this.sendMessage(out.toJSONString());
|
}
|
}
|
|
/**
|
* @ Param session
|
* @ Param error
|
*/
|
@OnError
|
public void onError(Session session, Throwable error) {
|
log.error("发生错误");
|
error.printStackTrace();
|
}
|
|
/**
|
* 实现服务器主动推送
|
*/
|
public void sendMessage(String message) throws IOException {
|
this.session.getBasicRemote().sendText(message);
|
}
|
|
/**
|
* 服务器主动推送给指定用户
|
*/
|
public void sendMessage(String message, String account) throws IOException {
|
for (ConsoleWebSocket item : webSocketServers) {
|
item.sendMessage(message);
|
}
|
}
|
|
public void sendMessage(String message, int userId) throws IOException {
|
for (ConsoleWebSocket item : webSocketServers) {
|
item.sendMessage(message);
|
}
|
}
|
|
/**
|
* 服务器主动推送给指定用户
|
*/
|
public static boolean sendMessageGlobal(String message, String account) throws IOException {
|
boolean tag = false;
|
for (ConsoleWebSocket item : webSocketServers) {
|
tag = true;
|
item.sendMessage(message);
|
}
|
return tag;
|
}
|
|
public static boolean sendMessageGlobal(String message, int userId) throws IOException {
|
boolean tag = false;
|
for (ConsoleWebSocket item : webSocketServers) {
|
tag = true;
|
item.sendMessage(message);
|
}
|
return tag;
|
}
|
|
public static synchronized int getOnlineClient() {
|
return onlineClient;
|
}
|
|
public static synchronized void addOnlineClient() {
|
ConsoleWebSocket.onlineClient++;
|
}
|
|
public static synchronized void subOnlineClient() {
|
if (ConsoleWebSocket.onlineClient > 0) {
|
ConsoleWebSocket.onlineClient--;
|
}
|
}
|
|
}
|