【前言】
上一篇博客已经讲解了如何在APP端调用websocket,接收消息和发送消息。点击直达 接下来讲解一下websocket的服务端如何写。websocket的服务端,简单说就是创建链接,然后接收消息,发送消息,关闭链接。 当然发送消息的方法也可以当做一个普通的方法被其他类调用。
【正文】
pom文件引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
创建websocket实现类
连接websocket
@OnOpen
public void onOpen(Session session){
log.info("webSocket消息,有新的连接");
this.session = session;
webSocketSet.add(this);
}
关闭websocket
@OnClose
public void onClose(){
try {
session.close();
} catch (IOException e) {
log.error("关闭链接异常",e);
}finally {
try {
session.close();
} catch (IOException e) {
log.error("关闭链接异常finally方法",e);
}
webSocketSet.remove(this);
}
log.info("webSocket消息,连接断开");
}
接收消息
@OnMessage
public void onMessage(String message){
log.info("webSocket消息,收到客户端发来的消息",message);
sendMessage(message);
}
发送消息
public void sendMessage(String message){
for (WebSocket webSocket: webSocketSet){
try {
webSocket.session.getBasicRemote().sendText(message);
}catch (Exception e){
log.error("发送消息异常",e);
log.error("在线连接的数量:"+webSocketSet.size());
webSocketSeterror.add(webSocket);
}
}
for (WebSocket w :webSocketSeterror) {
webSocketSet.remove(w);
}
if(webSocketSeterror.size() > 0 ){
log.error("需要移除的session对象个数:"+webSocketSeterror.size());
}
webSocketSeterror.clear();
}
全部代码
package com.tfjy.arprobackend.service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;
@Component
@ServerEndpoint("/webSocket")
@Slf4j
public class WebSocket {
private Session session;
private static CopyOnWriteArraySet<WebSocket> webSocketSet = new CopyOnWriteArraySet<>();
private static CopyOnWriteArraySet<WebSocket> webSocketSeterror = new CopyOnWriteArraySet<>();
@OnOpen
public void onOpen(Session session){
this.session = session;
webSocketSet.add(this);
}
@OnClose
public void onClose(){
try {
session.close();
} catch (IOException e) {
log.error("关闭链接异常",e);
}finally {
try {
session.close();
} catch (IOException e) {
log.error("关闭链接异常finally方法",e);
}
webSocketSet.remove(this);
}
}
@OnMessage
public void onMessage(String message){
sendMessage(message);
}
public void sendMessage(String message){
for (WebSocket webSocket: webSocketSet){
try {
webSocket.session.getBasicRemote().sendText(message);
}catch (Exception e){
log.error("发送消息异常",e);
log.error("在线连接的数量:"+webSocketSet.size());
webSocketSeterror.add(webSocket);
}
}
for (WebSocket w :webSocketSeterror) {
webSocketSet.remove(w);
}
if(webSocketSeterror.size() > 0 ){
log.error("需要移除的session对象个数:"+webSocketSeterror.size());
}
webSocketSeterror.clear();
}
}
其他类调用websocket发送消息
直接调用websocket实现类的sendMessage方法就可以了。
webSocket.sendMessage(activityParameterModel.getCourseId() + activityParameterModel.getClassId() + CommonConfigurationUtils.SUCCESS);
|