New file |
| | |
| | | package com.whyc.webSocket; |
| | | |
| | | import com.whyc.config.WebSocketConfig; |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.pojo.DocUser; |
| | | import com.whyc.service.WorksheetMainService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.servlet.http.HttpSession; |
| | | import javax.websocket.*; |
| | | import javax.websocket.server.ServerEndpoint; |
| | | import java.io.IOException; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * 工作流的状态统计 |
| | | */ |
| | | @Component |
| | | @ServerEndpoint(value = "/worksheet",encoders = WebSocketEncoder.class,configurator = WebSocketConfig.class) |
| | | public class WorksheetSocket { |
| | | |
| | | private Session session; |
| | | |
| | | private Thread thread; |
| | | private static WorksheetMainService mainService; |
| | | |
| | | @Autowired |
| | | public void setWorksheetMainService(WorksheetMainService mainService) { |
| | | WorksheetSocket.mainService = mainService; |
| | | } |
| | | |
| | | @OnOpen |
| | | public void onOpen(Session session, EndpointConfig config){ |
| | | this.session = session; |
| | | HttpSession httpSession = (HttpSession) config.getUserProperties().get("httpSession"); |
| | | |
| | | Thread thread = new Thread() { |
| | | @Override |
| | | public void run() { |
| | | try { |
| | | while (!currentThread().isInterrupted()) { |
| | | Map<String, Integer> statisticMap = mainService.getStatusStatistic((DocUser) httpSession.getAttribute("user")); |
| | | session.getBasicRemote().sendObject(new Response().set(1, statisticMap)); |
| | | sleep(2000); |
| | | } |
| | | } catch (Exception e) { |
| | | this.interrupt(); |
| | | } |
| | | } |
| | | }; |
| | | thread.start(); |
| | | this.thread = thread; |
| | | } |
| | | |
| | | @OnClose |
| | | public void onClose(CloseReason closeReason) throws IOException { |
| | | System.err.println("closeReason = " + closeReason); |
| | | thread.isInterrupted(); |
| | | if(session.isOpen()){ |
| | | this.thread.interrupt(); |
| | | session.close(); |
| | | } |
| | | } |
| | | |
| | | @OnError |
| | | public void onError(Throwable error) throws IOException { |
| | | error.printStackTrace(); |
| | | thread.isInterrupted(); |
| | | if(session.isOpen()){ |
| | | this.thread.interrupt(); |
| | | session.close(); |
| | | } |
| | | } |
| | | |
| | | } |