public void sendAsync ( BasicMessageWithExtraData < ? extends BasicMessage > messageWithData ) { if ( ! isConnected ( ) ) { throw new IllegalStateException ( "WebSocket connection was closed. Cannot send any messages" ) ; } BasicMessage message = messageWithData . getBasicMessage ( ) ; configurationAuthentication ( message ) ; sendExecutor . execute ( new Runnable ( ) { @ Override public void run ( ) { try { if ( messageWithData . getBinaryData ( ) == null ) { String messageString = ApiDeserializer . toHawkularFormat ( message ) ; @ SuppressWarnings ( "resource" ) Buffer buffer = new Buffer ( ) . writeUtf8 ( messageString ) ; RequestBody requestBody = RequestBody . create ( WebSocket . TEXT , buffer . readByteArray ( ) ) ; FeedCommProcessor . this . webSocket . sendMessage ( requestBody ) ; } else { BinaryData messageData = ApiDeserializer . toHawkularFormat ( message , messageWithData . getBinaryData ( ) ) ; RequestBody requestBody = new RequestBody ( ) { @ Override public MediaType contentType ( ) { return WebSocket . BINARY ; } @ Override public void writeTo ( BufferedSink bufferedSink ) throws IOException { emitToSink ( messageData , bufferedSink ) ; } } ; FeedCommProcessor . this . webSocket . sendMessage ( requestBody ) ; } } catch ( Throwable t ) { log . errorFailedToSendOverFeedComm ( message . getClass ( ) . getName ( ) , t ) ; } } } ) ; }
Sends a message to the server asynchronously . This method returns immediately ; the message may not go out until some time in the future .