一、概述
本文基于spring-boot-starter-websocket简单的完成收发信息功能,使用spring框架进行实现。
二、相关配置
spring:2.0.2,jdk:1.8.202,maven:3.3.9
因为spring和maven有版本匹配的要求,请大家注意自己的版本是否匹配
三、项目结构
四、代码
1.启动类
SocketDemoApplication
package com.lp.socketdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
@EnableScheduling
@SpringBootApplication
public class SocketDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SocketDemoApplication.class, args);
}
//处理定时任务报错问题
@Bean
public ThreadPoolTaskScheduler taskScheduler(){
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(10);
taskScheduler.initialize();
return taskScheduler;
}
}
2.握手拦截器
package com.lp.socketdemo.spring;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;
import java.util.Map;
/**
* 握手拦截器
*/
@Component
public class MyInterceptor extends HttpSessionHandshakeInterceptor {
@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
System.out.println(request.getRemoteAddress().toString()+"开始握手");
return super.beforeHandshake(request, response, wsHandler, attributes);
}
@Override
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception ex) {
System.out.println(request.getRemoteAddress().toString()+"完成握手");
super.afterHandshake(request, response, wsHandler, ex);
}
}
3.websocket主程序
package com.lp.socketdemo.spring;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.AbstractWebSocketHandler;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
/**
* websocket 主程序
*/
@Component
public class MyWsHandler extends AbstractWebSocketHandler {
private static Map<String,SessionBean> map;
private static AtomicInteger clientIdMaker;
static {
map = new ConcurrentHashMap<>();
clientIdMaker = new AtomicInteger(0);
}
/**
* 连接建立
* @param session
* @throws Exception
*/
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
SessionBean sb = new SessionBean(session,clientIdMaker.getAndIncrement());
map.put(session.getId(),sb);
System.out.println(map.get(session.getId()).getClientId() +":建立连接");
super.afterConnectionEstablished(session);
}
/**
* 接收信息
* @param session
* @param message
* @throws Exception
*/
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
super.handleTextMessage(session, message);
System.out.println(map.get(session.getId()).getClientId() +":"+message.getPayload());
}
/**
* 传输异常
* @param session
* @param exception
* @throws Exception
*/
@Override
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
super.handleTransportError(session, exception);
if(session.isOpen()) {
session.close();
}
map.remove(session.getId());
}
/**
* 连接关闭
* @param session
* @param status
* @throws Exception
*/
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
super.afterConnectionClosed(session, status);
System.out.println(map.get(session.getId()).getClientId() +":关闭连接");
map.remove(session.getId());
}
@Scheduled(fixedRate = 10000)
public void sendMsg() throws IOException{
for (String key: map.keySet()) {
map.get(key).getWebSocketSession().sendMessage(new TextMessage("ws2心跳"));
}
}
}
4.配置类
package com.lp.socketdemo.spring;
import com.lp.socketdemo.java.WebSocketConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import javax.annotation.Resource;
@Configuration
@EnableWebSocket
public class MyWsConfig implements WebSocketConfigurer {
@Resource
MyWsHandler MyWsHandler;
@Resource
MyInterceptor myInterceptor;
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(MyWsHandler,"/myWs2").addInterceptors(myInterceptor).setAllowedOrigins("*");
}
}
5.封装实体类
package com.lp.socketdemo.spring;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;
@Component
public class SessionBean {
private WebSocketSession webSocketSession;
private int clientId;
public SessionBean() {
}
public SessionBean(WebSocketSession webSocketSession, int clientId) {
this.webSocketSession = webSocketSession;
this.clientId = clientId;
}
public WebSocketSession getWebSocketSession() {
return webSocketSession;
}
public void setWebSocketSession(WebSocketSession webSocketSession) {
this.webSocketSession = webSocketSession;
}
public int getClientId() {
return clientId;
}
public void setClientId(int clientId) {
this.clientId = clientId;
}
}
5.前段
websocketTest.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>websocket调试页面</title>
</head>
<script type="text/javascript" src="jquery.min.js"></script>
<body>
<div style="float: left; padding: 20px">
<strong>location:</strong> <br />
<input type="text" id="serverUrl" size="35" value="" /> <br />
<button onclick="connect()">connect</button>
<button onclick="wsclose()">disConnect</button>
<br /> <strong>message:</strong> <br /> <input id="txtMsg" type="text" size="50" />
<br />
<button onclick="sendEvent()">发送</button>
</div>
<div style="float: left; margin-left: 20px; padding-left: 20px; width: 350px; border-left: solid 1px #cccccc;"> <strong>消息记录</strong>
<div style="border: solid 1px #999999;border-top-color: #CCCCCC;border-left-color: #CCCCCC; padding: 5px;width: 100%;height: 172px;overflow-y: scroll;" id="echo-log"></div>
<button onclick="clearLog()" style="position: relative; top: 3px;">清除消息</button>
</div>
</div>
</body>
<!-- 下面是h5原生websocket js写法 -->
<script type="text/javascript">
let output ;
let websocket;
function connect(){ //初始化连接
output = document.getElementById("echo-log")
let inputNode = document.getElementById("serverUrl");
let wsUri = inputNode.value;
try{
websocket = new WebSocket(wsUri);
}catch(ex){
console.log(ex)
alert("对不起websocket连接异常")
}
connecting();
window.addEventListener("load", connecting, false);
}
function connecting()
{
websocket.onopen = function(evt) { onOpen(evt) };
websocket.onclose = function(evt) { onClose(evt) };
websocket.onmessage = function(evt) { onMessage(evt) };
websocket.onerror = function(evt) { onError(evt) };
}
function sendEvent(){
let msg = document.getElementById("txtMsg").value
doSend(msg);
}
//连接上事件
function onOpen(evt)
{
writeToScreen("CONNECTED");
doSend("WebSocket 已经连接成功!");
}
//关闭事件
function onClose(evt)
{
writeToScreen("连接已经断开!");
}
//后端推送事件
function onMessage(evt)
{
writeToScreen('<span style="color: blue;">服务器: ' + evt.data+'</span>');
}
function onError(evt)
{
writeToScreen('<span style="color: red;">异常信息:</span> ' + evt.data);
}
function doSend(message)
{
writeToScreen("客户端A: " + message);
websocket.send(message);
}
//清除div的内容
function clearLog(){
output.innerHTML = "";
}
//浏览器主动断开连接
function wsclose(){
websocket.close();
}
function writeToScreen(message)
{
let pre = document.createElement("p");
pre.innerHTML = message;
output.appendChild(pre);
}
</script>
</html>
五.效果展示
六、引用
课程介绍_WebSocket入门与案例实战-慕课网
最全面的SpringMVC教程(六)——WebSocket_springmvc websocket_小新要变强的博客-CSDN博客