Refine search
@Override public void ping() throws IOException { session.getRemote().sendString(WebsocketMessages.PING); }
@Override public void complete(boolean success) { session.getRemote().sendStringByFuture("{ \"command\": \"complete\", \"success\": " + success + " }"); } }
@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().getRemote().sendString(text, new SendProcessorCallback()); } else if (WebSocketMessage.Type.BINARY.equals(message.getType())) { getSendProcessor().setReadyToSend(false); getDelegate().getRemote().sendBytes(buffer, new SendProcessorCallback()); } else if (WebSocketMessage.Type.PING.equals(message.getType())) { getDelegate().getRemote().sendPing(buffer); } else if (WebSocketMessage.Type.PONG.equals(message.getType())) { getDelegate().getRemote().sendPong(buffer); } else { throw new IllegalArgumentException("Unexpected message type: " + message.getType()); } return true; }
@Override public void send(final Message msg) { LOGGER.debug("{} send message: {}", sessionName(), msg); session.getRemote().sendBytesByFuture(ByteBuffer.wrap(MessageEncoding.encodeMessage(msg))); }
public void sendMsg(String context, int sizeOfMessage) throws IOException, JsonParseException, InterruptedException, ExecutionException { byte[] payload = new byte[sizeOfMessage]; String message = getEncoder().encodeToString(payload); String timeStamp = "{\"payload\": \"" + message + "\",\"context\": \"" + context + "\"}"; String sampleMsg = new Gson().fromJson(timeStamp, JsonObject.class).toString(); if (this.session != null && this.session.isOpen() && this.session.getRemote() != null) { startTimeMap.put(context, System.nanoTime()); this.session.getRemote().sendStringByFuture(sampleMsg).get(); } else { log.error("Session is already closed"); } }
public void send(BaseMessage msg) { if (session == null || !session.isOpen()) { return; } try { ObjectMapper mapper = new ObjectMapper(); session.getRemote().sendString(mapper.writeValueAsString(msg)); } catch (IOException e) { e.printStackTrace(); } } }
@OnWebSocketConnect public void onConnect(Session session) throws Exception { this.session = session; socketHealthService.register(this); LOGGER.debug("{} connected", sessionName()); session.getRemote().sendString(consoleLogCharsetJSONMessage); long start = parseStartLine(session.getUpgradeRequest()); LOGGER.debug("{} sending logs for {} starting at line {}.", sessionName(), jobIdentifier, start); try { handler.process(this, jobIdentifier, start); } catch (IOException e) { if ("Connection output is closed".equals(e.getMessage())) { LOGGER.debug("{} client (likely, browser) closed connection prematurely.", sessionName()); close(); // for good measure } else { throw e; } } }
@OnWebSocketConnect public void onConnect(Session session) throws IOException { System.out.printf("Got connect: %s%n", session); LOG.info("Got connect: %s%n", session); // Trivial MQTT Connect message // assertEquals(12, out.readByte()); //remaining length // verifyString("MQIsdp", out); // assertEquals(0x03, out.readByte()); //protocol version // assertEquals(0x32, out.readByte()); //flags // assertEquals(2, out.readByte()); //keepAliveTimer msb // assertEquals(0, out.readByte()); //keepAliveTimer lsb ByteBuffer msg = ByteBuffer.wrap(new byte[]{0x10, // message type 0x0C, // remaining lenght 12 bytes 'M', 'Q', 'I', 's', 'd', 'p', 0x03, // protocol version 0x32, // flags 0x02, 0x00// keepAlive Timer, 2 seconds }); // try { session.getRemote().sendBytes(msg); this.connectSentLatch.countDown(); session.close(StatusCode.NORMAL, "I'm done"); // } catch (IOException t) { // LOG.error(null, t); // } }
/** * Send binary frame to each WebSocket session. * @param bytes byte array to send. */ public void broadcast(final byte[] bytes) { for (final Session session : sessions) { try { session.getRemote().sendBytes(ByteBuffer.wrap(bytes)); } catch (final IOException e) { LOG.warn("Failed to send binary to remote session {}.", session.getRemoteAddress().toString()); } } }
Future<Void> sendMessage(DefaultWebsocket websocket, Object message) throws IOException { Future<Void> future = null; // in case there is web socket and socket connection is open - send message if (websocket != null && websocket.getSession().isOpen()) { log.trace("Sending to websocket {} -> {}", websocket.getConnectionKey(), message); if (message instanceof String) { future = websocket.getSession().getRemote().sendStringByFuture((String) message); } else if (message instanceof byte[]) { ByteBuffer buf = ByteBuffer.wrap((byte[]) message); future = websocket.getSession().getRemote().sendBytesByFuture(buf); } } return future; }
@OnWebSocketConnect public void onConnect(Session session) { System.out.printf("Got connect: %s%n", session); this.session = session; try { Future<Void> fut; fut = session.getRemote().sendStringByFuture("Hello"); fut.get(2, TimeUnit.SECONDS); fut = session.getRemote().sendStringByFuture("Thanks for the conversation."); fut.get(2, TimeUnit.SECONDS); session.close(StatusCode.NORMAL, "I'm done"); } catch (Throwable t) { t.printStackTrace(); } }
@Override public void onWebSocketText(String message) { if (isNotConnected()) { return; } if (message.equals("CLOSE")) { getSession().close(); return; } try { LOGGER.debug("Received text frame of size: {}", message); getRemote().sendString(message); } catch (IOException e) { e.printStackTrace(); } } }
@Override protected void sendTextMessage(TextMessage message) throws IOException { getRemoteEndpoint().sendString(message.getPayload()); }
@Override public void run() { if (!isWebSocketOpen()) { TaskEngine.getInstance().cancelScheduledTask(pingTask); } else { long idleTime = System.currentTimeMillis() - JiveConstants.MINUTE; if (xmppSession.getLastActiveDate().getTime() >= idleTime) { return; } try { // see https://tools.ietf.org/html/rfc6455#section-5.5.2 wsSession.getRemote().sendPing(null); lastPingFailed = false; } catch (IOException ioe) { Log.error("Failed to ping remote peer: " + wsSession, ioe); if (lastPingFailed) { closeSession(); TaskEngine.getInstance().cancelScheduledTask(pingTask); } else { lastPingFailed = true; } } } } }
private void doSend() throws IOException { queue.drainTo(send); if (send.size() > 0) { encodeAndSend(send); synchronized (toDiskLock) { if (!diskBuffer.isEmpty()) { while (diskBuffer.size() > 0) { File next = diskBuffer.remove(0); byte[] nextMsgs = Files.readAllBytes(next.toPath()); if (nextMsgs != null && nextMsgs.length > 0) { wsSession.getRemote().sendBytes(ByteBuffer.wrap(nextMsgs)); } if (!next.delete()) { logger.info("Failed to delete temporary file: " + next.getAbsolutePath()); } } } diskBuffer.clear(); toDiskBuffer.drainTo(send); encodeAndSend(send); } } else { wsSession.getRemote().sendPing(ByteBuffer.allocate(4).putInt(1234)); } } private void encodeAndSend(List<AbstractMessage> send) throws IOException {
/** * Sends a message to the specified clients. * @param message message * @param websockets clients * @throws QueryException query exception */ private static void send(final Value message, final List<WebSocket> websockets) throws QueryException { // serialize contents once final List<Object> values; try { values = WsResponse.serialize(message.iter(), new SerializerOptions()); } catch(final QueryIOException ex) { throw ex.getCause(); } // send result to all clients for(final WebSocket ws : websockets) { if(!ws.isConnected()) continue; final RemoteEndpoint remote = ws.getSession().getRemote(); for(final Object value : values) { if(value instanceof ByteBuffer) { remote.sendBytesByFuture((ByteBuffer) value); } else { remote.sendStringByFuture((String) value); } } } }
@Override protected void sendBinaryMessage(BinaryMessage message) throws IOException { getRemoteEndpoint().sendBytes(message.getPayload()); }