@SuppressWarnings("unchecked") private void sendMessageBinary(ByteBuffer msg, boolean last) throws WsIOException { if (binaryMsgHandler instanceof WrappedMessageHandler) { long maxMessageSize = ((WrappedMessageHandler) binaryMsgHandler).getMaxMessageSize(); if (maxMessageSize > -1 && msg.remaining() > maxMessageSize) { throw new WsIOException(new CloseReason(CloseCodes.TOO_BIG, sm.getString("wsFrame.messageTooBig", Long.valueOf(msg.remaining()), Long.valueOf(maxMessageSize)))); } } try { if (binaryMsgHandler instanceof MessageHandler.Partial<?>) { ((MessageHandler.Partial<ByteBuffer>) binaryMsgHandler).onMessage(msg, last); } else { // Caller ensures last == true if this branch is used ((MessageHandler.Whole<ByteBuffer>) binaryMsgHandler).onMessage(msg); } } catch(Throwable t) { handleThrowableOnSend(t); } }
@Override public final void onClose(Session session, CloseReason closeReason) { if (methodMapping.getOnClose() != null) { try { methodMapping.getOnClose().invoke(pojo, methodMapping.getOnCloseArgs(pathParameters, session, closeReason)); } catch (Throwable t) { ExceptionUtils.handleThrowable(t); log.error(sm.getString("pojoEndpointBase.onCloseFail", pojo.getClass().getName()), t); } } // Trigger the destroy method for any associated decoders Set<MessageHandler> messageHandlers = session.getMessageHandlers(); for (MessageHandler messageHandler : messageHandlers) { if (messageHandler instanceof PojoMessageHandlerWholeBase<?>) { ((PojoMessageHandlerWholeBase<?>) messageHandler).onClose(); } } }
public WebsocketClient(String uri, final String sessionID, MessageHandler.Whole<String> messageHandler) throws IOException { // add session ID so the request gets through LAMS security Builder configBuilder = ClientEndpointConfig.Builder.create(); configBuilder.configurator(new Configurator() { @Override public void beforeRequest(Map<String, List<String>> headers) { headers.put("Cookie", Arrays.asList("JSESSIONID=" + sessionID)); } }); ClientEndpointConfig clientConfig = configBuilder.build(); this.websocketEndpoint = new WebsocketEndpoint(messageHandler); WebSocketContainer container = ContainerProvider.getWebSocketContainer(); try { container.connectToServer(websocketEndpoint, clientConfig, new URI(uri)); } catch (DeploymentException | URISyntaxException e) { throw new IOException("Error while connecting to websocket server", e); } }
@SuppressWarnings("unchecked") private void sendMessageText(boolean last) throws WsIOException { if (textMsgHandler instanceof WrappedMessageHandler) { long maxMessageSize = ((WrappedMessageHandler) textMsgHandler).getMaxMessageSize(); if (maxMessageSize > -1 && messageBufferText.remaining() > maxMessageSize) { throw new WsIOException(new CloseReason(CloseCodes.TOO_BIG, sm.getString("wsFrame.messageTooBig", Long.valueOf(messageBufferText.remaining()), Long.valueOf(maxMessageSize)))); } } try { if (textMsgHandler instanceof MessageHandler.Partial<?>) { ((MessageHandler.Partial<String>) textMsgHandler).onMessage(messageBufferText.toString(), last); } else { // Caller ensures last == true if this branch is used ((MessageHandler.Whole<String>) textMsgHandler).onMessage(messageBufferText.toString()); } } catch (Throwable t) { handleThrowableOnSend(t); } finally { messageBufferText.clear(); } }
@SuppressWarnings("unchecked") private void sendMessageBinary(ByteBuffer msg, boolean last) throws WsIOException { if (binaryMsgHandler instanceof WrappedMessageHandler) { long maxMessageSize = ((WrappedMessageHandler) binaryMsgHandler).getMaxMessageSize(); if (maxMessageSize > -1 && msg.remaining() > maxMessageSize) { throw new WsIOException(new CloseReason(CloseCodes.TOO_BIG, sm.getString("wsFrame.messageTooBig", Long.valueOf(msg.remaining()), Long.valueOf(maxMessageSize)))); } } try { if (binaryMsgHandler instanceof MessageHandler.Partial<?>) { ((MessageHandler.Partial<ByteBuffer>) binaryMsgHandler).onMessage(msg, last); } else { // Caller ensures last == true if this branch is used ((MessageHandler.Whole<ByteBuffer>) binaryMsgHandler).onMessage(msg); } } catch (Throwable t) { handleThrowableOnSend(t); } }
public void onOpen(final Session session, EndpointConfig endpointConfig) { session.getAsyncRemote().sendText( "Client Success!Your id is: " + session.getId()); session.addMessageHandler(new MessageHandler.Whole<String>() { @Override public void onMessage(String message) { session.getAsyncRemote().sendObject(message, new SendHandler() { @Override public void onResult(SendResult result) { System.out.println(session.getId() + ":" + result.isOK()); } }); } }); }
@Override public void onOpen(Session sn, EndpointConfig ec) { System.out.println("In DataMessageEndpoint#onOpen"); this.session = sn; //TODO: Investgate why lambda version of the following didn't work: this.session.addMessageHandler(new MessageHandler.Whole<String>() { public void onMessage(String jsonReading) { try { ObjectMapper mapper = new ObjectMapper(); Reading reading = mapper.readValue(jsonReading, Reading.class); System.out.println("New reading: " + reading.toString()); Platform.runLater(() -> readingConsumer.setReading(reading)); } catch (Exception e) { System.out.println("Exception: " + e.getLocalizedMessage()); } } }); }
private void debugDumpSessionInfo() { if (srrp.getSessions().size() == 0) { Log.log(Level.FINE,this, " No sessions known."); } for (Session s : srrp.getSessions()) { Log.log(Level.FINE,this, " Session: " + s.getId()); Log.log(Level.FINE,this, " handlers: " + s.getMessageHandlers().size()); int mhc = 0; for (MessageHandler m : s.getMessageHandlers()) { if (m instanceof SessionMessageHandler) { SessionMessageHandler smh = (SessionMessageHandler) m; Log.log(Level.FINE,this, " [" + mhc + "] SessionMessageHandler for session " + smh.session.getId() + " linked to room " + smh.owner.room.getRoomId()); } else { Log.log(Level.FINE,this, " [" + mhc + "] unknown handler"); } mhc++; } } }
@Override public void onOpen(Session session, EndpointConfig config) { log.debug("Session {} open on channel", session.getId()); session.addMessageHandler(new MessageHandler.Whole<byte[]>() { @Override public void onMessage(byte[] message) { try { WebSocketEndpoint.this.onMessage(message, session); } catch (Exception e) { log.error("Unexpected exception", e); } } }); }
private void assertMessageReceived( String endpoint, String expectedMessage, String messageToSend ) throws Exception { final SettableFuture<String> futureMessage = SettableFuture.create(); client.connectToServer( new Endpoint() { @Override public void onOpen( Session session, EndpointConfig config ) { clientSession = session; try { session.addMessageHandler( new MessageHandler.Whole<String>() { @Override public void onMessage( String message ) { System.out.println( "Received message: " + message ); futureMessage.set( message ); } } ); session.getBasicRemote().sendText( messageToSend ); } catch ( IOException e ) { e.printStackTrace(); } } }, cec, new URI( "ws://localhost:8025/" + endpoint ) ); assertEquals( expectedMessage, futureMessage.get( 2, TimeUnit.SECONDS ) ); }
@Override public void onOpen(final Session session, EndpointConfig ec) { session.addMessageHandler(new MessageHandler.Whole<String>() { @Override public void onMessage(String text) { System.out.println("Received response in client from endpoint: " + text); } }); System.out.println("Connected to endpoint: " + session.getBasicRemote()); try { String name = "Duke"; System.out.println("Sending message from client -> endpoint: " + name); session.getBasicRemote().sendText(name); } catch (IOException ex) { Logger.getLogger(MyClient.class.getName()).log(Level.SEVERE, null, ex); } }
@Override public void onOpen(final Session session, EndpointConfig ec) { session.addMessageHandler(new MessageHandler.Whole<String>() { @Override public void onMessage(String data) { System.out.println("Received (MyEndpointHandler) : " + data); session.getAsyncRemote().sendText(data, new SendHandler() { @Override public void onResult(SendResult sr) { if (sr.isOK()) { System.out.println("Message written to the socket (handler)"); } else { System.out.println("Message NOT written to the socket (handler)"); sr.getException().printStackTrace(); } } }); } }); }
@Override public void onOpen(Session session, EndpointConfig config) { final String sessionId = session.getId(); log.info("established session with id: "+sessionId); //add text based message handler session.addMessageHandler(new MessageHandler.Whole<String>() { @Override public void onMessage(String msg) { log.info(sessionId+": text message: "+msg); } }); //add binary based message handler session.addMessageHandler(new MessageHandler.Whole<ByteBuffer>() { @Override public void onMessage(ByteBuffer buffer) { log.info(sessionId+": binary message: "+new String(buffer.array())); } }); }
/** * 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 ); } } } ); }
@Override public void onOpen(Session session, EndpointConfig arg1) { final RemoteEndpoint.Basic remote = session.getBasicRemote(); session.addMessageHandler(new MessageHandler.Whole<String>() { public void onMessage(String text) { try { remote.sendText(text.toUpperCase()); } catch (IOException ioe) { ioe.printStackTrace(); } } }); }
@Override public Set<MessageHandler> getMessageHandlers() { checkState(); Set<MessageHandler> result = new HashSet<MessageHandler>(); if (binaryMessageHandler != null) { result.add(binaryMessageHandler); } if (textMessageHandler != null) { result.add(textMessageHandler); } if (pongMessageHandler != null) { result.add(pongMessageHandler); } return result; }
@SuppressWarnings("unchecked") private void sendMessageText(boolean last) throws WsIOException { if (textMsgHandler instanceof WrappedMessageHandler) { long maxMessageSize = ((WrappedMessageHandler) textMsgHandler).getMaxMessageSize(); if (maxMessageSize > -1 && messageBufferText.remaining() > maxMessageSize) { throw new WsIOException(new CloseReason(CloseCodes.TOO_BIG, sm.getString("wsFrame.messageTooBig", Long.valueOf(messageBufferText.remaining()), Long.valueOf(maxMessageSize)))); } } try { if (textMsgHandler instanceof MessageHandler.Partial<?>) { ((MessageHandler.Partial<String>) textMsgHandler).onMessage( messageBufferText.toString(), last); } else { // Caller ensures last == true if this branch is used ((MessageHandler.Whole<String>) textMsgHandler).onMessage( messageBufferText.toString()); } } catch (Throwable t) { handleThrowableOnSend(t); } finally { messageBufferText.clear(); } }
private boolean usePartial() { if (Util.isControl(opCode)) { return false; } else if (textMessage) { return textMsgHandler instanceof MessageHandler.Partial<?>; } else { // Must be binary return binaryMsgHandler instanceof MessageHandler.Partial<?>; } }
/** * Expose the POJO if it is a message handler so the Session is able to * match requests to remove handlers if the original handler has been * wrapped. */ @Override public final MessageHandler getWrappedHandler() { if (pojo instanceof MessageHandler) { return (MessageHandler) pojo; } else { return null; } }
public Set<MessageHandler> getMessageHandlers(Object pojo, Map<String,String> pathParameters, Session session, EndpointConfig config) { Set<MessageHandler> result = new HashSet<MessageHandler>(); for (MessageHandlerInfo messageMethod : onMessage) { result.addAll(messageMethod.getMessageHandlers(pojo, pathParameters, session, config)); } return result; }
@Override public void onOpen(Session session, EndpointConfig config) { LOG.info("Websocket session {} opened.", session.getId()); session.addMessageHandler(new MessageHandler.Whole<byte[]>() { @Override public void onMessage(byte[] message) { LOG.info("Message received on Websocket session {}: {}", session.getId(), message); } }); }
@Override public void onOpen(Session session, EndpointConfig config) { System.out.println("Server session established"); //conn to redis jedis = new Jedis("192.168.99.100", 6379, 10000); session.addMessageHandler(new MessageHandler.Whole<MeetupRSVP>() { @Override public void onMessage(MeetupRSVP message) { List<GroupTopic> groupTopics = message.getGroup().getGroupTopics(); for (GroupTopic groupTopic : groupTopics) { try { if(GROUPS_IN_REDIS.contains(groupTopic.getTopicName())){ jedis.zincrby(LEADERBOARD_REDIS_KEY, 1, groupTopic.getTopicName()); }else{ //zscore = jedis.zscore(LEADERBOARD_REDIS_KEY, groupTopic.getTopicName()); jedis.zadd(LEADERBOARD_REDIS_KEY, 1, groupTopic.getTopicName()); GROUPS_IN_REDIS.add(groupTopic.getTopicName()); } // Double zscore = jedis.zscore(LEADERBOARD_REDIS_KEY, groupTopic.getTopicName());; // if(zscore == null){ // jedis.zadd(LEADERBOARD_REDIS_KEY, 1, groupTopic.getTopicName()); // }else{ // jedis.zincrby(LEADERBOARD_REDIS_KEY, 1, groupTopic.getTopicName()); // } } catch (Exception e) { e.printStackTrace(); } } } }); }
@Override public void removeMessageHandler(MessageHandler listener) { checkState(); if (listener == null) { return; } MessageHandler wrapped = null; if (listener instanceof WrappedMessageHandler) { wrapped = ((WrappedMessageHandler) listener).getWrappedHandler(); } if (wrapped == null) { wrapped = listener; } boolean removed = false; if (wrapped.equals(textMessageHandler) || listener.equals(textMessageHandler)) { textMessageHandler = null; removed = true; } if (wrapped.equals(binaryMessageHandler) || listener.equals(binaryMessageHandler)) { binaryMessageHandler = null; removed = true; } if (wrapped.equals(pongMessageHandler) || listener.equals(pongMessageHandler)) { pongMessageHandler = null; removed = true; } if (!removed) { // ISE for now. Could swallow this silently / log this if the ISE // becomes a problem throw new IllegalStateException(sm.getString("wsSession.removeHandlerFailed", listener)); } }