功能描述:希望PC页面和平板进行一个关联,但是PC无法直连平板,就需要服务器提供一个websocket支持,由PC、平板均以websocket和服务器进行连接,服务器当中中间件,进行消息转发。
- 平板和pc进行绑定,建立平板、pc绑定表,equipment_bind表
- equipment_bind表对应的CRUD操作。
- PC断开后,平板自动断开
- 心跳机制,服务器判断websocket是否断开。
- 基础的websocket功能
@OnMessage
@OnClose
@OnOpen
@OnError
部分代码段:
开启 定时心跳任务,判断是否有超时连接
@PostConstruct
public void init() {
log.info("定时程序启动,心跳监听开始");
Timer timer = new Timer();
HeartbeatTask task = new HeartbeatTask();
// 表示在0秒之后开始执行,并且每45秒执行一次
timer.schedule(task, 0, 45000);
}
package com.demo.websocket;
import java.util.Date;
import java.util.Map;
import java.util.TimerTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HeartbeatTask extends TimerTask {
private static Logger log = LoggerFactory.getLogger(HeartbeatTask.class);
public void run() {
for (Map.Entry<String, Date> entry : AppSignWebSocket.statusManager.entrySet()) {
System.out.println("当前连接id===" + entry.getKey() + "==心跳时间===" + entry.getValue());
if (isBreak(entry.getValue())) {
// 如果已断开,则移除
log.info("id为" + entry.getKey() + "的连接已断开!移除此id的连接!");
AppSignWebSocket.statusManager.remove(entry.getKey());
AppSignWebSocket.webSocketSet.remove(entry.getKey());
}
}
}
public Boolean isBreak(Date startDate) {
long nowTime = new Date().getTime();
long heartBeatTime = startDate.getTime();
int timeDifference = (int) ((nowTime - heartBeatTime) / 1000);
if (timeDifference > 30) {
// 连接已断开!
return true;
} else {
return false;
}
}
}
开启连接
@OnOpen
public void OnOpen(Session session, @PathParam("operatorId") String operatorId) throws InterruptedException {
log.info("OnOpen接口调用当前websocket连接数量为={}", Integer.valueOf(webSocketSet.size()));
this.session = session;
this.operatorId = operatorId;
System.out.println("用户登录" + operatorId);
statusManager.put(operatorId, new Date());
webSocketSet.put(operatorId, this);
log.info("操作员ID为" + operatorId + " 连接成功,当前连接人数为:={}", Integer.valueOf(webSocketSet.size()));
}
服务器收到消息后转发
@OnMessage
public String OnMessage(String message) {
log.info("[WebSocket] 收到消息:{}", message);
JSONObject jo = new JSONObject();
jo = JSONObject.parseObject(message);
JSONObject messageResult = new JSONObject();
if (jo.containsKey("a")) {
log.info("收到id为" + this.operatorId + "发来的心跳消息,刷新心跳时间");
statusManager.put(this.operatorId, new Date());
messageResult.put("msgType", "99");
// webSocketSet.put(this.operatorId, this);
} else if (jo.containsKey("recipientId")) {
String recipientId = jo.getString("recipientId");
try {
String msgType = "1";
if (jo.containsKey("msgType")) {
msgType = jo.get("msgType").toString();
}
log.info("签字板websocket转发消息");
jo.put("msgType", msgType);
AppointSending(recipientId, jo.toJSONString());
} catch (Exception e) {
messageResult.put("msgType", "0");
messageResult.put("type", "0");
messageResult.put("message", e.getMessage());
}
}
// pc登录后发送msgType=8,然后签字板自动登录
if (jo.containsKey("msgType") && MSG_TYPE_AUTO_LOGIN.equals(jo.getString("msgType"))) {
// this.zxbm = jo.getString("zxbm");
// 查询PC绑定的平板mac地址
PtCommentEquipmentBindBean ptEquipmentBindBean = new PtCommentEquipmentBindBean();
ptEquipmentBindBean.setPcMacUrl(this.operatorId);
try {
List resultList = service.queryEquipmentBindList2(ptEquipmentBindBean);
log.info("查询设备绑定列表结果===" + resultList.toString());
if (resultList.size() > 0) {
PtCommentEquipmentBindResultBean equipment = (PtCommentEquipmentBindResultBean) resultList.get(0);
String signPadMacUrl = equipment.getSignPadMacUrl();
String callPadMacUrl = equipment.getCallPadMacUrl();
try {
AppointSending(signPadMacUrl, jo.toString());
} catch (Exception e) {
log.error("向签字平板发送自动登录消息失败!");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
if ((jo.containsKey("msgType")) && ("3".equals(jo.getString("msgType"))) && (!jo.containsKey("recipientId"))) {
PtCommentEquipmentBindBean ptEquipmentBindBean = new PtCommentEquipmentBindBean();
ptEquipmentBindBean.setPcMacUrl(this.operatorId);
try {
List resultList = service.queryEquipmentBindList2(ptEquipmentBindBean);
log.info("查询设备绑定列表结果===" + resultList.toString());
if (resultList.size() > 0) {
PtCommentEquipmentBindResultBean equipment = (PtCommentEquipmentBindResultBean) resultList.get(0);
String signPadMacUrl = equipment.getSignPadMacUrl();
try {
AppointSending(signPadMacUrl, jo.toString());
} catch (Exception e) {
log.error("向签字平板发送消息打开评价页面失败!");
messageResult.put("msgType", MSG_TYPE_RESPONSE);
messageResult.put("type", SEND_MSG_RESULT_TYPE_FAIL);
messageResult.put("message", e.getMessage());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
if ((jo.containsKey("msgType")) && ("6".equals(jo.getString("msgType"))) && (!jo.containsKey("recipientId"))) {
PtCommentEquipmentBindBean ptEquipmentBindBean = new PtCommentEquipmentBindBean();
ptEquipmentBindBean.setSignPadMacUrl(this.operatorId);
try {
List resultList = service.queryEquipmentBindList2(ptEquipmentBindBean);
log.info("查询设备绑定列表结果===" + resultList.toString());
if (resultList.size() > 0) {
PtCommentEquipmentBindResultBean equipment = (PtCommentEquipmentBindResultBean) resultList.get(0);
String pcMacUrl = equipment.getPcMacUrl();
try {
AppointSending(pcMacUrl, jo.toString());
} catch (Exception e) {
log.error("签字板请求PC,获取登录信息失败!");
messageResult.put("msgType", MSG_TYPE_RESPONSE);
messageResult.put("type", SEND_MSG_RESULT_TYPE_FAIL);
messageResult.put("message", e.getMessage());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
if ((jo.containsKey("senderId")) && (jo.getString("senderId").equals(this.operatorId))) {
try {
messageResult.put("msgType", "0");
messageResult.put("type", "1");
messageResult.put("message", "发送成功");
// AppointSending(this.operatorId,
// messageResult.toJSONString());
} catch (Exception e) {
log.error(e.getMessage());
}
}
return messageResult.toJSONString();
}
具体项目已上传github