@kangwg
2017-04-26T18:30:53.000000Z
字数 4162
阅读 1056
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
package cn.langya.longjing.websocket;
import org.slf4j.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.*;
@Component
@ServerEndpoint("/dispatch/{userCode}")
public class WebsocketEndPoint {
private static Logger log = LoggerFactory.getLogger(WebsocketEndPoint.class);
@Autowired
private HandleWebSocketMsg handleWebSocketMsg;
/**
* 打开连接时触发
*
* @param userCode
* @param session
*/
@OnOpen
public void onOpen(@PathParam("userCode") String userCode, Session session) {
log.info("Websocket Start Connecting:" + userCode);
log.info("handleWebSocketMsg:{}", handleWebSocketMsg);
SessionUtils.put(userCode, session);
}
/**
* 收到客户端消息时触发
*
* @param userCode
* @param message
* @return
*/
@OnMessage
public String onMessage(@PathParam("userCode") String userCode, String message) throws Exception {
log.info("get onMessage userCode:{},message{}", userCode, message);
WebsocketMsg websocketMsg = this.handleWebSocketMsg.msgCast(userCode, message);
String json = websocketMsg.toJosn();
log.info("cast onMessage userCode:{},message{}", userCode, json);
return json;
}
/**
* 异常时触发
*
* @param userCode
* @param session
*/
@OnError
public void onError(@PathParam("userCode") String userCode, Throwable throwable, Session session) {
log.error("Websocket Connection Exception:" + userCode);
log.error(throwable.getMessage(), throwable);
}
/**
* 关闭连接时触发
*
* @param userCode
* @param session
*/
@OnClose
public void onClose(@PathParam("userCode") String userCode, Session session) {
log.info("Websocket Close Connection:" + userCode);
SessionUtils.remove(userCode);
}
}
handleWebSocketMsg 为空,我该怎样做才能注入
package cn.langya.longjing.websocket;
import cn.langya.longjing.bizenum.LongjingFwConst;
import cn.langya.longjing.service.*;
import cn.swao.baselib.util.ArrayUtils;
import cn.swao.framework.api.*;
import org.slf4j.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.util.*;
@Component
@Scope("prototype")
public class HandleWebSocketMsg {
private static Logger log = LoggerFactory.getLogger(HandleWebSocketMsg.class);
@Autowired
private TerminalMonitorService terminalMonitorService;
@Autowired
private ResDispatchService resDispatchService;
@Autowired
private ResPackageDispatchService resPackageDispatchService;
public void broadcast(String userCode, String message) {
if (SessionUtils.hasConnection(userCode)) {
SessionUtils.get(userCode).getAsyncRemote().sendText(message);
} else {
throw new CustomBizException(ApiCodeEnum.FAIL, userCode + " Connection does not exist");
}
}
public void broadcast(String userCode, Object data, String method) {
WebsocketMsg websocketMsg = new WebsocketMsg(method, data);
this.broadcast(userCode, websocketMsg.toJosn());
}
public String getMsg(Object obj, String method) {
WebsocketMsg websocketMsg = new WebsocketMsg(method, obj);
return websocketMsg.toJosn();
}
public WebsocketMsg msgCast(String userCode, String message) {
WebsocketMsg websocketMsg = null;
try {
WebsocketMsg wsm = WebsocketMsg.getWebsocketMsg(message);
Map<String, Object> data = (Map<String, Object>) wsm.getData();
String method = wsm.getMethod();
switch (method) {
case LongjingFwConst.WebSockMethod.C_NOTICEPACKAGERESULT:
List<Map<String, Object>> packages = (List<Map<String, Object>>) data.get("packages");
for (Map<String, Object> map : packages) {
Long packageId = ArrayUtils.getMapLong(map, "packageId");
Integer result = ArrayUtils.getMapInt(map, "result");
this.resDispatchService.changeDisPatchStatus(packageId, userCode, result);
}
websocketMsg = new WebsocketMsg(method);
break;
case LongjingFwConst.WebSockMethod.C_GETPACKAGE:
Long lastTime = ArrayUtils.getMapLong(data, "lastTime");
Date date = null;
if (lastTime != null)
date = new Date(lastTime);
Map<String, Object> packageList = this.resPackageDispatchService.getPackageList(userCode, date);
websocketMsg = new WebsocketMsg(LongjingFwConst.WebSockMethod.S_PACKAGEUPDATE, packageList);
break;
default:
websocketMsg = new WebsocketMsg();
break;
}
} catch (CustomBizException e) {
log.error("CustomBizException websoket cast fail", e);
} catch (Exception e) {
log.error("websoket cast fail", e);
}
if (websocketMsg == null) {
websocketMsg = new WebsocketMsg();
}
return websocketMsg;
}
}