public StandardWebSocketSession(Session session, HandshakeInfo info, DataBufferFactory factory, @Nullable MonoProcessor<Void> completionMono) { super(session, session.getId(), info, factory, completionMono); }
@SuppressWarnings("MethodMayBeStatic") @OnError public void onError(final Session session, final Throwable t) { log.error("websocket error in session {}", session.getId(), t); }
@Override public void onError(final Throwable e) { log.error("error in session {}", session.getId(), e); SessionUtil.closeSilently(session, CloseCodes.UNEXPECTED_CONDITION, "streams exception"); }
static void closeSilently( final Session session, final CloseCodes code, final String message) { try { session.close(new CloseReason(code, truncate(message))); } catch (final Exception e) { LOG.info("Exception caught closing session {}", session.getId(), e); } }
@Override public void onNext(final Collection<T> rows) { for (final T row : rows) { // check if session is closed inside the loop to avoid // logging too many async callback errors after close if (!closed) { try { final String buffer = mapper.writeValueAsString(row); session.getAsyncRemote().sendText( buffer, result -> { if (!result.isOK()) { log.warn( "Error sending websocket message for session {}", session.getId(), result.getException() ); } }); } catch (final JsonProcessingException e) { log.warn("Error serializing row in session {}", session.getId(), e); } } } if (!closed) { subscription.request(1); } }
@OnClose public void onClose(final Session session, final CloseReason closeReason) { if (subscriber != null) { subscriber.close(); } log.debug( "Closing websocket session {} ({}): {}", session.getId(), closeReason.getCloseCode(), closeReason.getReasonPhrase() ); }
@Before public void setUp() { query = new Query(queryBody, Optional.empty()); when(session.getId()).thenReturn("session-id"); when(statementParser.parseSingleStatement(anyString())) .thenAnswer(invocation -> new PreparedStatement<>(invocation.getArgument(0).toString(), query)); when(serviceContext.getSchemaRegistryClient()).thenReturn(schemaRegistryClient); when(serviceContext.getTopicClient()).thenReturn(topicClient); when(ksqlEngine.isAcceptingStatements()).thenReturn(true); givenRequest(VALID_REQUEST); wsQueryEndpoint = new WSQueryEndpoint( ksqlConfig, OBJECT_MAPPER, statementParser, ksqlEngine, serviceContext, commandQueue, exec, queryPublisher, topicPublisher, activenessRegistrar, COMMAND_QUEUE_CATCHUP_TIMEOUT); }
@SuppressWarnings("unused") @OnOpen public void onOpen(final Session session, final EndpointConfig unused) { log.debug("Opening websocket session {}", session.getId()); if (!ksqlEngine.isAcceptingStatements()) { log.info("The cluster has been terminated. No new request will be accepted.");
@Test public void testOnError() throws Exception { replayOnSubscribe(); final Capture<CloseReason> reason = EasyMock.newCapture(); EasyMock.expect(session.getId()).andReturn("abc123").once(); session.close(EasyMock.capture(reason)); EasyMock.expectLastCall().once(); subscription.cancel(); EasyMock.expectLastCall().once(); EasyMock.replay(subscription, session); subscriber.onError(new RuntimeException("streams died")); subscriber.close(); assertEquals("streams exception", reason.getValue().getReasonPhrase()); assertEquals(CloseCodes.UNEXPECTED_CONDITION, reason.getValue().getCloseCode()); EasyMock.verify(subscription, session); } }
@OnClose public void end(Session wsSession) { current.set(wsSession); try { CRaSHSession session = sessions.remove(wsSession.getId()); if (session != null) { log.fine("Destroying session " + wsSession.getId()); WSProcessContext current = session.current.getAndSet(null); if (current != null) { log.fine("Cancelling on going command " + current.command + " for " + wsSession.getId()); current.process.cancel(); } } else { log.fine("No shell session found"); } } finally { current.set(null); } }
/** * 当关闭连接:1.移除会话对象 2.更新在线人数 */ @OnClose public void onClose(Session session) { onlineSessions.remove(session.getId()); sendMessageToAll(Message.jsonStr(Message.QUIT, "", "", onlineSessions.size())); }
/** * 当客户端打开连接:1.添加会话对象 2.更新在线人数 */ @OnOpen public void onOpen(Session session) { onlineSessions.put(session.getId(), session); sendMessageToAll(Message.jsonStr(Message.ENTER, "", "", onlineSessions.size())); }
@OnClose public void onClose(@PathParam("username") String username, Session session) { String sessionId = session.getId(); //当前的Session 移除 livingSessions.remove(sessionId); //并且通知其他人当前用户已经离开聊天室了 sendTextAll("用户[" + username + "] 已经离开聊天室了!"); }
@OnOpen public void openSession(@PathParam("username") String username, Session session) { String sessionId = session.getId(); livingSessions.put(sessionId, session); sendTextAll("欢迎用户[" + username + "] 来到聊天室!"); }
@OnOpen public void start(Session wsSession) { current.set(wsSession); try { URI uri = wsSession.getRequestURI(); String path = uri.getPath(); log.fine("Establishing session for " + path); String contextPath = path.substring(0, path.lastIndexOf('/')); PluginContext context = WebPluginLifeCycle.getPluginContext(contextPath); if (context != null) { Boolean enabled = context.getProperty(WebPlugin.ENABLED); if (enabled != null && enabled) { log.fine("Using shell " + context); ShellFactory factory = context.getPlugin(ShellFactory.class); Principal user = wsSession.getUserPrincipal(); Shell shell = factory.create(user); CRaSHSession session = new CRaSHSession(wsSession, shell); sessions.put(wsSession.getId(), session); log.fine("Established session " + wsSession.getId()); } else { log.fine("Web plugin disabled"); } } else { log.fine("No shell found"); } } finally { current.set(null); } }
@OnMessage public void incoming(String message, Session wsSession) { String key = wsSession.getId(); log.fine("Received message " + message + " from session " + key); current.set(wsSession);
@Override public void onError(Session session, Throwable t) { LOG.error("An error occurred in web socket connection with id : " + session.getId(), t); super.onError(session, t); if (isApplicationAlive()) { javaxWebSocketProcessor.onError(t); } }
@OnMessage public void onTextMessage(@PathParam("name") String name, String text, Session session) throws IOException { String msg = name + ":" + text; LOGGER.info("Received Text: " + text + " from " + name + session.getId()); sendMessageToAll(msg); }
@OnMessage public void onTextMessage(@PathParam("name") String name, String text, Session session) throws IOException { String msg = name + ":" + text; LOGGER.info("Received Text: " + text + " from " + name + session.getId()); sendMessageToAll(msg); }
@OnMessage public void onTextMessage(@PathParam("name") String name, String text, Session session) throws IOException { String msg = name + ":" + text; log.info("Received Text: " + text + " from " + name + session.getId()); sendMessageToAll(msg); }