主要参考了这篇文章:https://www.cnblogs.com/freud/p/8397934.html
1.客户端
var websocket = null;
var host = document.location.host;
var userid = "${user.id}"; // 获得当前登录人员的id
//判断当前浏览器是否支持WebSocket
if ('WebSocket' in window) {
websocket = new WebSocket('ws://'+host+'/iats/webSocket/?userid='+userid);
} else {
alert('当前浏览器不支持websocket')
}
//连接发生错误的回调方法
websocket.onerror = function() {
setMessageInnerHTML("WebSocket连接发生错误");
};
//连接成功建立的回调方法
websocket.onopen = function() {
setMessageInnerHTML("WebSocket连接成功");
}
//接收到消息的回调方法
websocket.onmessage = function(event) {
alert("接收到消息的回调方法")
alert("这是后台推送的消息:"+event.data);
websocket.close();
alert("webSocket已关闭!")
}
//连接关闭的回调方法
websocket.onclose = function() {
setMessageInnerHTML("WebSocket连接关闭");
}
//监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
window.onbeforeunload = function() {
closeWebSocket();
}
//关闭WebSocket连接
function closeWebSocket() {
websocket.close();
}
//将消息显示在网页上
function setMessageInnerHTML(innerHTML) {
document.getElementById('message').innerHTML += innerHTML + '
';
}
2.服务端
import java.io.IOException;
import java.util.Map;
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.JSONObject;
@ServerEndpoint("/webSocket/")
public class WebSocket {
private static int onlineCount = 0;
private static Map clients = new ConcurrentHashMap();
private Session session;
private String userid;
@OnOpen
public void onOpen(@PathParam("userid") String userid, Session session) throws IOException {
this.userid = userid;
this.session = session;
addOnlineCount();
clients.put(userid, this);
System.out.println("已连接");
}
@OnClose
public void onClose() throws IOException {
clients.remove(userid);
subOnlineCount();
}
@OnMessage
public void onMessage(String message) throws IOException {
JSONObject jsonTo = JSONObject.parseObject(message);
String mes = (String) jsonTo.get("message");
if (!jsonTo.get("To").equals("All")){
sendMessageTo(mes, jsonTo.get("To").toString());
}else{
sendMessageAll("给所有人");
}
}
@OnError
public void onError(Session session, Throwable error) {
error.printStackTrace();
}
public void sendMessageTo(String message, String To) throws IOException {
// session.getBasicRemote().sendText(message);
//session.getAsyncRemote().sendText(message);
for (WebSocket item : clients.values()) {
if (item.userid.equals(To) )
item.session.getAsyncRemote().sendText(message);
}
}
public void sendMessageAll(String message) throws IOException {
for (WebSocket item : clients.values()) {
item.session.getAsyncRemote().sendText(message);
}
}
public static synchronized int getOnlineCount() {
return onlineCount;
}
public static synchronized void addOnlineCount() {
WebSocket.onlineCount++;
}
public static synchronized void subOnlineCount() {
WebSocket.onlineCount--;
}
public static synchronized Map getClients() {
return clients;
}
3.调用
WebSocket ws = new WebSocket();
JSONObject jo = new JSONObject();
jo.put("message", "这是后台返回的消息!");
jo.put("To", UserUtils.getUser().getId());
ws.onMessage(jo.toString());
4.遇到的问题
web容器换成tomcat就行了,参考文章:https://www.oschina.net/question/1756518_238747
本来以为websocket可以和http协议一样传参,于是这样写了:
websocket = new WebSocket('ws://'+host+'/iats/webSocket/?userid='+userid);
然而不能这样传,改成这样就行了
客户端:
websocket = new WebSocket('ws://'+host+'/iats/webSocket/'+userid);
服务端:
@ServerEndpoint("/webSocket/{userid}")
传多个参:
websocket = new WebSocket('ws://'+host+'/iats/webSocket/'+userid+'/'+param);
@ServerEndpoint("/webSocket/{userid}/{param}")
public void onOpen(@PathParam("userid") String userid, @PathParam("param") String param, Session session) throws IOException {
产生原因貌似是:发送数据频繁而导致发送重叠。 我的websocket使用场景是自动化测试中,对代码进行分析,将分析日志同步推送到页面展示。所以确实会每秒推送大量信息。
解决方案:
item.session.getAsyncRemote().sendText(message);
改为
item.session.getBasicRemote().sendText(message);
AsyncRemote是异步推送,BasicRemote为同步推送,同步推送中,前面一条没推送完时,会阻塞推送。一般使用情况下,比如做的是页面聊天系统之类的,推荐用异步推送。然而我的运用场景反而用同步推送比较适合,所以改为BasicRemote就不报错了