直接上代码
server类
package com.src.xxx.websocket;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
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 com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
@ServerEndpoint("/websocket/{userId}")
@Component
@Slf4j
public class WebSocketServer {
/**静态变量,用来记录当前在线连接数。*/
private static int onlineCount = 0;
/**concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象,。*/
private static ConcurrentHashMap<String,WebSocketServer> webSocketMap = new ConcurrentHashMap<>();
/**与某个客户端的连接会话,需要通过它来给客户端发送数据*/
private Session session;
/**接收userId*/
private String userId="";
/**
* 连接建立成功调用的方法*/
@OnOpen
public void onOpen(Session session,@PathParam("userId") String userId) {
this.session = session;
this.userId=userId;
if(webSocketMap.containsKey(userId)){
webSocketMap.remove(userId);
webSocketMap.put(userId,this);
//加入set中
}else{
webSocketMap.put(userId,this);
//加入set中
addOnlineCount();
//在线数加1
}
log.info("用户连接:"+userId+",当前在线人数为:" + getOnlineCount());
try {
sendMessage("连接成功");
} catch (IOException e) {
log.error("用户:"+userId+",网络异常!!!!!!");
}
}
/**
* 连接关闭调用的方法
*/
@OnClose
public void onClose() {
if(webSocketMap.containsKey(userId)){
webSocketMap.remove(userId);
//从set中删除
subOnlineCount();
}
log.info("用户退出:"+userId+",当前在线人数为:" + getOnlineCount());
}
// @OnMessage
// public void heart_beat(Session session) throws IOException {
// log.info("用户心跳连接:"+this.userId);
// webSocketMap.get(this.userId).sendMessage("websocket-heart-beat");
// }
/**
* 收到客户端消息后调用的方法
*
* @param message 客户端发送过来的消息*/
@OnMessage
public void onMessage(String message, Session session) {
//log.info("用户心跳连接:"+this.userId);
try {
webSocketMap.get(this.userId).sendMessage("websocket-heart-beat-ok");
}catch (Exception e){
e.printStackTrace();
}
// log.info("用户消息:"+userId+",报文:"+message);
// //可以群发消息
// //消息保存到数据库、redis
// if(!StringUtils.isEmpty(message)){
// try {
// //解析发送的报文
// JSONObject jsonObject = JSON.parseObject(message);
// //追加发送人(防止串改)
// jsonObject.put("fromUserId",this.userId);
// String toUserId=jsonObject.getString("toUserId");
// //传送给对应toUserId用户的websocket
// if(!StringUtils.isEmpty(toUserId)&&webSocketMap.containsKey(toUserId)){
// webSocketMap.get(toUserId).sendMessage(jsonObject.toJSONString());
// }else{
// log.error("请求的userId:"+toUserId+"不在该服务器上");
// //否则不在这个服务器上,发送到mysql或者redis
// }
// }catch (Exception e){
// e.printStackTrace();
// }
// }
}
/**
*
* @param session
* @param error
*/
@OnError
public void onError(Session session, Throwable error) {
log.error("用户错误:"+this.userId+",原因:"+error.getMessage());
error.printStackTrace();
}
/**
* 实现服务器主动推送
*/
public void sendMessage(String message) throws IOException {
this.session.getBasicRemote().sendText(message);
}
/**
* 发送自定义消息
* */
public static void sendInfo(String message,@PathParam("userId") String userId) throws IOException {
log.info("发送消息到:"+userId+",报文:"+message);
if(!StringUtils.isEmpty(userId)&&webSocketMap.containsKey(userId)){
webSocketMap.get(userId).sendMessage(message);
}else{
log.error("用户"+userId+",不在线!");
}
}
public static synchronized int getOnlineCount() {
return onlineCount;
}
public static synchronized void addOnlineCount() {
WebSocketServer.onlineCount++;
}
public static synchronized void subOnlineCount() {
WebSocketServer.onlineCount--;
}
}
对应config
package com.src.xxx;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
maven包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
目录结构如下?
?使用
WebSocketServer.sendInfo('123',id);//id就是连接id
测试html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>WebSocket</title>
</head>
<body>
<h3>hello socket</h3>
<p>【userId】:<div><input id="userId" name="userId" type="text" value="10"></div>
<p>【toUserId】:<div><input id="toUserId" name="toUserId" type="text" value="20"></div>
<p>【toUserId】:<div><input id="contentText" name="contentText" type="text" value="hello websocket"></div>
<p>操作:<div><a onclick="createWebSocket()">开启socket</a></div>
<p>【操作】:<div><a onclick="sendMessage()">发送消息</a></div>
</body>
<script>
var lockReconnect = false; //避免ws重复连接
var ws = null; //判断当前浏览器是否支持WebSocket
//createWebSocket(url); //连接ws
function createWebSocket() {
var userId = document.getElementById('userId').value;
var url="ws://127.0.0.1:8080/websocket/"+userId;
try {
if ('WebSocket' in window) {
ws = new WebSocket(url);
} else if ('MozWebSocket' in window) {
ws = new MozWebSocket(url);
} else {
alert("您的浏览器不支持websocket的协议,建议使用新版谷歌、火狐等浏览器,请勿使用IE10以下浏览器,360浏览器请使用极速模式")
}
initEventHandle();
} catch(e) {
reconnect(url);
console.log(e);
}
}
function initEventHandle() {
ws.onclose = function() {
reconnect(wsUrl);
console.log("llws连接关闭!"+new Data().toUTCString());
};
ws.onerror = function() {
reconnect(wsUrl);
console.log("llws连接错误!");
};
ws.onopen = function() {
heartCheck.reset().start(); //心跳检测重置
console.log("llws连接成功!"+new Data().toUTCString());
};
ws.onmessage = function(event) {//如果获得到消息、心跳检测重置
heartCheck.reset().start(); //拿到任何消息都说明当前连接是正常的
console.log("llws收到消息啦:"+event.data);
};
}
//监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server断会抛异常
window.onbeforeunload = function() {
ws.close();
}
function reconnect(url) {
if (lockReconnect) return;
lockReconnect = true;
setTimeout(function() { //没连接上会一直重连,设置延迟避免请求过多
createWebSocket(url);
lockReconnect = false;
}, 2000);
}
//心跳检测
var heartCheck = {
timeout: 500000, //9分钟发一次心跳
timeoutObj: null,
serverTimeoutObj: null,
reset: function() {
clearTimeout(this.timeoutObj);
clearTimeout(this.serverTimeoutObj);
return this;
},
start: function() {
var self = this;
this.timeoutObj = setTimeout(function() {
//这里发送一个心跳,后端收到后,返回一个心跳消息,
//onmessage拿到返回的心跳就说明连接正常
ws.send("ping");
console.log("ping!");
self.serverTimeoutObj = setTimeout(function() {//如果超过一定时间还没重置,说明后端主动断开了
ws.close(); //如果onclose会执行reconnect,我们执行ws.close()就行了,如果直接执行reconnect会触发onclose导致重连两次
}, self.timeout)
}, this.timeout)
}
}
</script>
</html>
|