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 servlet.Log;
@ServerEndpoint(value = “/websocket/{clientId}”)
public class WebSocketServer {
private final Log log = new Log(WebSocketServer.class);
private Session session;
private String clientId;
private static Map<String, WebSocketServer> clients = new ConcurrentHashMap<String, WebSocketServer>();
// 連接配接時執行
@OnOpen
public void onOpen(@PathParam("clientId") String clientId, Session session) throws IOException {
this.session = session;
this.clientId = clientId;
clients.put(clientId, this);
log.info("新連接配接:" + clientId);
}
// 關閉時執行
@OnClose
public void onClose(@PathParam("clientId") String clientId, Session session) {
clients.remove(clientId);
log.info("連接配接 " + clientId + " 關閉");
}
// 收到消息時執行
@OnMessage
public void onMessage(String message, Session session) throws IOException {
log.info("收到使用者的消息: "+ message);
/*if("getMpDefsAndRtDatas".equals(message)){
String msg = UnityServlet.getInstance().getAllMpDefsAndRtDatas();
this.sendMessage(session, msg);
}*/
}
// 連接配接錯誤時執行
@OnError
public void onError(@PathParam("clientId") String clientId, Throwable error, Session session) {
log.info("使用者id為:" + clientId + "的連接配接發送錯誤");
error.printStackTrace();
}
/**
* 發送消息給某個用戶端
* @param message
* @param To
* @throws IOException
*/
public static void sendMessageTo(String message, String To) throws IOException {
for (WebSocketServer item : clients.values()) {
if (item.clientId.equals(To))
item.session.getAsyncRemote().sendText(message);
}
}
/**
* 發送消息給某些用戶端
* @param message
* @param To
* @throws IOException
*/
public static void sendMessageToSomeone(String message, String To) throws IOException {
for (WebSocketServer item : clients.values()) {
if (item.clientId.startsWith(To))
item.session.getAsyncRemote().sendText(message);
}
}
/**
* 發送消息給所有用戶端
* @param message
* @throws IOException
*/
public static void sendMessageAll(String message) throws IOException {
for (WebSocketServer item : clients.values()) {
item.session.getAsyncRemote().sendText(message);
}
}
/**
* 發送消息
* @param session
* @param message
* @throws IOException
*/
private void sendMessage(Session session,String message) throws IOException{
session.getBasicRemote().sendText(message);
}
}
Java端發送請求指令
String clientId = “broadcast”;
try {
WebSocketServer.sendMessageTo("broadcast",clientId);
} catch (IOException e) {
e.printStackTrace();
}
C#用戶端設計
## 一線網際網路大廠Java核心面試題庫

正逢面試跳槽季,給大家整理了大廠問到的一些面試真題,由于文章長度限制,隻給大家展示了部分題目,更多Java基礎、異常、集合、并發程式設計、JVM、Spring全家桶、MyBatis、Redis、資料庫、中間件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等...已整理上傳,感興趣的朋友可以看看支援一波!
用戶端設計
## 一線網際網路大廠Java核心面試題庫
[外鍊圖檔轉存中...(img-R9uUYLB3-1630931508465)]
正逢面試跳槽季,給大家整理了大廠問到的一些面試真題,由于文章長度限制,隻給大家展示了部分題目,更多Java基礎、異常、集合、并發程式設計、JVM、Spring全家桶、MyBatis、Redis、資料庫、中間件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等...已整理上傳,感興趣的朋友可以看看支援一波!
**[CodeChina開源項目:【一線大廠Java面試題解析+核心總結學習筆記+最新講解視訊】](https://codechina.csdn.net/m0_60958482/java-p7)**