先上代码再看调试结果:
package com.qiang.user.util;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @ClassName WebSocket
* @Description TODO
* @Author Hbl
* @Date 2019/10/21 13:45
* @Version 1.0
*/
@Component
@ServerEndpoint(value = "/webSocket/{account}")
public class WebSocket {
// 静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
private static int onlineCount = 0;
// concurrent包的线程安全Map,用来存放每个客户端对应的MyWebSocket对象。
private static Map<String, WebSocket> clients = new ConcurrentHashMap<>();
// 与某个客户端的连接会话,需要通过它来给客户端发送数据
private Session session;
private String account;
/**
* 连接建立成功调用的方法
*/
@OnOpen
public void onOpen(@PathParam("account") String account, Session session) throws IOException {
this.account = account;
this.session = session;
addOnlineCount();
clients.put(account, this);
System.out.println("当前连接用户数量:"+getOnlineCount());
System.out.println("已连接,客户连接id:" + account + " ,session:" + session);
}
/**
* 连接关闭调用的方法
*/
@OnClose
public void onClose() throws IOException {
System.out.println("连接已关闭,被关闭的id为:" + account);
clients.remove(account);
subOnlineCount();
System.out.println("关闭后当前连接用户数量:"+getOnlineCount());
}
/**
* 收到客户端消息后调用的方法
*
* @param message 客户端发送过来的消息
*/
@OnMessage
public void onMessage(String message) throws IOException, InterruptedException {
JSONObject jsonTo = JSONObject.parseObject(message);
String ID = jsonTo.getString("id");
String msg = jsonTo.getString("msg");
System.out.println("客户发送过来的消息:" + jsonTo + "客户的id:" + ID);
Thread.sleep(2000);
sendMessageTo("恭喜你成功了!您的Id是:" + ID + ",对吗?", ID);
System.out.println("恭喜你成功了!");
Thread.sleep(2000);
sendMessageTo("恭喜你再次成功了!您发过来的消息是:" + msg + "", ID);
System.out.println("恭喜你再次成功了!");
if (ID.equals("200")){
sendMessageAll("这是后台给所有人发送的消息,发送消息人的ID为:"+ID);
System.out.println("这是后台给所有人发送的消息,发送消息人的ID为:"+ID);
}
}
/**
* 发生错误时调用
*/
@OnError
public void onError(Session session, Throwable error) {
error.printStackTrace();
}
/**
* 像当前客户端发送消息
*
* @param message 字符串消息
* @throws IOException
*/
public void sendMessageTo(String message, String To) throws IOException {
// session.getBasicRemote().sendText(message);
//session.getAsyncRemote().sendText(message);
for (WebSocket item : clients.values()) {
if (item.account.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<String, WebSocket> getClients() {
return clients;
}
}
前端来连接几个测试看下打印:
测试下发送消息:
关闭连接测试:
测试成功!
前端代码请看下面这篇文章: