[关闭]
@kangwg 2017-04-26T18:30:53.000000Z 字数 4162 阅读 1056

spring boot websocket


Autowired is null

  1. @Configuration
  2. public class WebSocketConfig {
  3. @Bean
  4. public ServerEndpointExporter serverEndpointExporter() {
  5. return new ServerEndpointExporter();
  6. }
  7. }
  1. package cn.langya.longjing.websocket;
  2. import org.slf4j.*;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Component;
  5. import javax.websocket.*;
  6. import javax.websocket.server.*;
  7. @Component
  8. @ServerEndpoint("/dispatch/{userCode}")
  9. public class WebsocketEndPoint {
  10. private static Logger log = LoggerFactory.getLogger(WebsocketEndPoint.class);
  11. @Autowired
  12. private HandleWebSocketMsg handleWebSocketMsg;
  13. /**
  14. * 打开连接时触发
  15. *
  16. * @param userCode
  17. * @param session
  18. */
  19. @OnOpen
  20. public void onOpen(@PathParam("userCode") String userCode, Session session) {
  21. log.info("Websocket Start Connecting:" + userCode);
  22. log.info("handleWebSocketMsg:{}", handleWebSocketMsg);
  23. SessionUtils.put(userCode, session);
  24. }
  25. /**
  26. * 收到客户端消息时触发
  27. *
  28. * @param userCode
  29. * @param message
  30. * @return
  31. */
  32. @OnMessage
  33. public String onMessage(@PathParam("userCode") String userCode, String message) throws Exception {
  34. log.info("get onMessage userCode:{},message{}", userCode, message);
  35. WebsocketMsg websocketMsg = this.handleWebSocketMsg.msgCast(userCode, message);
  36. String json = websocketMsg.toJosn();
  37. log.info("cast onMessage userCode:{},message{}", userCode, json);
  38. return json;
  39. }
  40. /**
  41. * 异常时触发
  42. *
  43. * @param userCode
  44. * @param session
  45. */
  46. @OnError
  47. public void onError(@PathParam("userCode") String userCode, Throwable throwable, Session session) {
  48. log.error("Websocket Connection Exception:" + userCode);
  49. log.error(throwable.getMessage(), throwable);
  50. }
  51. /**
  52. * 关闭连接时触发
  53. *
  54. * @param userCode
  55. * @param session
  56. */
  57. @OnClose
  58. public void onClose(@PathParam("userCode") String userCode, Session session) {
  59. log.info("Websocket Close Connection:" + userCode);
  60. SessionUtils.remove(userCode);
  61. }
  62. }

handleWebSocketMsg 为空,我该怎样做才能注入

  1. package cn.langya.longjing.websocket;
  2. import cn.langya.longjing.bizenum.LongjingFwConst;
  3. import cn.langya.longjing.service.*;
  4. import cn.swao.baselib.util.ArrayUtils;
  5. import cn.swao.framework.api.*;
  6. import org.slf4j.*;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.context.annotation.Scope;
  9. import org.springframework.stereotype.Component;
  10. import java.util.*;
  11. @Component
  12. @Scope("prototype")
  13. public class HandleWebSocketMsg {
  14. private static Logger log = LoggerFactory.getLogger(HandleWebSocketMsg.class);
  15. @Autowired
  16. private TerminalMonitorService terminalMonitorService;
  17. @Autowired
  18. private ResDispatchService resDispatchService;
  19. @Autowired
  20. private ResPackageDispatchService resPackageDispatchService;
  21. public void broadcast(String userCode, String message) {
  22. if (SessionUtils.hasConnection(userCode)) {
  23. SessionUtils.get(userCode).getAsyncRemote().sendText(message);
  24. } else {
  25. throw new CustomBizException(ApiCodeEnum.FAIL, userCode + " Connection does not exist");
  26. }
  27. }
  28. public void broadcast(String userCode, Object data, String method) {
  29. WebsocketMsg websocketMsg = new WebsocketMsg(method, data);
  30. this.broadcast(userCode, websocketMsg.toJosn());
  31. }
  32. public String getMsg(Object obj, String method) {
  33. WebsocketMsg websocketMsg = new WebsocketMsg(method, obj);
  34. return websocketMsg.toJosn();
  35. }
  36. public WebsocketMsg msgCast(String userCode, String message) {
  37. WebsocketMsg websocketMsg = null;
  38. try {
  39. WebsocketMsg wsm = WebsocketMsg.getWebsocketMsg(message);
  40. Map<String, Object> data = (Map<String, Object>) wsm.getData();
  41. String method = wsm.getMethod();
  42. switch (method) {
  43. case LongjingFwConst.WebSockMethod.C_NOTICEPACKAGERESULT:
  44. List<Map<String, Object>> packages = (List<Map<String, Object>>) data.get("packages");
  45. for (Map<String, Object> map : packages) {
  46. Long packageId = ArrayUtils.getMapLong(map, "packageId");
  47. Integer result = ArrayUtils.getMapInt(map, "result");
  48. this.resDispatchService.changeDisPatchStatus(packageId, userCode, result);
  49. }
  50. websocketMsg = new WebsocketMsg(method);
  51. break;
  52. case LongjingFwConst.WebSockMethod.C_GETPACKAGE:
  53. Long lastTime = ArrayUtils.getMapLong(data, "lastTime");
  54. Date date = null;
  55. if (lastTime != null)
  56. date = new Date(lastTime);
  57. Map<String, Object> packageList = this.resPackageDispatchService.getPackageList(userCode, date);
  58. websocketMsg = new WebsocketMsg(LongjingFwConst.WebSockMethod.S_PACKAGEUPDATE, packageList);
  59. break;
  60. default:
  61. websocketMsg = new WebsocketMsg();
  62. break;
  63. }
  64. } catch (CustomBizException e) {
  65. log.error("CustomBizException websoket cast fail", e);
  66. } catch (Exception e) {
  67. log.error("websoket cast fail", e);
  68. }
  69. if (websocketMsg == null) {
  70. websocketMsg = new WebsocketMsg();
  71. }
  72. return websocketMsg;
  73. }
  74. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注