Java Code Examples for org.springframework.web.socket.WebSocketSession#isOpen()
The following examples show how to use
org.springframework.web.socket.WebSocketSession#isOpen() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Handler.java From kurento-tutorial-java with Apache License 2.0 | 6 votes |
private synchronized void sendMessage(final WebSocketSession session, String message) { log.debug("[Handler::sendMessage] {}", message); if (!session.isOpen()) { log.warn("[Handler::sendMessage] Skip, WebSocket session isn't open"); return; } final String sessionId = session.getId(); if (!users.containsKey(sessionId)) { log.warn("[Handler::sendMessage] Skip, unknown user, id: {}", sessionId); return; } try { session.sendMessage(new TextMessage(message)); } catch (IOException ex) { log.error("[Handler::sendMessage] Exception: {}", ex.getMessage()); } }
Example 2
Source File: MyWebSocketHandler.java From albert with MIT License | 6 votes |
/** * 消息传输错误处理 */ public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception { if (session.isOpen()) { session.close(); } Iterator<Entry<Integer, WebSocketSession>> it = userSocketSessionMap.entrySet().iterator(); // 移除当前抛出异常用户的Socket会话 while (it.hasNext()) { Entry<Integer, WebSocketSession> entry = it.next(); if (entry.getValue().getId().equals(session.getId())) { userSocketSessionMap.remove(entry.getKey()); System.out.println("Socket会话已经移除:用户ID" + entry.getKey()); String username=userService.getNameById(entry.getKey()); Message msg = new Message(); msg.setFrom(-2); msg.setText(username); this.broadcast(new TextMessage(new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create().toJson(msg))); break; } } }
Example 3
Source File: WsContent.java From proxyee-down with Apache License 2.0 | 6 votes |
public void sendMsg(WsForm wsForm) { try { if (wsForm == null) { return; } TextMessage message = new TextMessage(JSON.toJSONString(wsForm)); for (Entry<String, WebSocketSession> entry : wcContent.entrySet()) { WebSocketSession session = entry.getValue(); if (session.isOpen()) { synchronized (session) { session.sendMessage(message); } } } } catch (Exception e) { LOGGER.warn("sendMsg", e); } }
Example 4
Source File: SocketHandler.java From SpringBootBucket with MIT License | 5 votes |
/** * 发送消息给指定的用户 */ private void sendMessageToUser(WebSocketSession user, TextMessage message) { try { // 在线就发送 if (user.isOpen()) { user.sendMessage(message); } } catch (IOException e) { logger.error("发送消息给指定的用户出错", e); } }
Example 5
Source File: WebSocketServerHandler.java From redtorch with MIT License | 5 votes |
@Override public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception { int nodeId = (int) session.getAttributes().get(RtConstant.KEY_NODE_ID); logger.error("传输错误,节点ID:{},会话ID:{}", nodeId, session.getId(), exception); try { if (session.isOpen()) { session.close(); } } catch (Exception e) { logger.error("关闭会话错误,节点ID:{},会话ID:{}", nodeId, session.getId(), exception); } }
Example 6
Source File: SocketHandler.java From SpringBootBucket with MIT License | 5 votes |
@Override public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { if (session.isOpen()) { session.close(); } sessions.remove(session); logger.info(String.format("Session %s closed because of %s", session.getId(), status.getReason())); }
Example 7
Source File: ChatWebSocketHandler.java From mySSM with MIT License | 5 votes |
@Override protected void handleTextMessage(WebSocketSession session, TextMessage message) { // System.out.println(session.getId()+":send...."); // chatTextMessageHandler(message.getPayload()); try { // super.handleTextMessage(session, message); // System.out.println(session.getId()+" :"+message.getPayload() + " " + new Date()); String m = message.getPayload(); String[] wsk = m.split(","); String phone = wsk[0]; long time = Long.parseLong(wsk[1]); String action = wsk[2]; if (action.equals("start")){ session.sendMessage(new TextMessage("success")); SaveSession.getInstance().save(phone,time); return; } boolean b = SaveSession.getInstance().isHave(phone,time); if (b) { if (session.isOpen()) { session.sendMessage(new TextMessage("error")); } } else { if (session.isOpen()) session.sendMessage(new TextMessage("success")); } } catch (Exception e) { e.printStackTrace(); try { session.sendMessage(new TextMessage("error")); } catch (IOException e1) { e1.printStackTrace(); } } }
Example 8
Source File: ApolloProtocolHandler.java From graphql-spqr-spring-boot-starter with Apache License 2.0 | 5 votes |
private Runnable keepAliveTask(WebSocketSession session) { return () -> { try { if (session != null && session.isOpen()) { session.sendMessage(ApolloMessage.keepAlive()); } } catch (Exception exception) { try { session.close(CloseStatus.SESSION_NOT_RELIABLE); } catch (Exception ignored) {/*no-op*/} } }; }
Example 9
Source File: ActiveThreadCountHandler.java From pinpoint with Apache License 2.0 | 5 votes |
@Override public void run() { try { if (logger.isDebugEnabled()) { logger.debug("HealthCheckTimerTask started."); } // check session state. List<WebSocketSession> snapshot = filterHealthCheckSuccess(sessionRepository); // send healthCheck packet String pingTextMessage = messageConverter.getPingTextMessage(); TextMessage pingMessage = new TextMessage(pingTextMessage); for (WebSocketSession session : snapshot) { if (!session.isOpen()) { continue; } // reset healthCheck state final WebSocketSessionContext sessionContext = getSessionContext(session); sessionContext.changeHealthCheckFail(); sendPingMessage(session, pingMessage); } } finally { if (healthCheckTimer != null && onTimerTask.get()) { healthCheckTimer.schedule(new HealthCheckTimerTask(), healthCheckDelay); } } }
Example 10
Source File: WebSocketService.java From FlyCms with MIT License | 5 votes |
/** * 给所有的用户发送消息 */ public void sendMessagesToUsers(TextMessage message) { for (WebSocketSession user : users) { try { // isOpen()在线就发送 if (user.isOpen()) { user.sendMessage(message); } } catch (IOException e) { e.printStackTrace(); } } }
Example 11
Source File: WebSocketService.java From FlyCms with MIT License | 5 votes |
@Override public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception { if (session.isOpen()) { session.close(); } users.remove(session); System.out.println("安全退出了系统"); System.out.println("======当前剩余在线用户========="+users.size()); }
Example 12
Source File: SubProtocolWebSocketHandler.java From java-technology-stack with MIT License | 5 votes |
@Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { // WebSocketHandlerDecorator could close the session if (!session.isOpen()) { return; } this.stats.incrementSessionCount(session); session = decorateSession(session); this.sessions.put(session.getId(), new WebSocketSessionHolder(session)); findProtocolHandler(session).afterSessionStarted(session, this.clientInboundChannel); }
Example 13
Source File: ChatWebSocketHandler.java From spring-boot-starter-samples with Apache License 2.0 | 5 votes |
@Override public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception { if(session.isOpen()){ session.close(); } logger.debug("websocket chat connection closed......"); sessions.remove(session); }
Example 14
Source File: SocketSessionUtil.java From Jpom with MIT License | 5 votes |
public static void send(WebSocketSession session, String msg) throws IOException { if (StrUtil.isEmpty(msg)) { return; } if (!session.isOpen()) { throw new RuntimeException("session close "); } try { LOCK.lock(session.getId()); IOException exception = null; int tryCount = 0; do { tryCount++; if (exception != null) { // 上一次有异常、休眠 500 try { Thread.sleep(500); } catch (InterruptedException ignored) { } } try { session.sendMessage(new TextMessage(msg)); exception = null; break; } catch (IOException e) { DefaultSystemLog.getLog().error("发送消息失败:" + tryCount, e); exception = e; } } while (tryCount <= ERROR_TRY_COUNT); if (exception != null) { throw exception; } } finally { LOCK.unlock(session.getId()); } }
Example 15
Source File: ChatWebSocketHandler.java From mySSM with MIT License | 5 votes |
@Override public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception { if (session.isOpen()) { session.close(); } sessions.remove(session); }
Example 16
Source File: SshHandler.java From Jpom with MIT License | 5 votes |
@Override public void destroy(WebSocketSession session) { try { if (session.isOpen()) { session.close(); } } catch (IOException ignored) { } HandlerItem handlerItem = HANDLER_ITEM_CONCURRENT_HASH_MAP.get(session.getId()); IoUtil.close(handlerItem.inputStream); IoUtil.close(handlerItem.outputStream); JschUtil.close(handlerItem.channel); JschUtil.close(handlerItem.openSession); }
Example 17
Source File: SubProtocolWebSocketHandler.java From spring-analysis-note with MIT License | 5 votes |
@Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { // WebSocketHandlerDecorator could close the session if (!session.isOpen()) { return; } this.stats.incrementSessionCount(session); session = decorateSession(session); this.sessions.put(session.getId(), new WebSocketSessionHolder(session)); findProtocolHandler(session).afterSessionStarted(session, this.clientInboundChannel); }
Example 18
Source File: SessionMonitor.java From devicehive-java-server with Apache License 2.0 | 4 votes |
public WebSocketSession getSession(String sessionId) { WebSocketSession session = sessionMap.get(sessionId); return session != null && session.isOpen() ? session : null; }
Example 19
Source File: MyWebSocketHandler.java From albert with MIT License | 3 votes |
/** * 给某个用户发送消息 * * @param userName * @param message * @throws IOException */ public void sendMessageToUser(Integer uid, TextMessage message) throws IOException { WebSocketSession session = userSocketSessionMap.get(uid); if (session != null && session.isOpen()) { session.sendMessage(message); } }
Example 20
Source File: GlobalSession.java From seppb with MIT License | 3 votes |
/** * 向客户端发送消息 * * @param webSocketSession * @param message (需要json格式的字符串,方便客户端处理) * @throws IOException */ public static void sendMessage(WebSocketSession webSocketSession, String message) throws IOException { TextMessage textMessage = new TextMessage(message); if (webSocketSession.isOpen()) { webSocketSession.sendMessage(textMessage); } }