spring框架中自带了websocket的jar包,利用它可以实现与H5中WebSocket的对接,甚至websocket还可以通过依赖注入与http请求一同工作,详细配置实现过程如下
文件目录结构如下,主要是controller和websocket文件夹
1.配置自动扫描加载:
<!--如果使用注解,那么只需要下面的配置-->
<!--组件扫描-->
<context:component-scan base-package="com.xiaoxiaohei.ssm.websocket,com.xiaoxiaohei.ssm.controller"></context:component-scan>
<!--注解自动加载,不用配置映射器和适配器了-->
<mvc:annotation-driven validator="validator"></mvc:annotation-driven>
2.创建一个WebSocket配置类(这里也可以用配置文件来实现其实),实现接口来配置Websocket请求的路径和拦截器。
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(myHandler(), "/myHandler").addInterceptors(new WebSocketInterceptor());
}
@Bean
public WebSocketHandler myHandler() {
return new MyHandler();
}
}
3.拦截器主要是用于用户登录标识(userId)的记录,便于后面获取指定用户的会话标识并向指定用户发送消息,在下面的拦截器中,我在session中获取会话标识(这个标识是在登录时setAttribute进去的,后面代码会说到),你也可以通过H5在new WebSocket(url)
中,在url传入标识参数,然后通过serverHttpRequest.getServletRequest().getParameterMap()
来获取标识信息。
public class WebSocketInterceptor implements HandshakeInterceptor {
@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler handler, Map<String, Object> map) throws Exception {
if (request instanceof ServletServerHttpRequest) {
ServletServerHttpRequest serverHttpRequest = (ServletServerHttpRequest) request;
HttpSession session = serverHttpRequest.getServletRequest().getSession();
// Map parameterMap = serverHttpRequest.getServletRequest().getParameterMap();
// System.out.println(parameterMap);
if (session != null) {
map.put("userId", session.getAttribute("userId"));
}
}
return true;
}
@Override
public void afterHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse, WebSocketHandler webSocketHandler, Exception e) {
}
}
4.实现Websocket建立连接、发送消息、断开连接等时候的处理类。
在这个类中,主要的处理流程如下:
a.在afterConnectionEstablished
连接建立成功之后,记录用户的连接标识,便于后面发信息,这里我是记录将id记录在Map集合中。
b.在handleTextMessage
中可以对H5 Websocket的send方法进行处理
c.sendMessageToUser
向指定用户发送消息,传入用户标识和消息体
d.sendMessageToAllUsers
向左右用户广播消息,只需要传入消息体
e.handleTransportError
连接出错处理,主要是关闭出错会话的连接,和删除在Map集合中的记录
f.afterConnectionClosed
连接已关闭,移除在Map集合中的记录。
g.getClientId
我自己封装的一个方法,方便获取用户标识
@Service
public class MyHandler extends TextWebSocketHandler {
//在线用户列表
private static final Map<Integer, WebSocketSession> users;
//用户标识
private static final String CLIENT_ID = "userId";
static {
users = new HashMap<>();
}
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
System.out.println("成功建立连接");
Integer userId = getClientId(session);
System.out.println(userId);
if (userId != null) {
users.put(userId, session);
session.sendMessage(new TextMessage("成功建立socket连接"));
System.out.println(userId);
System.out.println(session);
}
}
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) {
// ...
System.out.println(message.getPayload());
WebSocketMessage message1 = new TextMessage("server:"+message);
try {
session.sendMessage(message1);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 发送信息给指定用户
* @param clientId
* @param message
* @return
*/
public boolean sendMessageToUser(Integer clientId, TextMessage message) {
if (users.get(clientId) == null) return false;
WebSocketSession session = users.get(clientId);
System.out.println("sendMessage:" + session);
if (!session.isOpen()) return false;
try {
session.sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 广播信息
* @param message
* @return
*/
public boolean sendMessageToAllUsers(TextMessage message) {
boolean allSendSuccess = true;
Set<Integer> clientIds = users.keySet();
WebSocketSession session = null;
for (Integer clientId : clientIds) {
try {
session = users.get(clientId);
if (session.isOpen()) {
session.sendMessage(message);
}
} catch (IOException e) {
e.printStackTrace();
allSendSuccess = false;
}
}
return allSendSuccess;
}
@Override
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
if (session.isOpen()) {
session.close();
}
System.out.println("连接出错");
users.remove(getClientId(session));
}
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
System.out.println("连接已关闭:" + status);
users.remove(getClientId(session));
}
@Override
public boolean supportsPartialMessages() {
return false;
}
/**
* 获取用户标识
* @param session
* @return
*/
private Integer getClientId(WebSocketSession session) {
try {
Integer clientId = (Integer) session.getAttributes().get(CLIENT_ID);
return clientId;
} catch (Exception e) {
return null;
}
}
}
封装完成后,下面是具体的使用流程:
1.可以建立一个controller用于用户登录,发送消息等(此处需要发送消息,只需要用依赖注入即可)
@Controller
public class SocketController {
@Autowired
MyHandler handler;
@RequestMapping("/login/{userId}")
public @ResponseBody String login(HttpSession session, @PathVariable("userId") Integer userId) {
System.out.println("登录接口,userId="+userId);
session.setAttribute("userId", userId);
System.out.println(session.getAttribute("userId"));
return "success";
}
@RequestMapping("/message")
public @ResponseBody String sendMessage() {
boolean hasSend = handler.sendMessageToUser(4, new TextMessage("发送一条小xi"));
System.out.println(hasSend);
return "message";
}
}
2.具体的html代码:
<script type="text/javascript">
$(function() {
console.log("abc");
$.ajax({url:"http://localhost:8080/login/4",success:function(result){
console.log(result);
var ws = new WebSocket("ws://localhost:8080/myHandler")
ws.onopen = function () {
console.log("onpen");
ws.send("{}");
}
ws.onclose = function () {
console.log("onclose");
}
ws.onmessage = function (msg) {
console.log(msg.data);
}
}});
})
</script>