| | |
| | | package org.springblade.common.handler; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.corundumstudio.socketio.*; |
| | | import com.corundumstudio.socketio.annotation.OnConnect; |
| | | import com.corundumstudio.socketio.annotation.OnDisconnect; |
| | | import com.corundumstudio.socketio.annotation.OnEvent; |
| | | import com.corundumstudio.socketio.protocol.Packet; |
| | | import org.springblade.core.tool.api.R; |
| | | import org.springblade.modules.modules.entity.FunctionEntity; |
| | | import org.springblade.modules.modules.service.IFunctionService; |
| | | import org.springblade.modules.modules.vo.FunctionVO; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.boot.CommandLineRunner; |
| | | import org.springframework.core.annotation.Order; |
| | |
| | | import javax.annotation.PostConstruct; |
| | | import javax.annotation.PreDestroy; |
| | | import java.net.ServerSocket; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | |
| | | // 用来存已连接的客户端 |
| | | 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; |
| | | |
| | | /** |
| | | * Spring IoC容器创建之后,在加载SocketIOConfiguration Bean之后启动 |
| | |
| | | */ |
| | | @OnConnect |
| | | public void onConnect(SocketIOClient client){ |
| | | log.info(getIpByClient(client)); |
| | | HandshakeData handshakeData = client.getHandshakeData(); |
| | | Map<String, List<String>> urlParams = handshakeData.getUrlParams(); |
| | | 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); |
| | | 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 ackRequest |
| | | * @param data |
| | | */ |
| | | @OnEvent(value = "msg2") |
| | | public void OnEventMsg2(SocketIOClient client, AckRequest ackRequest, String data){ |
| | | log.info("发来消息:" + data); |
| | | client.sendEvent("ClientReceive","copy"+data); |
| | | @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 = "menuChange") |
| | | 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); |
| | | }); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | @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); |
| | | } |
| | | } |
| | | }else { |
| | | for (int i = 0; i <=controller.size() ; i++) { |
| | | if (controller.get(i).containsKey(funcVo.getModulesId())) { |
| | | controller.remove(i); |
| | | } |
| | | } |
| | | } |
| | | log.info("{}断开连接",client.getSessionId()); |
| | | } |
| | | |
| | |
| | | return sa.substring(1, sa.indexOf(":")); |
| | | } |
| | | |
| | | private FunctionVO getFuncVo(SocketIOClient client){ |
| | | String connectInfo = client.getHandshakeData().getSingleUrlParam("connectInfo"); |
| | | return JSON.parseObject(connectInfo, FunctionVO.class); |
| | | } |
| | | |
| | | |
| | | } |