/**
* @author weirdo_world
*/
@Controller
@Slf4j
@ServerEndpoint("/ws/task")
public class TaskSocketService {
public static TaskMessageService taskMessageService;
private static int count = 0;
/**
* 连接时执行
*/
@OnOpen
public void onOpen() {
count++;
log.info("建立websocket连接:已连接数{}", count);
}
/**
* 关闭时执行
*/
@OnClose
public void onClose() {
count--;
log.info("WebSocket连接关闭,剩余连接:{}", count);
}
/**
* 收到消息时执行
*/
@OnMessage
public String onMessage(String params) {
int count = taskMessageService.getSocketMessageCount(params);
return String.valueOf(count);
}
/**
* 连接错误时执行
*/
@OnError
public void onError(Throwable error) {
error.printStackTrace();
log.error("WebSocket连接错误");
}
}
|