@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 WebSocket write(String s) throws IOException { if (!isOpen()) { throw new IOException("Socket closed {}"); } boolean acquired = false; try { acquired = semaphore.tryAcquire(writeTimeout, TimeUnit.MILLISECONDS); if (acquired) { session.getAsyncRemote().sendText(s, new WriteResult(resource(), s)); } else { throw new IOException("Socket closed"); } } catch (Throwable e) { if (IOException.class.isAssignableFrom(e.getClass())) { throw IOException.class.cast(e); } handleError(e, acquired); } return this; }
@Override public void send(String message) { int index = 0; while (message.length() - index > MAX_MESSAGE_SIZE) { int next = index + MAX_MESSAGE_SIZE; session.getAsyncRemote().sendText("," + message.substring(index, next)); index = next; } session.getAsyncRemote().sendText("." + message.substring(index)); }
/** * Send a message. * * @param message */ public void sendMessage(String message) { this.userSession.getAsyncRemote().sendText(message); }
@OnOpen public void onOpen(Session session, EndpointConfig config) { String msg = String.format("{\"ins\": \"%s\"}", ins); log.trace("onOpen: {}", msg); session.getAsyncRemote().sendText(msg); }
private static boolean webSockectPushMessage(Session session , String message) { if(session==null) return false; session.getAsyncRemote().sendText(message); return true; }
@Override public void run() { if (session.isOpen()) { session.getAsyncRemote().sendText("keep alive"); } else { keepAliveTimer.cancel(); } } }
@Override public void run() { if (session.isOpen()) { session.getAsyncRemote() .sendText("keep alive"); } else { keepAliveTimer.cancel(); } } }
@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()); }
public static void sendMessageAll(String message) throws IOException { Set<Map.Entry<String, MyWebSocketService>> entries = clients.entrySet(); for (Map.Entry<String, MyWebSocketService> item : entries) { item.getValue().session.getAsyncRemote().sendText(message); } }
@OnOpen public void onOpen(Session session, EndpointConfig config) { String msg = "{\"messageType\":\"logon\"}"; log.trace("onOpen: {}", msg); session.getAsyncRemote().sendText(msg); }
public Future<Void> removeSubscription(String metric) throws Exception { RemoveSubscription remove = new RemoveSubscription(); remove.setSubscriptionId(subscriptionId); remove.setMetric(metric); return session.getAsyncRemote().sendText(JsonSerializer.getObjectMapper().writeValueAsString(remove)); }
public synchronized void send(String message) { if (s != null && s.isOpen()) { s.getAsyncRemote().sendText(message); handler.handleSent(message); } }
public static void sendMessageTo(String message, String To) throws IOException { Set<Map.Entry<String, MyWebSocketService>> entries = clients.entrySet(); for (Map.Entry<String, MyWebSocketService> item : entries) { if (item.getKey().equals(To) ) { item.getValue().session.getAsyncRemote().sendText(message); } } }
public Future<Void> addSubscription(String metric, Map<String, String> tags, long startTime, long endTime, long delayTime) throws IOException { AddSubscription add = new AddSubscription(); add.setSubscriptionId(subscriptionId); add.setMetric(metric); add.setTags(Optional.ofNullable(tags)); add.setStartTime(Optional.ofNullable(startTime)); add.setEndTime(Optional.ofNullable(endTime)); add.setDelayTime(Optional.ofNullable(delayTime)); return session.getAsyncRemote().sendText(JsonSerializer.getObjectMapper().writeValueAsString(add)); }
@Override public Future<Void> sendAsText(byte[] data) { synchronized (this) { return websocketSession.getAsyncRemote().sendText(new String(data, Charsets.UTF_8)); } }
@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); } }
@Test public void testSanity() throws Exception { replayOnSubscribe(); EasyMock.expect(session.getAsyncRemote()).andReturn(async).anyTimes(); final Capture<String> json = EasyMock.newCapture(CaptureType.ALL); async.sendText(EasyMock.capture(json), EasyMock.anyObject()); EasyMock.expectLastCall().times(3); subscription.request(1); EasyMock.expectLastCall().once(); session.close(EasyMock.anyObject()); EasyMock.expectLastCall().once(); subscription.cancel(); EasyMock.replay(subscription, session, async); subscriber.onNext(ImmutableList.of(ImmutableMap.of("a", 1), ImmutableMap.of("b", 2), ImmutableMap.of("c", 3))); assertEquals(ImmutableList.of("{\"a\":1}","{\"b\":2}","{\"c\":3}"), json.getValues()); subscriber.onComplete(); subscriber.close(); EasyMock.verify(subscription, session, async); }
@Test public void testStopSendingAfterClose() { replayOnSubscribe(); EasyMock.expect(session.getAsyncRemote()).andReturn(async).anyTimes(); final Capture<String> json = EasyMock.newCapture(CaptureType.ALL); async.sendText(EasyMock.capture(json), EasyMock.anyObject()); subscription.request(1); subscription.cancel(); EasyMock.replay(subscription, session, async); subscriber.onNext(ImmutableList.of(ImmutableMap.of("a", 1))); subscriber.close(); subscriber.onNext(ImmutableList.of(ImmutableMap.of("b", 2), ImmutableMap.of("c", 3))); assertEquals(ImmutableList.of("{\"a\":1}"), json.getValues()); EasyMock.verify(subscription, session, async); }
@Override protected boolean sendMessage(WebSocketMessage message) throws IOException { ByteBuffer buffer = message.getPayload().asByteBuffer(); if (WebSocketMessage.Type.TEXT.equals(message.getType())) { getSendProcessor().setReadyToSend(false); String text = new String(buffer.array(), StandardCharsets.UTF_8); getDelegate().getAsyncRemote().sendText(text, new SendProcessorCallback()); } else if (WebSocketMessage.Type.BINARY.equals(message.getType())) { getSendProcessor().setReadyToSend(false); getDelegate().getAsyncRemote().sendBinary(buffer, new SendProcessorCallback()); } else if (WebSocketMessage.Type.PING.equals(message.getType())) { getDelegate().getAsyncRemote().sendPing(buffer); } else if (WebSocketMessage.Type.PONG.equals(message.getType())) { getDelegate().getAsyncRemote().sendPong(buffer); } else { throw new IllegalArgumentException("Unexpected message type: " + message.getType()); } return true; }