ArvinShaffer

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

在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

 

posted on 2017-10-19 15:12  ArvinShaffer  阅读(12868)  评论(2)    收藏  举报