在springmvc項目中配置websocket的環境,給客戶端發送消息模塊,變量無法注入,值為空
@Autowired
private Mydata mydata;
/** * 收到客戶端消息后調用的方法 * @param message 客戶端發送過來的消息 * @param session 可選的參數 * @throws EncodeException */ @OnMessage public void onMessage(String message, Session session) { System.out.println("來自客戶端的消息:" + message); //群發消息 for(JdeviceController item: webSocketSet){ try { int yy = mydata.findAll().size(); String teee = "webtest" + yy; item.sendMessage(teee); } catch (IOException e) { e.printStackTrace(); continue; } } }
mydata值為空導致websocket連接關閉。原來代碼如下
@Controller @ServerEndpoint( "/websocket") public class MydataController extends BaseController {
}
通過設置ServerEndpoint(value = "/websocket",configurator = SpringConfigurator.class)解決問題,代碼如下
@Controller @ServerEndpoint(value = "/websocket",configurator = SpringConfigurator.class) public class MydataController extends BaseController {
}
需要添加的jar為:spring-websocket-4.3.8.RELEASE