天天看點

java websocket 斷線_WebSocket移動端通路時斷開連接配接報錯問題。

該樓層疑似違規已被系統折疊 隐藏此樓檢視此樓

package com.ruoyi.web.core;

@ServerEndpoint("/websocket/{fromUser}")

@Component

public class WebSocket {

private static final Logger log = LoggerFactory.getLogger(WebSocket.class);

// @Autowired

// private IQywxAccessTokenService qywxAccessTokenService;

private static IQywxAccessTokenService qywxAccessTokenService;

private static IWxTalkingInfoService wxTalkingInfoService;

//靜态變量,用來記錄目前線上連接配接數。應該把它設計成線程安全的。

private static int onlineCount = 0;

//concurrent包的線程安全Set,用來存放每個用戶端對應的MyWebSocket對象。若要實作服務端與單一用戶端通信的話,可以使用Map來存放,其中Key可以為使用者辨別

// private static CopyOnWriteArraySet webSocketSet = new CopyOnWriteArraySet();

private static ConcurrentHashMap webSocketSetMap = new ConcurrentHashMap<>();

//與某個用戶端的連接配接會話,需要通過它來給用戶端發送資料

// private Session session;

// 毫秒轉換機關

final static int SECOND = 1000;

// 注入的時候,給類的 service 注入

@Autowired

public void setChatService(IQywxAccessTokenService qywxAccessTokenService) {

WebSocket.qywxAccessTokenService = qywxAccessTokenService;

}

@Autowired

public void setIWxTalkingInfoService(IWxTalkingInfoService wxTalkingInfoService) {

WebSocket.wxTalkingInfoService = wxTalkingInfoService;

}

//@本地啟動不能注釋,打成war包的時候需要注釋,否則在linux中部署會報錯

@Bean

public ServerEndpointExporter serverEndpointExporter() {

return new ServerEndpointExporter();

}

@OnOpen

public void onOpen(@PathParam("fromUser")String fromUser, Session session){

System.err.println("連接配接成功");

// this.session = session;

if(webSocketSetMap == null) {

webSocketSetMap = new ConcurrentHashMap();

}

webSocketSetMap .put(fromUser,session);

// webSocketSet.add(this); //加入set中

// addOnlineCount(); //線上數加1

}

@OnClose

public void onClose(@PathParam("fromUser") String fromUser){

System.err.println(fromUser+ "退出了聊天界面");

webSocketSetMap.remove(fromUser);

// webSocketSet.remove(this); //從set中删除

// subOnlineCount(); //線上數減1

}

@OnMessage

public void onMessage(String message, Session session) {

//{"fromUser":3,"toUser":4,"msgInfo":測試}

System.err.println("message : " + message);

JSONObject jsonObj = JSONObject.parseObject(message);

String msgInfo = "";

if(StringUtils.isNotNull(jsonObj)){

String fromUser = jsonObj.getString("fromUser");

String toUser = jsonObj.getString("toUser");

msgInfo = jsonObj.getString("msgInfo");

String fromUserType = jsonObj.getString("fromUserType");

if(fromUser.length() > toUser.length()){

System.err.println("2222222222222222222222222222222222222");

//說明是使用者給理财經理發消息,需要調接口

if(StringUtils.isNotEmpty(fromUser) && StringUtils.isNotEmpty(toUser)){

//調企業微信接口,向理财經理推送文本消息

String corpid = (String) YMLUtils.getCommonYml("qyapi.corpid");

String corpsecret = (String) YMLUtils.getCommonYml("qyapi.corpsecret");

String agentid = YMLUtils.getCommonYml("qyapi.agentid").toString();

String sendUrl = YMLUtils.getCommonYml("qyapi.sendUrl").toString();

log.info("corpid : " + corpid);

log.info("corpsecret : " + corpsecret);

if(qywxAccessTokenService != null){

QywxAccessToken qywxAccessToken = new QywxAccessToken();

qywxAccessToken.setCorpsecret(corpsecret);

List qywxAccessTokenList = qywxAccessTokenService.selectQywxAccessTokenList(qywxAccessToken);

String qyAccessToken = getQyAccessToken(qywxAccessTokenList);

JSONObject jsonObject = new JSONObject();

Text text = new Text();

// text.setContent(msgInfo);

text.setContent("點選檢視新消息");

TextMessage msg = new TextMessage();

msg.setText(text);

msg.setTouser("SunZhiQiang");

msg.setAgentid(agentid);

msg.setMsgtype("text");

String msgJson = JSONObject.toJSONString(msg);

String result = HttpUtils.sendSSLPost(sendUrl.replace("ACCESS_TOKEN",qyAccessToken), msgJson);

System.err.println("result : " + result);

}else{

System.err.println("..................");

}

}

}else{

}

// if(webSocketSetMap.containsKey(toUser) && StringUtils.isNotNull(webSocketSetMap.get(toUser))){

// try {

// webSocketSetMap.get(toUser).sendMessage(message);

// } catch (IOException e) {

// e.printStackTrace();

// }

// }

for(Session session_ : webSocketSetMap.values()) {

session_.getAsyncRemote().sendText(message);

}

}

}

@OnError

public void onError(Throwable error){

System.err.println("....................................");

error.printStackTrace();

}

// public void sendMessage(String message) throws IOException{

// this.session.getBasicRemote().sendText(message);

// }

public static synchronized int getOnlineCount() {

return onlineCount;

}

public static synchronized void addOnlineCount() {

WebSocket.onlineCount++;

}

public static synchronized void subOnlineCount() {

WebSocket.onlineCount--;

}