对于Spring对websocket的属性注入失败问题,困扰我一天,最后终于解决了
2017-05-08 17:14
531 查看
首先导入包必须的:
以及一个websocket-api.jar
下面看代码
:
主要在@ServerEndpoint设置: configurator = SpringConfigurator.class
意思是可以Spring注入,就这么简单
<!-- https://mvnrepository.com/artifact/org.springframework/spring-websocket --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-websocket</artifactId> <version>4.3.5.RELEASE</version> </dependency>
以及一个websocket-api.jar
下面看代码
:
package com.sanker.webSocket; import java.io.IOException; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import javax.annotation.Resource; import javax.websocket.OnClose; import javax.websocket.OnError; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.server.PathParam; import javax.websocket.server.ServerEndpoint; import org.hibernate.SessionFactory; import org.springframework.web.socket.server.standard.SpringConfigurator; import com.sanker.Bean.player.Player; import com.sanker.Dao.Impl.player.PlayerDaoImpl; import com.sanker.util.JsonUtil;
@ServerEndpoint(value="/websocket/{playerId}",configurator = SpringConfigurator.class)
public class WebSocketController { @Resource private PlayerDaoImpl playerDao; @Resource() private SessionFactory sessionFactory; /** * 用来存放所有的在线用户 */ private static final Map<String,WebSocketController> connections = new LinkedHashMap<String, WebSocketController>(); /** * 获取用户传过来的用户信息 */ private Session session; /** * 连接websocket成功 * @param playerId * @param session */ @OnOpen public void onOpen(@PathParam("playerId") String playerId, Session session){ this.session = session; System.out.println(this.session.toString()); System.out.println(playerId+"打开了连接!!!"); connections.put(playerId, this); Map<String, String> msg = new HashMap<String, String>(); msg.put("title", "pId_check"); msg.put("result", "success"); sendMessage(playerId, JsonUtil.getJsonMap(msg)); /*********** 下面的问题 *************/ System.out.println(playerDao); Player playerInfo = playerDao.getPlayerById(playerId); System.out.println(playerInfo); Map<String, Object> playerInfoMap = new HashMap<String, Object>(); playerInfoMap.put("title", "playerInfo"); playerInfoMap.put("info", playerInfo); //sendMessage(playerId, JsonUtil.getJson(playerInfoMap)); } /** * 接受客服端发送的消息 * @param playerId 发送消息的ID * @param message 消息 * @param session */ @OnMessage public void OnMessage(@PathParam("playerId") String playerId, String message, Session session){ System.out.println(message); sendMessage(playerId, "收到消息了"); } /** * 向客户端发送消息 * @param playerId 当前用户ID * @param message 消息内容 */ public void sendMessage(String playerId,String message) { try { if (connections.containsKey(playerId)) { this.session.getBasicRemote().sendText(message); } } catch (IOException e) { e.printStackTrace(); } } /** * 用户关闭连接 * @param playerId * 移除当前ID的session */ @OnClose public void onClose(@PathParam("playerId") String playerId) { System.out.println(playerId + " 已掉线!"); connections.remove(playerId); } /** * 发送错误!!! * @param playerId * @param session * @param error * 移除当前ID的session */ @OnError public void onError(@PathParam("playerId") String playerId, Session session, Throwable error) { System.out.println("websocket onerror - " + playerId); connections.remove(playerId); error.printStackTrace(); } }
主要在@ServerEndpoint设置: configurator = SpringConfigurator.class
意思是可以Spring注入,就这么简单
相关文章推荐
- idea上使用maven分模块开发时解决spring注入失败问题java.lang.IllegalStateException: Failed to load ApplicationContext
- spring整合websocket注入service失败问题
- SpringBoot集成Quartz(解决@Autowired空指针Null问题即依赖注入的属性为null)
- 解决WebSocket无法依赖注入的问题(Spring)
- mapper注入失败问题解决 {@org.springframework.beans.factory.annotation.Autowired(required=true)}
- Spring AOP切面类依赖注入失败问题解决
- springboot websocket 解决不能注入bean问题
- spring+mybatis 注入properties文件中属性失败导致datasource加载失败的原因和解决
- spring+springmvc整合mabytis时mapper注入失败问题解决方法
- SSI报空指针异常,即spring注入失败问题的解决方式
- Spring注解配置注入失败属性null解决
- 15年的最后一天终于暂时解决了软件中的“过年虫”问题
- 困扰我3天的问题终于解决了!
- 兼容IE6,IE7,IE8,Firefox,Chrome,DIV+CSS垂直居中终于失败,最后三分钟用写了几行JS解决,经简单测试,还行。
- 终于解决了一个困扰多日的问题:模块句柄
- 关于 php 用webservice传输数据的问题(nosoap与.NET对接)此处一直困扰三天时间,终于解决
- 困扰已久的乱码问题终于得到解决,希望下次出现更多问题,解决问题的过程很愉快!!!!!
- 困扰很久问题---备份失败解决
- 应用通过JNDI访问WAS数据源时Spring中datasource初始化失败问题的解决办法
- 困扰我多日的英文网站单词无法换行问题终于解决了。