parent
a7e9adf02a
commit
7c75938263
7 changed files with 268 additions and 24 deletions
@ -0,0 +1,63 @@ |
||||
package org.jeecg.modules.tablex.ws; |
||||
|
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import javax.websocket.*; |
||||
import javax.websocket.server.PathParam; |
||||
import javax.websocket.server.ServerEndpoint; |
||||
import java.util.Map; |
||||
import java.util.concurrent.ConcurrentHashMap; |
||||
|
||||
@Slf4j |
||||
@Component |
||||
@ServerEndpoint(value = "/uploadProcess/{val}") |
||||
public class MyWebSocket { |
||||
|
||||
|
||||
/** |
||||
* 已经建立连接的对话,ConcurrentHashMap是一个线程安全的Map |
||||
*/ |
||||
private static final Map<String, Session> sessionPool = |
||||
new ConcurrentHashMap<>(); |
||||
|
||||
|
||||
/** |
||||
* 连接建立成功触发 |
||||
* |
||||
* @param session 会话 |
||||
*/ |
||||
@OnOpen |
||||
public void onOpen(Session session, EndpointConfig endpointConfig, |
||||
@PathParam("val") String key) { |
||||
// 将session进行保存
|
||||
sessionPool.put(key, session); |
||||
log.info("创建一个连接,当前连接的连接数量是:{}", sessionPool.size()); |
||||
|
||||
} |
||||
|
||||
/** |
||||
* 发送消息 |
||||
* |
||||
* @param info 信息 |
||||
*/ |
||||
@OnMessage |
||||
public void onMessage(String info) { |
||||
log.info("接受到的消息是:{}", info); |
||||
} |
||||
|
||||
public Map<String, Session> getSessionPool() { |
||||
return sessionPool; |
||||
} |
||||
|
||||
/** |
||||
* 关闭连接时触发 |
||||
* |
||||
* @param session 会话 |
||||
*/ |
||||
@OnClose |
||||
public void onClose(Session session, @PathParam("val") String key) { |
||||
sessionPool.remove(key); |
||||
log.info("关闭一个连接,当前剩余连接数为:{}", sessionPool.size()); |
||||
} |
||||
} |
@ -0,0 +1,53 @@ |
||||
package org.jeecg.modules.tablex.ws; |
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import javax.annotation.PostConstruct; |
||||
import javax.websocket.Session; |
||||
import java.util.Map; |
||||
|
||||
@Slf4j |
||||
@Component |
||||
public class WebSocketUtils { |
||||
@Autowired |
||||
MyWebSocket webSocket; |
||||
|
||||
|
||||
private static Map<String, Session> sessionPool; |
||||
|
||||
|
||||
@PostConstruct |
||||
private void init() { |
||||
sessionPool = webSocket.getSessionPool(); |
||||
} |
||||
|
||||
|
||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); |
||||
|
||||
|
||||
public void sendMessage(String key, Map<String, String> message) { |
||||
Session session = sessionPool.get(key); |
||||
try { |
||||
session.getBasicRemote() |
||||
.sendText(OBJECT_MAPPER |
||||
.writeValueAsString(message)); |
||||
} catch (Exception e) { |
||||
log.error(e.getMessage()); |
||||
} |
||||
} |
||||
|
||||
public void sendAllMessage(Map<String, String> message) { |
||||
sessionPool.values().forEach(session -> { |
||||
try { |
||||
session.getBasicRemote() |
||||
.sendText(OBJECT_MAPPER |
||||
.writeValueAsString(message)); |
||||
} catch (Exception e) { |
||||
log.error(e.getMessage()); |
||||
} |
||||
}); |
||||
} |
||||
} |
Loading…
Reference in new issue