@ServerEndpoint(value="/websocket/{param}",configurator = GetHttpSessionConfigurator.class)
@Component
public class WebSocketServer {
private static final Logger logger = LoggerFactory.getLogger(WebSocketServer.class);
/**
* 连接建立成功调用的方法
*
* 1000 连接正常关闭
1001 端点离线,例如服务器down,或者浏览器已经离开此页面
1002 端点因为协议错误而中断连接
1003 端点因为受到不能接受的数据类型而中断连接
1004 保留
1005 保留, 用于提示应用未收到连接关闭的状态码
1006 端点异常关闭
1007 端点收到的数据帧类型不一致而导致连接关闭
1008 数据违例而关闭连接
1009 收到的消息数据太大而关闭连接
1010 客户端因为服务器未协商扩展而关闭
1011 服务器因为遭遇异常而关闭连接
1015 TLS握手失败关闭连接
* @param session
* 可选的参数。session为与某个客户端的连接会话,需要通过它来给客户端发送数据
* @throws IOException
*/
@OnOpen
public void onOpen(@PathParam(value = "param") String param, Session session) throws IOException {
AccountSubService accountSubService=(AccountSubService)SpringContextUtil.getBean("accountSubServiceImpl");
try {
session.setMaxIdleTimeout(100000);//心跳等待时间
logger.info("有新连接加入{},超时时间:{},当前时间:{}",param,session.getMaxIdleTimeout(),System.currentTimeMillis());
addOnlineCount(); // 在线数加1
WebSocketMap.getInstance().sessionMap.put(session.getId(), session);
WebSocketMap.getInstance().sessionAccountMap.put(session.getId(), param);
WebSocketMap.getInstance().accountSessionMap.put(param, session.getId());
logger.info("有新连接加入{},当前在线人数为{},seeionid:{}",param,getOnlineCount(),session.getId());
String[] params=param.split(Constants.SEPARATOR_CHAR_2);
accountSubService.editOnlineStatus(BusinessConstants.ACCOUNT_SUB_ONLINE_STATUS_ONLINE, params[1]);//在线状态,子账户号
ResponseModel responseModel=new ResponseModel();
responseModel.setRequestId(session.getId());
responseModel.setRetCode(RetCodeEnum.SUCCESS.getCode());
responseModel.setRetMsg(RetCodeEnum.SUCCESS.getValue());
responseModel.setSuccess(true);
responseModel.setWsStatus(BusinessConstants.WEBSOCKET_STATUS_ONLIN);
sendMessage(responseModel,session);
} catch (IOException e) {
logger.error("websocket-onOpen连接异常: 登录人{},Exception:{}",param,e.getMessage());
}
}
/**
* 连接关闭调用的方法
*/
@OnClose
public void onClose(Session session) {
subOnlineCount(); // 在线数减1
String param=WebSocketMap.getInstance().sessionAccountMap.get(session.getId());
try {
WebSocketMap.getInstance().sessionMap.remove(session.getId());
WebSocketMap.getInstance().sessionAccountMap.remove(session.getId());
WebSocketMap.getInstance().accountSessionMap.remove(param);
logger.info("有一连接关闭!当前在线人数为{},seeionid:{},param:{},当前时间:{}",getOnlineCount(),session.getId(),param,System.currentTimeMillis());
AccountSubService accountSubService=(AccountSubService)SpringContextUtil.getBean("accountSubServiceImpl");
String[] params=param.split(Constants.SEPARATOR_CHAR_2);
accountSubService.editOnlineStatus(BusinessConstants.ACCOUNT_SUB_ONLINE_STATUS_OFFLINE, params[1]);//在线状态,子账户号
ResponseModel responseModel=new ResponseModel();
responseModel.setRequestId(session.getId());
responseModel.setRetCode(RetCodeEnum.FAIL.getCode());
responseModel.setRetMsg(RetCodeEnum.FAIL.getValue());
responseModel.setSuccess(false);
responseModel.setWsStatus(BusinessConstants.WEBSOCKET_STATUS_CLOSE);
sendMessage(responseModel,session);
} catch (Exception e) {
logger.error("websocket-onClose连接异常: 登录人{},Exception:{}",param,e.getMessage());
}
}
/**
* 收到客户端消息后调用的方法
*
* @param message
* 客户端发送过来的消息
* @param session
* 可选的参数
*/
@OnMessage
public void onMessage(String message, Session session) {
String param=WebSocketMap.getInstance().sessionAccountMap.get(session.getId());
try {
logger.info("来自客户端的消息!message:{},sessionId:{},param:{},当前时间:{}",message,session.getId(),param,System.currentTimeMillis());
WsAsyncBussService wsAsyncBussService=(WsAsyncBussService)SpringContextUtil.getBean("wsAsyncBussService");
ResponseModel responseModel=wsAsyncBussService.recivedMessage(message);
} catch (Exception e) {
logger.error("websocket-onMessage接收消息失败: 登录人{},Exception:{}",param,e.getMessage());
}
}
/**
* 发生错误时调用
* @param session
* @param error
*/
@OnError
public void onError(Session session, Throwable error) {
subOnlineCount(); // 在线数减1
String param=WebSocketMap.getInstance().sessionAccountMap.get(session.getId());
try {
WebSocketMap.getInstance().sessionMap.remove(session.getId());
WebSocketMap.getInstance().sessionAccountMap.remove(session.getId());
WebSocketMap.getInstance().accountSessionMap.remove(param);
logger.info("有一连接发生错误!当前在线人数为{},seeionid:{},param:{},当前时间:{}",getOnlineCount(),session.getId(),param,System.currentTimeMillis());
AccountSubService accountSubService=(AccountSubService)SpringContextUtil.getBean("accountSubServiceImpl");
String[] params=param.split(Constants.SEPARATOR_CHAR_2);
accountSubService.editOnlineStatus(BusinessConstants.ACCOUNT_SUB_ONLINE_STATUS_OFFLINE, params[1]);//在线状态,子账户号
ResponseModel responseModel=new ResponseModel();
responseModel.setRequestId(session.getId());
responseModel.setRetCode(RetCodeEnum.FAIL.getCode());
responseModel.setRetMsg(RetCodeEnum.FAIL.getValue());
responseModel.setSuccess(false);
responseModel.setWsStatus(BusinessConstants.WEBSOCKET_STATUS_ERROR);
sendMessage(responseModel,session);
} catch (Exception e) {
logger.error("websocket-onError连接异常: 登录人{},Exception:{}",param,e.getMessage());
}
}
/**
* 这个方法与上面几个方法不一样。没有用注解,是根据自己需要添加的方法。
*/
public void sendMessage(String message,Session session) throws IOException {
logger.info("发送的消息:{}",message);
session.getBasicRemote().sendText(message);
}
public void sendMessage(ResponseModel responseModel,Session session) throws IOException {
logger.info("发送的消息:{}",JSON.toJSONString(responseModel));
session.getBasicRemote().sendText(JSON.toJSONString(responseModel));
}
public static int getOnlineCount() {
return WebSocketMap.onlineCount.get();
}
public static void addOnlineCount() {
WebSocketMap.onlineCount.incrementAndGet();//在线数加1
}
public static void subOnlineCount() {
WebSocketMap.onlineCount.decrementAndGet();//在线数减1
}
}