package com.zykj.hbaseAdmin.socket;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class WebSocketEndpoint extends TextWebSocketHandler {
/**
* 在线客户端集合
*/
public static List<WebSocketSession> socketSessions = new ArrayList<WebSocketSession>();
/**
* 在线人数
*/
public static int onlineCount = 0;
/**
* 客户端的session
*/
private WebSocketSession webSocketSession;
/**
* 处理 收到的消息
* @param session
* @param message
* @throws Exception
*/
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
super.handleTextMessage(session,message);
TextMessage returnMessage = new TextMessage(message.getPayload() + "received at server");
session.sendMessage(returnMessage);
startTask();
}
/**
* 连接成功后的操作
* @param session
* @throws Exception
*/
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
super.afterConnectionEstablished(session);
this.webSocketSession = session;
// WebSocketEndpoint.webSocketEndpoints.add(this);
WebSocketEndpoint.socketSessions.add(session);
WebSocketEndpoint.onlineCount++;
String id = session.getId();
String s = "val|id=" + id; //变量类型 的消息,
this.send(s);
}
/**
* 连接关闭后到操作
* @param session
* @param status
* @throws Exception
*/
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
super.afterConnectionClosed(session, status);
// WebSocketEndpoint.webSocketEndpoints.remove(this);
WebSocketEndpoint.socketSessions.remove(this.webSocketSession);
WebSocketEndpoint.onlineCount--;
}
/**
* 获取在线人数
* @return
*/
public static int getOnlineCount() {
return WebSocketEndpoint.onlineCount;
}
/**
* 群发消息
* @param message
*/
public static void sendAll(String message) {
TextMessage textMessage = new TextMessage(message);
for (WebSocketSession socketSession : WebSocketEndpoint.socketSessions) {
try {
if (socketSession.isOpen()) {
socketSession.sendMessage(textMessage);
}
} catch (IOException e) {
// e.printStackTrace();
continue;
}
}
}
/**
* 发送消息到指定id的客户端
* @param type 消息类型 msg-普通消息,cmd-命令,val-变量
* @param id 客户端id
* @param data 消息内容
* @throws IOException
*/
public static void sendWithId(String type,String id,String data) throws IOException {
WebSocketSession webSocketSession = WebSocketEndpoint.findSessionById(id);
if (webSocketSession != null) {
String message = "";
if (type.equals("msg")) {
message = "msg|" + data;
} else if (type.equals("cmd")) {
message = "cmd|" + data;
} else if (type.equals("val")) {
message = "val|" + data;
}
//完整的消息结构为: "type|data" ,例如普通消息为 "msg|现在是2017年4月6日15:48:20",命令消息 为 "cmd|close",变量消息为 "val|a=123"
TextMessage textMessage = new TextMessage(message);
webSocketSession.sendMessage(textMessage);
}
}
/**
* 查找客户端webSocketSession
* @param id
* @return
*/
public static WebSocketSession findSessionById(String id){
for (WebSocketSession webSocketSession : WebSocketEndpoint.socketSessions) {
if (id.equals(webSocketSession.getId())) {
return webSocketSession;
}
}
return null;
}
/**
* 创建 消息
* @param type 消息类型 msg-普通消息,cmd-命令,val-变量
* @param data 消息内容
* @return
*/
public static TextMessage createTextMessage(String type, String data) {
if (type.equals("val")) {
return new TextMessage(type + "|" + data);
}
return new TextMessage(type + "|" + new Date(System.currentTimeMillis()).toString() + " " + data);
}
private void send(String message) throws IOException {
TextMessage textMessage = new TextMessage(message);
this.webSocketSession.sendMessage(textMessage);
}
public void startTask() {
// new Thread(new SpssTask()).start();
// try {
// StatsUtil.start();
// System.out.println(13216546);
// StatsUtil.stop();
// } catch (Exception e) {
// e.printStackTrace();
// }
}
}