| 12 | |
| 13 | |
| 14 | @Service |
| 15 | @Slf4j |
| 16 | public class SocketService { |
| 17 | |
| 18 | /** |
| 19 | * 给指定用户发送信息 |
| 20 | * |
| 21 | * @param session session |
| 22 | * @param msg 发送的消息 |
| 23 | */ |
| 24 | public void sendMessage(Session session, String msg) { |
| 25 | synchronized (session) { |
| 26 | final RemoteEndpoint.Basic basic = session.getBasicRemote(); |
| 27 | if (basic == null) |
| 28 | return; |
| 29 | try { |
| 30 | basic.sendText(msg); |
| 31 | } catch (IOException e) { |
| 32 | log.error("消息发送异常,异常情况: {}", e.getMessage()); |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | public void sendMessageAll(String message, Long userId) { |
| 38 | log.info("广播:群发消息"); |
| 39 | // 遍历map |
| 40 | SocketPool.getSessionMap().forEach((keyId, session) -> { |
| 41 | if (!userId.equals(keyId)) { |
| 42 | sendMessage(session, message); |
| 43 | } |
| 44 | }); |
| 45 | } |
| 46 | } |
nothing calls this directly
no outgoing calls
no test coverage detected