| | |
| | | package com.whyc.webSocket; |
| | | |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.config.WebSocketConfig; |
| | | import com.whyc.service.BattalarmDataService; |
| | | 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.Map; |
| | | |
| | | @Component |
| | | @ServerEndpoint(value = "/battAlarmFoot",encoders = WebSocketEncoder.class) |
| | | @ServerEndpoint(value = "/battAlarmFoot", encoders = WebSocketEncoder.class, configurator = WebSocketConfig.class) |
| | | public class BattAlarmFootSocket { |
| | | private volatile Thread thread; |
| | | |
| | |
| | | |
| | | private Session session; |
| | | |
| | | private HttpSession httpSession; |
| | | |
| | | @Autowired |
| | | public void setService(BattalarmDataService service){ |
| | | BattAlarmFootSocket.service = service; |
| | | } |
| | | |
| | | @OnOpen |
| | | public void onOpen(Session session){ |
| | | public void onOpen(Session session, EndpointConfig config) { |
| | | this.session=session; |
| | | this.httpSession = (HttpSession) config.getUserProperties().get("httpSession"); |
| | | } |
| | | |
| | | @OnMessage |
| | | public synchronized void onMessage(Session session, String uId){ |
| | | String lang = (String) this.httpSession.getAttribute("lang"); |
| | | thread = new Thread("Thread_battAlarmFoot") { |
| | | @Override |
| | | public void run() { |
| | | while (runFlag && !isInterrupted()) { |
| | | Thread thread = currentThread(); |
| | |
| | | if (session.isOpen()) { |
| | | //推送信息 |
| | | synchronized (session) { |
| | | session.getBasicRemote().sendObject(service.getRealAlarm(Integer.parseInt(uId))); |
| | | session.getBasicRemote().sendObject(service.getRealAlarm(Integer.parseInt(uId), lang)); |
| | | } |
| | | threadFlagMap.put(thread.getId(),false); |
| | | } |