Refine search
@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 protocol-level Ping message with the current time as payload. Does * nothing if not currently connected. */ private synchronized void sendPing() { if (s != null && s.isOpen()) { try { ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES); buffer.putLong(0, System.currentTimeMillis()); s.getAsyncRemote().sendPing(buffer); System.out.println("Sending"+buffer); } catch (Exception ex) { LOGGER.warning("[FFZ-WS] Failed to send ping: "+ex); } } }
@Override public boolean send(String payload,boolean async) { if (!readyState.equals(OPEN)) { return false; } try { if(!async) { ws.getBasicRemote().sendText(payload); } else{ ws.getAsyncRemote().sendText(payload); } } catch (IOException ex) { log.log(Level.WARNING, "Error sending raw websocket data", ex); } return true; }
@OnOpen public void onOpen(@PathParam("fileSystemName") String fileSystemName, Session session) { LOGGER.debug("WebSocket session '{}' opened for file system '{}'", session.getId(), fileSystemName); ListenableAppStorage storage = appDataBean.getStorage(fileSystemName); AppStorageListener listener = eventList -> { if (session.isOpen()) { RemoteEndpoint.Async remote = session.getAsyncRemote(); remote.setSendTimeout(1000); remote.sendObject(eventList, result -> { if (!result.isOK()) { LOGGER.error(result.getException().toString(), result.getException()); } }); } else { webSocketContext.removeSession(session); } }; storage.addListener(listener); session.getUserProperties().put("listener", listener); // to prevent weak listener from being garbage collected webSocketContext.addSession(session); }
@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); }
private void sendBinary(String topic, byte[] message) { AbstractMessage msg = transform.transform(new BinaryMessage(topic, message), session); if(!msg.isDiscard()) { remoteEndpoint.sendObject(msg); } }
private void sendText(String topic, byte[] message) { String messageString = new String(message, Charset.forName("UTF-8")); LOG.trace("XXX Sending text message to remote endpoint: {} {}", topic, messageString); AbstractMessage msg = transform.transform(new TextMessage(topic, messageString), session); if(!msg.isDiscard()) { remoteEndpoint.sendObject(msg); } }
@Override public void sendBinary(byte[] attachment) { getEndpoint().sendBinary(ByteBuffer.wrap(attachment)); incPendingMessages(); }
@Override public void send(final URI receiverUri, final byte[] message, final String tag) throws IOException { if (remotes.containsKey(receiverUri)) { final Async remote = remotes.get(receiverUri); remote.sendBinary(ByteBuffer.wrap(message)); remote.flushBatch(); } else { throw new IOException("Remote: " + receiverUri.toASCIIString() + " is currently not connected."); } }
private void sendObject(Object data) { getEndpoint().sendObject(data); incPendingMessages(); }
@Override public void send(final URI receiverUri, final String message, final String tag) throws IOException { if (remotes.containsKey(receiverUri)) { final Async remote = remotes.get(receiverUri); remote.sendText(message); remote.flushBatch(); } else { throw new IOException("Remote: " + receiverUri.toASCIIString() + " is currently not connected. (" + getAddress() + " / " + remotes.keySet() + ")"); } }
@Override public void run() { char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-|{}[]():".toCharArray(); int lettersLen = letters.length; char randomText[] = new char[size]; Random rand = new Random(42); String msg; for (int n = 0; n < count; n++) { // create random text for (int i = 0; i < size; i++) { randomText[i] = letters[rand.nextInt(lettersLen)]; } msg = String.format("ManyThreads [%s]", String.valueOf(randomText)); remote.sendText(msg); } } }
@Override public void send(final URI receiverUri, final byte[] message, final String tag) throws IOException { if (!receiverUri.equals(serverUrl)) { throw new IOException( "Currently it's only possible to send to the server agent directly, not other agents:" + receiverUri.toASCIIString()); } if (remote == null || !isConnected()) { connect(); } if (remote != null) { try { remote.sendBinary(ByteBuffer.wrap(message)); remote.flushBatch(); } catch (RuntimeException rte) { if (rte.getMessage().equals("Socket is not connected.")) { remote = null; // retry! send(receiverUri, message, tag); } } } else { throw new IOException("Not connected?"); } }
@Override public void send(final URI receiverUri, final String message, final String tag) throws IOException { if (!receiverUri.equals(serverUrl)) { throw new IOException( "Currently it's only possible to send to the server agent directly, not other agents:" + receiverUri.toASCIIString() + " serverUrl:" + serverUrl.toASCIIString()); } if (remote == null || !isConnected()) { connect(); } if (remote != null) { try { remote.sendText(message); remote.flushBatch(); } catch (RuntimeException rte) { if (rte.getMessage().equals("Socket is not connected.")) { remote = null; // retry! send(receiverUri, message, tag); } } } else { throw new IOException("Not connected?"); } }
sendFuture = wsSession.getAsyncRemote().sendBinary(ByteBuffer.wrap(buf));
try { if (useAsync) { sendFuture = wsSession.getAsyncRemote().sendText(output); sendFuture.get(20000L, TimeUnit.MILLISECONDS); } else {
@Override public void run() { if (session.isOpen()) { session.getAsyncRemote() .sendText("keep alive"); } else { keepAliveTimer.cancel(); } } }
default: // to get rid of a compiler warning case BASIC: endpoint = session.getBasicRemote(); break; case ASYNC: endpoint = session.getAsyncRemote(); break; ((RemoteEndpoint.Basic) endpoint).sendText(message.getPayload().toString()); } else { ((RemoteEndpoint.Async) endpoint).sendText(message.getPayload().toString()); ((RemoteEndpoint.Basic) endpoint).sendBinary(ByteBuffer.wrap(message.getPayload().toString().getBytes(Utils.getDefaultEncoding()))); } else { ((RemoteEndpoint.Async) endpoint).sendBinary(ByteBuffer.wrap(message.getPayload().toString().getBytes(Utils.getDefaultEncoding())));