@kangwg
2017-04-26T10:30:53.000000Z
字数 4162
阅读 1173
@Configurationpublic class WebSocketConfig {@Beanpublic 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);@Autowiredprivate HandleWebSocketMsg handleWebSocketMsg;/*** 打开连接时触发** @param userCode* @param session*/@OnOpenpublic 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*/@OnMessagepublic 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*/@OnErrorpublic 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*/@OnClosepublic 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);@Autowiredprivate TerminalMonitorService terminalMonitorService;@Autowiredprivate ResDispatchService resDispatchService;@Autowiredprivate 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;}}