webSocket是HTML5的一种新协议,它实现了服务端与客户端的全双工通信,建立在传输层,tcp协议之上,即浏览器与服务端需要先建立tcp协议,再发送webSocket连接建立请求。webSocket的连接:客户端发送请求信息,服务端接受到请求并返回相应的信息。连接建立。客户端发送http请求时,通过 Upgrade:webSocket Connection:Upgrade 告知服务器需要建立的是webSocket连接,并且还会传递webSocket版本号,协议的字版本号,原始地址,主机地址, webSocket相互通信的Header很小,大概只有2Bytes
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
@Configuration
public class WebsocketConfiguration {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
@ServerEndpoint("/test")
@Component
@Slf4j
public class MyWebsocketServer {
/**
* 存放所有在线的客户端
*/
private static Map<String, Session> clients = new ConcurrentHashMap<>();
@OnOpen
public void onOpen(Session session) {
log.info("有新的客户端连接了: {}", session.getId());
//将新用户存入在线的组
clients.put(session.getId(), session);
}
/**
* 客户端关闭
* @param session session
*/
@OnClose
public void onClose(Session session) {
log.info("有用户断开了, id为:{}", session.getId());
//将掉线的用户移除在线的组里
clients.remove(session.getId());
}
/**
* 发生错误
* @param throwable e
*/
@OnError
public void onError(Throwable throwable) {
throwable.printStackTrace();
}
/**
* 收到客户端发来消息
* @param message 消息对象
*/
@OnMessage
public void onMessage(String message) {
log.info("服务端收到客户端发来的消息: {}", message);
this.sendAll(message);
}
/**
* 群发消息
* @param message 消息内容
*/
private void sendAll(String message) {
for (Map.Entry<String, Session> sessionEntry : clients.entrySet()) {
sessionEntry.getValue().getAsyncRemote().sendText(message);
}
}
}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
@Configuration
public class WebsocketConfiguration {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Message {
private String userId;
private String message;
}
@ServerEndpoint("/test-one")
@Component
@Slf4j
public class MyOneToOneServer {
/**
* 用于存放所有在线客户端
*/
private static Map<String, Session> clients = new ConcurrentHashMap<>();
private Gson gson = new Gson();
@OnOpen
public void onOpen(Session session) {
log.info("有新的客户端上线: {}", session.getId());
clients.put(session.getId(), session);
}
@OnClose
public void onClose(Session session) {
String sessionId = session.getId();
log.info("有客户端离线: {}", sessionId);
clients.remove(sessionId);
}
@OnError
public void onError(Session session, Throwable throwable) {
throwable.printStackTrace();
if (clients.get(session.getId()) != null) {
clients.remove(session.getId());
}
}
@OnMessage
public void onMessage(String message) {
log.info("收到客户端发来的消息: {}", message);
this.sendTo(gson.fromJson(message, Message.class));
}
/**
* 发送消息
*
* @param message 消息对象
*/
private void sendTo(Message message) {
Session s = clients.get(message.getUserId());
if (s != null) {
try {
s.getBasicRemote().sendText(message.getMessage());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://lebron.blog.csdn.net/article/details/123161584
内容来源于网络,如有侵权,请联系作者删除!