| | |
| | | |
| | | |
| | | @OnOpen |
| | | public void onOpen(Session session){ |
| | | Thread thread = new Thread() { |
| | | public void onOpen(Session session) { |
| | | this.session = session; |
| | | } |
| | | @OnMessage |
| | | public void onMessage(Session session, String message) { |
| | | thread = new Thread("Thread_RtstateSocket") { |
| | | @Override |
| | | public void run() { |
| | | try { |
| | | while (!currentThread().isInterrupted()) { |
| | | while (runFlag && !isInterrupted()) { |
| | | Thread thread = currentThread(); |
| | | threadFlagMap.put(thread.getId(), true); |
| | | try { |
| | | Response res = getPowerInfRt(); |
| | | session.getBasicRemote().sendObject(res); |
| | | if (session.isOpen()) { |
| | | //推送信息 |
| | | synchronized (session) { |
| | | session.getBasicRemote().sendObject(res); |
| | | } |
| | | 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); |
| | | } |
| | | //实时数据 |
| | | private Response getPowerInfRt() { |