引入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.13.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@Component
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
import com.alibaba.fastjson.JSONObject;
import com.ndsoft.single.bizservice.repository.MessageInfoRepository;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.*;
@ServerEndpoint("/webSocket/talk/{userId}/{acceptId}")
@Service
public class WebSocketService {
private Session session;
// private static CopyOnWriteArraySet<WebSocketService> webSocketSet = new CopyOnWriteArraySet<WebSocketService>();
private static MessageInfoRepository messageInfoRepository;
@Autowired
public void setMessageInfoRepository(MessageInfoRepository messageInfoRepository) {
WebSocketService.messageInfoRepository = messageInfoRepository;
}
private static Map<String, WebSocketService> userMap = new HashMap<>();
private String acceptId;
private String userId;
public static RedisService redisService;
private MessageInfo messageInfo;
private String infoKey;
private final static String PREFIX = "MSG";
/**
* 建立连接
*
* @Author : yyc
* @CreateDate : 2020/8/9
*/
@OnOpen
public void onOpen(@PathParam(value = "userId") String userId, @PathParam(value = "acceptId") String acceptId, Session session) throws IOException{
// 连接成功 发送消息
this.session = session;
this.acceptId = acceptId;
this.userId = userId;
userMap.put(PREFIX + userId, this);
sendBasicMesszzzage("连接成功");
}
@OnMessage
public void onMessage(String message)throws IOException {
WebSocketService webSocketTest = userMap.get(PREFIX + acceptId);
if (webSocketTest != null) {
webSocketTest.sendAsyncMessage(content);
}
}
@OnClose//关闭连接执行
public void onClose(Session session) {}
@OnError//连接错误的时候执行
public void onError(Throwable error, Session session) {error.printStackTrace();}
/**
* 异步发送消息
*
*/
private void sendAsyncMessage(String message) throws IOException {this.session.getAsyncRemote().sendText(message);}
/**
* 同步发送消息
*
*/
private void sendBasicMessage(String message) throws IOException {this.session.getBasicRemote().sendText(message);}
}