package com.whyc.webSocket;
|
|
import com.whyc.config.WebSocketConfig;
|
import com.whyc.dto.NameValueIntDto;
|
import com.whyc.dto.Response;
|
import com.whyc.pojo.db_user.User;
|
import com.whyc.service.LeaderHomeService;
|
import com.whyc.util.JsonUtil;
|
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;
|
|
/**
|
* 领导层首页 子模块
|
* 同品牌同时间
|
*/
|
@Component
|
@ServerEndpoint(value = "/leaderHome/BattSameTimeSameBrand",encoders = WebSocketEncoder.class,configurator = WebSocketConfig.class)
|
public class LeaderHomeSubModule1Socket {
|
|
private Session session;
|
|
private Thread thread;
|
|
private static HttpSession httpSession;
|
private static LeaderHomeService leaderHomeService;
|
|
@Autowired
|
public void setLeaderHomeService(LeaderHomeService leaderHomeService) {
|
LeaderHomeSubModule1Socket.leaderHomeService = leaderHomeService;
|
}
|
|
@OnOpen
|
public void onOpen(Session session, EndpointConfig config){
|
this.session = session;
|
httpSession = (HttpSession) config.getUserProperties().get("httpSession");
|
}
|
|
@OnMessage
|
public void onMessage(Session session, String message) {
|
//value为投运时间,年为单位
|
//name为品牌
|
NameValueIntDto nameValueDto = JsonUtil.getGson().fromJson(message, NameValueIntDto.class);
|
User user = (User) httpSession.getAttribute("user");
|
Integer userId = user.getId();
|
Thread thread = new Thread() {
|
@Override
|
public void run() {
|
try {
|
while (!currentThread().isInterrupted()) {
|
Response response = leaderHomeService.getBattPerformanceOfSameTimeSameBrand(userId, nameValueDto);
|
session.getBasicRemote().sendObject(new Response().set(1, response));
|
sleep(4000);
|
}
|
} catch (Exception e) {
|
this.interrupt();
|
}
|
}
|
};
|
thread.start();
|
this.thread = thread;
|
}
|
|
@OnClose
|
public void onClose(CloseReason closeReason) throws IOException {
|
System.err.println("closeReason = " + closeReason);
|
if(session.isOpen()){
|
session.close();
|
}
|
}
|
|
@OnError
|
public void onError(Throwable error) throws IOException {
|
error.printStackTrace();
|
thread.isInterrupted();
|
if(session.isOpen()){
|
session.close();
|
}
|
}
|
|
}
|