@Override protected void sendPongMessage(PongMessage message) throws IOException { getNativeSession().getBasicRemote().sendPong(message.getPayload()); }
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); } }
/** * Callback hook for Connection open events. * * @param userSession the userSession which is opened. */ @Override public void onOpen( Session userSession, EndpointConfig endpointConfig ) { this.userSession = userSession; this.userSession.setMaxTextMessageBufferSize( MAX_TXT_MSG_BUF_SIZE ); this.userSession.setMaxBinaryMessageBufferSize( MAX_BIN_MSG_BUF_SIZE ); userSession.addMessageHandler( new MessageHandler.Whole<Message>() { /** * Callback hook for Message Events. This method will be invoked when the server send a message. * * @param message The text message */ @Override public void onMessage( Message message ) { try { messageEventService.fireEvent( message ); } catch ( MessageEventFireEventException e ) { throw new RuntimeException( e ); } } } ); }
if (!session.isOpen()) { logger.trace("Session Closed {}", session); return; if (maxBinaryBufferSize != -1) session.setMaxBinaryMessageBufferSize(maxBinaryBufferSize); if (webSocketWriteTimeout != -1) session.setMaxIdleTimeout(webSocketWriteTimeout); if (maxTextBufferSize != -1) session.setMaxTextMessageBufferSize(maxTextBufferSize); URI uri = session.getRequestURI(); String rawPath = uri.getPath(); String contextPath = framework.getAtmosphereConfig().getServletContext().getContextPath(); origin = "http" + (session.isSecure() ? "s" : "") + "://0.0.0.0:80"; .pathInfo(pathInfo) .destroyable(false) .userPrincipal(session.getUserPrincipal()) .remoteInetSocketAddress(new Callable<InetSocketAddress>() { @Override .queryString(session.getQueryString()); session.close(new CloseReason(CloseReason.CloseCodes.CANNOT_ACCEPT, "Handshake not accepted.")); } catch (IOException e) { logger.trace("", e); if (session.isOpen()) { session.addMessageHandler(new MessageHandler.Whole<String>() { @Override
@Test public void testOnSchema() throws Exception { replayOnSubscribe(); session.getBasicRemote(); EasyMock.expectLastCall().andReturn(basic).once(); final Capture<String> schema = EasyMock.newCapture(); basic.sendText(EasyMock.capture(schema)); EasyMock.expectLastCall().andThrow(new IOException("bad bad io")).once(); final Capture<CloseReason> reason = EasyMock.newCapture(); session.close(EasyMock.capture(reason)); subscription.cancel(); EasyMock.replay(subscription, session, basic); subscriber.onSchema(SchemaBuilder .struct() .field("currency", Schema.STRING_SCHEMA) .field("amount", Schema.OPTIONAL_FLOAT32_SCHEMA) .build()); subscriber.close(); assertEquals( "[" + "{\"name\":\"currency\"," + "\"schema\":{\"type\":\"STRING\",\"fields\":null,\"memberSchema\":null}}," + "{\"name\":\"amount\"," + "\"schema\":{\"type\":\"DOUBLE\",\"fields\":null,\"memberSchema\":null}}]" , schema.getValue()); assertEquals("Unable to send schema", reason.getValue().getReasonPhrase()); assertEquals(CloseCodes.PROTOCOL_ERROR, reason.getValue().getCloseCode()); EasyMock.verify(subscription, session, basic); }
@OnOpen public void open(Session session){ log.info("Session created: "+session.getId()); clients.add(session); try { session.getBasicRemote().sendText("welcome "+session.getUserPrincipal()); } catch (IOException e) { log.log(Level.WARNING,"Unable to send welcome message", e); } // TODO check privileges and close connection if invalid } @OnClose
@Override @OnOpen public void onOpen(final Session session, EndpointConfig config) { remote = session.getBasicRemote(); session.addMessageHandler(new MessageHandler.Whole<String>() {
/** * On handshake completed, get the WebSocket Session and send * a message to ServerEndpoint that WebSocket is ready. * http://docs.saltstack.com/en/latest/ref/netapi/all/salt.netapi.rest_cherrypy.html#ws * * @param session The just started WebSocket {@link Session}. * @param config The {@link EndpointConfig} containing the handshake informations. * @throws IOException if something goes wrong sending message to the remote peer */ @OnOpen public void onOpen(Session session, EndpointConfig config) throws IOException { this.session = session; session.getBasicRemote().sendText("websocket client ready"); }
@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); } }
@Test public void shouldNotTruncateShortReasons() throws Exception { // Given: final String reason = "some short reason"; // When: SessionUtil.closeSilently(session, CloseCodes.CANNOT_ACCEPT, reason); // Then: verify(session).close(reasonCaptor.capture()); assertThat(reasonCaptor.getValue().getReasonPhrase(), is(reason)); }
/** * A callback when created a new websocket connection. */ @OnOpen public void onOpen(Session session) { logger.info("Connected ... " + session.getId()); try { session.getBasicRemote().sendText("start"); } catch (IOException e) { throw new RuntimeException(e); } }
/** * Close the session informing the reason. */ public void close( String message ) throws KettleException { sessionValid(); try { if ( this.userSession != null && this.userSession.isOpen() ) { this.userSession.close( new CloseReason( CloseReason.CloseCodes.NORMAL_CLOSURE, message ) ); } } catch ( IOException e ) { throw new KettleException( e ); } }
@OnOpen public void onWebSocketOpen(Session session) { this.session = session; this.remote = this.session.getAsyncRemote(); log.info("WebSocket Connect: {}",session); this.remote.sendText("You are now connected to " + this.getClass().getName()); }
@Override public void onOpen(Session session, EndpointConfig config) { try { session.addMessageHandler(new MessageHandler.Whole<String>() { session.getBasicRemote().sendText(""); } catch (IOException e) {
private void verifyClosedWithReason(final String reason, final CloseCodes code) throws Exception { verify(session).close(closeReasonCaptor.capture()); final CloseReason closeReason = closeReasonCaptor.getValue(); assertThat(closeReason.getReasonPhrase(), is(reason)); assertThat(closeReason.getCloseCode(), is(code)); }
@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() ); }
@OnMessage public void onMessage(String text, Session session) { try { session.getBasicRemote().sendText("Welcome to Stream Processor Studio"); LOGGER.info("Received message : " + text); } catch (IOException e) { LOGGER.error(e.getMessage()); } }