首先导入包必须的:
<!-- 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注入,就这么简单