| | |
| | | import com.corundumstudio.socketio.annotation.OnDisconnect; |
| | | import com.corundumstudio.socketio.annotation.OnEvent; |
| | | 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 org.springblade.modules.modules.service.IModulesService; |
| | | import org.springblade.modules.modules.vo.FunctionVO; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.boot.CommandLineRunner; |
| | |
| | | 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> clientMap = new ConcurrentHashMap<>(); |
| | | |
| | | // 用来存已连接的视图客户端 |
| | | private List<Map<String, SocketIOClient>> view = new ArrayList<>(); |
| | | |
| | | // 用来存已连接的控制客户端 |
| | | private List<Map<String, SocketIOClient>> controller = new ArrayList<>(); |
| | | |
| | | @Autowired |
| | | private SocketIOServer socketIOServer; |
| | | |
| | | @Autowired |
| | | private IFunctionService functionService; |
| | | |
| | | @Autowired |
| | | private IModulesService modulesService; |
| | | |
| | | /** |
| | | * Spring IoC容器创建之后,在加载SocketIOConfiguration Bean之后启动 |
| | |
| | | |
| | | /** |
| | | * 连接时 |
| | | * |
| | | * @param client |
| | | */ |
| | | @OnConnect |
| | | public void onConnect(SocketIOClient client){ |
| | | FunctionVO functionEntity = getFuncVo(client); |
| | | |
| | | //判断页面和控制器是否已经连接过了 |
| | | if (functionEntity.getIsView()){ |
| | | for (int i = 0; i < view.size(); i++) { |
| | | if (view.get(i).containsKey(functionEntity.getModulesId())){ |
| | | client.sendEvent("connectError",R.fail("已连接")); |
| | | return; |
| | | } |
| | | } |
| | | }else { |
| | | for (int i = 0; i < controller.size(); i++) { |
| | | if (controller.get(i).containsKey(functionEntity.getModulesId())){ |
| | | client.sendEvent("connectError",R.fail("已连接")); |
| | | return; |
| | | } |
| | | } |
| | | } |
| | | |
| | | List<FunctionEntity> all = functionService.getAll(functionEntity); |
| | | public void onConnect(SocketIOClient client) { |
| | | log.info(client.getSessionId().toString()); |
| | | if (all.size()>0){ |
| | | clientMap.put(functionEntity.getModulesId(),client); |
| | | if (functionEntity.getIsView()){ |
| | | view.add(clientMap); |
| | | }else { |
| | | controller.add(clientMap); |
| | | } |
| | | client.sendEvent("connectOk", R.data(all)); |
| | | }else { |
| | | client.sendEvent("connectError",R.fail("连接失败")); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 监听前端订阅相同事件发送过来的信息 |
| | | * |
| | | * @param client |
| | | * @param ackRequest |
| | | * @param data |
| | | */ |
| | | @OnEvent(value = "msg") |
| | | public void OnEvent(SocketIOClient client, AckRequest ackRequest, String data){ |
| | | public void OnEvent(SocketIOClient client, AckRequest ackRequest, String data) { |
| | | log.info("发来消息:" + data); |
| | | } |
| | | |
| | | /** |
| | | * 监听前端订阅相同事件发送过来的信息 |
| | | * @param client |
| | | * @param ackRequest |
| | | * @param data |
| | | * 添加进clientMap |
| | | */ |
| | | @OnEvent(value = "changeImgPage") |
| | | public void OnEventImgChange(SocketIOClient client, AckRequest ackRequest, String data){ |
| | | log.info("图册变更:" + data); |
| | | FunctionVO funcVo = getFuncVo(client); |
| | | view.forEach(e->{ |
| | | if (data.equals("previous")){ |
| | | e.get(funcVo.getModulesId()).sendEvent("previousPage","previous"); |
| | | }else if (data.equals("next")){ |
| | | e.get(funcVo.getModulesId()).sendEvent("nextPage","next"); |
| | | } |
| | | }); |
| | | @OnEvent(value = "putInClientMap") |
| | | public void OnEventPutInClientMap(SocketIOClient client, AckRequest ackRequest, String data) { |
| | | FunctionEntity functionEntity = new FunctionEntity(); |
| | | |
| | | SocketEntity socketEntity = parseMsg(data); |
| | | //判断是否是控制器 |
| | | if (StringUtil.isBlank(socketEntity.getTarget())) { |
| | | //媒体端 |
| | | functionEntity.setModulesId(socketEntity.getCurrent()); |
| | | } else { |
| | | //控制器 |
| | | functionEntity.setModulesId(socketEntity.getTarget()); |
| | | } |
| | | |
| | | List<FunctionEntity> all = functionService.getAll(functionEntity); |
| | | if (all.size() > 0) { |
| | | clientMap.put(socketEntity.getCurrent(), client); |
| | | client.sendEvent("connectOk", R.data(all)); |
| | | } else { |
| | | client.sendEvent("connectError", R.fail("该模块不存在")); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 图册上下页控制 |
| | | */ |
| | | @OnEvent(value = "changeImgPage") |
| | | public void OnEventImgChange(SocketIOClient client, AckRequest ackRequest, String data) { |
| | | log.info("图册变更:" + data); |
| | | 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){ |
| | | public void OnEventMenuChange(SocketIOClient client, AckRequest ackRequest, String data) { |
| | | log.info("菜单变更:" + data); |
| | | FunctionVO funcVo = getFuncVo(client); |
| | | view.forEach(e->{ |
| | | e.get(funcVo.getModulesId()).sendEvent("menuChange",data); |
| | | }); |
| | | SocketEntity socketEntity = parseMsg(data); |
| | | log.info("目标客户端:{}", clientMap.get(socketEntity.getTarget()).getSessionId().toString()); |
| | | clientMap.get(socketEntity.getTarget()).sendEvent("menuChange", socketEntity.getMsg()); |
| | | } |
| | | |
| | | /** |
| | | * 断开连接时 |
| | | * |
| | | * @param client |
| | | */ |
| | | @OnDisconnect() |
| | | public void OnEvent(SocketIOClient client){ |
| | | |
| | | |
| | | FunctionVO funcVo = getFuncVo(client); |
| | | if (funcVo.getIsView()){ |
| | | for (int i = 0; i <=view.size() ; i++) { |
| | | if (view.get(i).containsKey(funcVo.getModulesId())) { |
| | | view.remove(i); |
| | | } |
| | | public void OnEvent(SocketIOClient client) { |
| | | clientMap.forEach((e, socketIOClient) -> { |
| | | if (socketIOClient.equals(client)) { |
| | | clientMap.remove(e); |
| | | log.info("{}断开连接", e); |
| | | } |
| | | }else { |
| | | for (int i = 0; i <=controller.size() ; i++) { |
| | | if (controller.get(i).containsKey(funcVo.getModulesId())) { |
| | | controller.remove(i); |
| | | } |
| | | } |
| | | } |
| | | log.info("{}断开连接",client.getSessionId()); |
| | | }); |
| | | } |
| | | |
| | | 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); |
| | | } |
| | | |
| | | |