【男性深夜解壓神器】
https://item.taobao.com/item.htm?spm=a21dvs.23580594.0.0.1d293d0dZgIXMJ&ft=t&id=742087626933文章來源:http://www.zghlxwxcb.cn/news/detail-712534.html
1. 依賴
<!-- SpringBoot WebSocket -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
2. 自動(dòng)注冊(cè)配置類
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@Configuration
public class WebSocketConfig {
/**
* ServerEndpointExporter 作用
*
* 這個(gè)Bean會(huì)自動(dòng)注冊(cè)使用@ServerEndpoint注解聲明的websocket endpoint
*
* @return
*/
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
3. WebSocket服務(wù)類
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.concurrent.CopyOnWriteArraySet;
/**
* postman訪問: ws://localhost:8080/ws/{userId}
* 瀏覽器訪問: http://localhost:8080/ws
*/
@Component
@ServerEndpoint("/ws/{userId}")
public class WebSocketServer {
private static int onlineCount = 0;
private static CopyOnWriteArraySet<WebSocketServer> webSocketSet = new CopyOnWriteArraySet<>();
private Session session;
private String userId = "";
@OnOpen
public void onOpen(Session session, @PathParam("userId") String userId) {
this.session = session;
webSocketSet.add(this); //加入set中
addOnlineCount(); //在線數(shù)加1
this.userId = userId;
sendMessage(userId + "用戶" + ", 連接成功 !");
System.out.println("【websocket】 " + userId + "用戶" + "已連接!當(dāng)前在線人數(shù)為" + getOnlineCount());
}
@OnClose
public void onClose() {
webSocketSet.remove(this); //從set中刪除
subOnlineCount(); //在線數(shù)減1
System.out.println("【websocket】 " + userId + "用戶" + "已關(guān)閉!當(dāng)前在線人數(shù)為" + getOnlineCount());
}
@OnMessage
public void onMessage(String message, Session session) {
if(message.startsWith("target-")){
int index = message.indexOf(":");
String userId = message.substring(7,index);
sendInfo(message.substring(index + 1), userId);
return;
}
this.session = session;
sendMessage("【websocket】 服務(wù)端收到來自窗口" + userId + "發(fā)送的消息:" + message);
}
@OnError
public void onError(Session session, Throwable error) {
this.session = session;
error.printStackTrace();
}
private void sendMessage(String message) {
try {
this.session.getBasicRemote().sendText(message);
} catch (IOException e) {
e.printStackTrace();
}
}
// 群發(fā)消息
/**
* 群發(fā)自定義消息
*/
public static void sendInfo(String message, @PathParam("userId") String userId) {
System.out.println("【websocket】 推送消息給" + userId + "用戶" + ",推送內(nèi)容:" + message);
for (WebSocketServer item : webSocketSet) {
//這里可以設(shè)定只推送給這個(gè)userId的,為null則全部推送
if (userId == null) {
// item.sendMessage(message);
} else if (item.userId.equals(userId)) {
item.sendMessage(message);
}
}
}
public static synchronized int getOnlineCount() {
return onlineCount;
}
public static synchronized void addOnlineCount() {
WebSocketServer.onlineCount++;
}
public static synchronized void subOnlineCount() {
WebSocketServer.onlineCount--;
}
public static CopyOnWriteArraySet<WebSocketServer> getWebSocketSet() {
return webSocketSet;
}
}
4. 模擬通信
文章來源地址http://www.zghlxwxcb.cn/news/detail-712534.html
【websocket】 2015用戶已連接!當(dāng)前在線人數(shù)為1
【websocket】 7349用戶已連接!當(dāng)前在線人數(shù)為2
【websocket】 推送消息給7349用戶,推送內(nèi)容:你好, 我是2015用戶, 很高興認(rèn)識(shí)你 !
【websocket】 推送消息給2015用戶,推送內(nèi)容:你好, 我是7349用戶, 我也很高興認(rèn)識(shí)你 !
?
訪問:http://localhost:8080/ws
可以打開多個(gè)
開始連接,一個(gè)客戶端給另一個(gè)客戶端發(fā)消息。
可以服務(wù)端推送消息給所有連接的客戶端
POST:http://localhost:8080/push
任意內(nèi)容
?
5. 需要完整代碼的可私信我
到了這里,關(guān)于SpringBoot 整合 Websocket 通信demo (附瀏覽器聊天窗口)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!