| | |
| | | import javax.websocket.*; |
| | | import javax.websocket.server.ServerEndpoint; |
| | | import java.io.IOException; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.concurrent.CountDownLatch; |
| | | import java.util.concurrent.ThreadPoolExecutor; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | |
| | | /** |
| | |
| | | |
| | | private HttpSession httpSession; |
| | | |
| | | private volatile boolean runFlag = true; |
| | | |
| | | private volatile Map<String, Thread> threadMap = new HashMap<>(); |
| | | |
| | | private volatile Map<Long,Boolean> threadFlagMap = new HashMap<>(); |
| | | |
| | | @Autowired |
| | | public void setDevInfService(DevInfService dinfService) { |
| | | DevInfSocket.dinfService = dinfService; |
| | |
| | | @OnMessage |
| | | public void onMessage(Session session, String message) { |
| | | Page page= ActionUtil.getGson().fromJson(message,Page.class); |
| | | UserInf user = (UserInf) this.httpSession.getAttribute("user"); |
| | | final int userId = user.getUid(); |
| | | //final int userId = 1087; |
| | | Thread thread = new Thread() { |
| | | //UserInf user = (UserInf) this.httpSession.getAttribute("user"); |
| | | //final int userId = user.getUid(); |
| | | final int userId = 101; |
| | | thread = new Thread("Thread_DinfSocket") { |
| | | @Override |
| | | public void run() { |
| | | try { |
| | | while (!currentThread().isInterrupted()) { |
| | | Map<String, Object> res = new HashMap<>(); |
| | | while (runFlag && !isInterrupted()) { |
| | | Thread thread = currentThread(); |
| | | threadFlagMap.put(thread.getId(), true); |
| | | try { |
| | | Response response=dinfService.getAllInf(userId,page); |
| | | session.getBasicRemote().sendObject(response); |
| | | if (session.isOpen()) { |
| | | //推送信息 |
| | | synchronized (session) { |
| | | session.getBasicRemote().sendObject(new Response().set(1, response)); |
| | | } |
| | | threadFlagMap.put(thread.getId(), false); |
| | | } |
| | | sleep(4000); |
| | | } catch (Exception e) { |
| | | interrupt(); |
| | | } |
| | | } catch (Exception e) { |
| | | this.interrupt(); |
| | | } |
| | | } |
| | | }; |
| | | thread.start(); |
| | | this.thread = thread; |
| | | threadFlagMap.put(thread.getId(),true); |
| | | //停止老的socket线程 |
| | | Thread threadBefore = threadMap.get(session.getId()); |
| | | if(threadBefore !=null && threadBefore.isAlive()){ |
| | | while (threadFlagMap.get(threadBefore.getId())){ |
| | | } |
| | | threadBefore.interrupt(); |
| | | } |
| | | //将线程存储,便于调用定位 |
| | | threadMap.put(session.getId(), this.thread); |
| | | } |
| | | |
| | | |
| | | @OnClose |
| | | public void onClose(CloseReason closeReason) throws IOException { |
| | | public void onClose(CloseReason closeReason){ |
| | | System.err.println("closeReason = " + closeReason); |
| | | if(session.isOpen()){ |
| | | session.close(); |
| | | runFlag = false; |
| | | if (thread != null && thread.isAlive()) { |
| | | thread.interrupt(); |
| | | } |
| | | threadMap.remove(session.getId()); |
| | | } |
| | | |
| | | @OnError |
| | | public void onError(Throwable error) throws IOException { |
| | | public void onError(Throwable error) { |
| | | error.printStackTrace(); |
| | | thread.isInterrupted(); |
| | | if(session.isOpen()){ |
| | | session.close(); |
| | | if (thread != null && thread.isAlive()) { |
| | | thread.interrupt(); |
| | | } |
| | | threadMap.remove(session.getId()); |
| | | } |
| | | |
| | | } |