| | |
| | | import com.corundumstudio.socketio.protocol.Packet; |
| | | import org.springblade.core.mp.support.Condition; |
| | | import org.springblade.core.tool.api.R; |
| | | import org.springblade.core.tool.utils.StringUtil; |
| | | import org.springblade.modules.modules.entity.FunctionEntity; |
| | | import org.springblade.modules.modules.entity.ModulesEntity; |
| | | import org.springblade.modules.modules.service.IFunctionService; |
| | |
| | | import javax.annotation.PreDestroy; |
| | | import java.net.ServerSocket; |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | |
| | | |
| | | private final Logger log = LoggerFactory.getLogger(this.getClass()); |
| | | |
| | | // 用来存已连接的客户端 |
| | | private static Map<String, SocketIOClient> viewMap = new ConcurrentHashMap<>(); |
| | | |
| | | // 用来存已连接的客户端 |
| | | private static Map<String, SocketIOClient> controllerMap = new ConcurrentHashMap<>(); |
| | | |
| | | // // 用来存已连接的视图客户端 |
| | | // private List<Map<String, SocketIOClient>> view = new ArrayList<>(); |
| | | // |
| | | // // 用来存已连接的控制客户端 |
| | | // private List<Map<String, SocketIOClient>> controller = new ArrayList<>(); |
| | | private static Map<String, SocketIOClient> clientMap = new ConcurrentHashMap<>(); |
| | | |
| | | @Autowired |
| | | private SocketIOServer socketIOServer; |
| | | |
| | | @Autowired |
| | | private IFunctionService functionService; |
| | | |
| | | @Autowired |
| | | private IModulesService modulesService; |
| | | |
| | | /** |
| | | * Spring IoC容器创建之后,在加载SocketIOConfiguration Bean之后启动 |
| | | * |
| | | * @throws Exception |
| | | */ |
| | | @PostConstruct |
| | | private void autoStartup() throws Exception { |
| | |
| | | |
| | | /** |
| | | * Spring IoC容器在销毁SocketIOConfiguration Bean之前关闭,避免重启项目服务端口占用问题 |
| | | * |
| | | * @throws Exception |
| | | */ |
| | | @PreDestroy |
| | | private void autoStop() throws Exception { |
| | |
| | | |
| | | /** |
| | | * 连接时 |
| | | * |
| | | * @param client |
| | | */ |
| | | @OnConnect |
| | | public void onConnect(SocketIOClient client) { |
| | | FunctionVO functionEntity = getFuncVo(client); |
| | | String connectInfo = client.getHandshakeData().getSingleUrlParam("connectInfo"); |
| | | SocketEntity socketEntity = parseMsg(connectInfo); |
| | | |
| | | //判断页面和控制器是否已经连接过了 |
| | | if (functionEntity.getIsView()) { |
| | | for (int i = 0; i < viewMap.size(); i++) { |
| | | if (viewMap.containsKey(functionEntity.getModulesId())) { |
| | | client.sendEvent("connectError", R.fail("已连接")); |
| | | return; |
| | | } |
| | | } |
| | | } else { |
| | | for (int i = 0; i < controllerMap.size(); i++) { |
| | | if (controllerMap.containsKey(functionEntity.getModulesId())) { |
| | | client.sendEvent("connectError", R.fail("已连接")); |
| | | return; |
| | | } |
| | | } |
| | | } |
| | | |
| | | List<FunctionEntity> all = functionService.getAll(functionEntity); |
| | | if (all.size() > 0) { |
| | | if (functionEntity.getIsView()) { |
| | | log.info("预览页{}", client.getSessionId().toString()); |
| | | viewMap.put(functionEntity.getModulesId(), client); |
| | | |
| | | } else { |
| | | log.info("控制页{}", client.getSessionId().toString()); |
| | | controllerMap.put(functionEntity.getModulesId(), client); |
| | | } |
| | | client.sendEvent("connectOk", R.data(all)); |
| | | } else { |
| | | client.sendEvent("connectError", R.fail("连接失败")); |
| | | if (clientMap.containsKey(socketEntity.getCurrent())){ |
| | | client.sendEvent("connectError",R.fail("连接已存在")); |
| | | log.info("设备{}连接已存在",socketEntity.getCurrent()); |
| | | }else { |
| | | clientMap.put(socketEntity.getCurrent(),client); |
| | | client.sendEvent("connectOk",R.success("连接成功")); |
| | | log.info("设备{}连接成功",socketEntity.getCurrent()); |
| | | } |
| | | } |
| | | |
| | |
| | | @OnEvent(value = "changeImgPage") |
| | | public void OnEventImgChange(SocketIOClient client, AckRequest ackRequest, String data) { |
| | | log.info("图册变更:" + data); |
| | | FunctionVO funcVo = getFuncVo(client); |
| | | |
| | | |
| | | viewMap.forEach((e,socketIO)->{ |
| | | if (data.equals("previous")){ |
| | | if (e.equals(funcVo.getModulesId())){ |
| | | socketIO.sendEvent("previousPage","previous"); |
| | | } |
| | | }else if (data.equals("next")){ |
| | | if (e.equals(funcVo.getModulesId())){ |
| | | socketIO.sendEvent("previousPage","next"); |
| | | } |
| | | } |
| | | }); |
| | | SocketEntity socketEntity = parseMsg(data); |
| | | if (socketEntity.getMsg().equals("previous")) { |
| | | //上一页 |
| | | clientMap.get(socketEntity.getTarget()).sendEvent("previousPage", "previous"); |
| | | } else { |
| | | //下一页 |
| | | clientMap.get(socketEntity.getTarget()).sendEvent("nextPage", "next"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | @OnEvent(value = "menuChange") |
| | | public void OnEventMenuChange(SocketIOClient client, AckRequest ackRequest, String data) { |
| | | log.info("菜单变更:" + data); |
| | | FunctionVO funcVo = getFuncVo(client); |
| | | viewMap.forEach((e,socketIOClient)->{ |
| | | if (e.equals(funcVo.getModulesId())){ |
| | | log.info("目标客户端:{}", socketIOClient.getSessionId().toString()); |
| | | socketIOClient.sendEvent("menuChange", data); |
| | | } |
| | | }); |
| | | SocketEntity socketEntity = parseMsg(data); |
| | | log.info("控制的设备是:{}", parseMsg(client.getHandshakeData().getSingleUrlParam("connectInfo")).getTarget()); |
| | | clientMap.get(socketEntity.getTarget()).sendEvent("menuChange", socketEntity.getMsg()); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | @OnDisconnect() |
| | | public void OnEvent(SocketIOClient client) { |
| | | FunctionVO funcVo = getFuncVo(client); |
| | | if (funcVo.getIsView()) { |
| | | viewMap.forEach((e,socketIOClient)->{ |
| | | if (e.equals(funcVo.getModulesId())){ |
| | | viewMap.remove(e); |
| | | log.info("预览页:{}断开连接", socketIOClient.getSessionId()); |
| | | } |
| | | }); |
| | | } else { |
| | | controllerMap.forEach((e,socketIOClient)->{ |
| | | if (e.equals(funcVo.getModulesId())){ |
| | | controllerMap.remove(e); |
| | | log.info("控制页:{}断开连接", socketIOClient.getSessionId()); |
| | | } |
| | | }); |
| | | } |
| | | String connectInfo = client.getHandshakeData().getSingleUrlParam("connectInfo"); |
| | | SocketEntity socketEntity = parseMsg(connectInfo); |
| | | //保证断连只会断连自己,不会断连其他客户端 |
| | | clientMap.forEach((key,value)->{ |
| | | if (value.getSessionId().equals(client.getSessionId())){ |
| | | clientMap.remove(key); |
| | | } |
| | | }); |
| | | log.info("设备{}断开连接",socketEntity.getCurrent()); |
| | | } |
| | | |
| | | public void stop() { |
| | |
| | | return sa.substring(1, sa.indexOf(":")); |
| | | } |
| | | |
| | | private FunctionVO getFuncVo(SocketIOClient client) { |
| | | String connectInfo = client.getHandshakeData().getSingleUrlParam("connectInfo"); |
| | | return JSON.parseObject(connectInfo, FunctionVO.class); |
| | | /** |
| | | * 解析参数,转化为实体类 |
| | | */ |
| | | private SocketEntity parseMsg(String msg) { |
| | | return JSON.parseObject(msg, SocketEntity.class); |
| | | } |
| | | |
| | | |