public void sendMessage ( String message , final Respoke . TaskCompletionListener completionListener ) { if ( isActive ( ) ) { JSONObject jsonMessage = new JSONObject ( ) ; try { jsonMessage . put ( "message" , message ) ; byte [ ] rawMessage = jsonMessage . toString ( ) . getBytes ( Charset . forName ( "UTF-8" ) ) ; ByteBuffer directData = ByteBuffer . allocateDirect ( rawMessage . length ) ; directData . put ( rawMessage ) ; directData . flip ( ) ; DataChannel . Buffer data = new DataChannel . Buffer ( directData , false ) ; if ( dataChannel . send ( data ) ) { Respoke . postTaskSuccess ( completionListener ) ; } else { Respoke . postTaskError ( completionListener , "Error sending message" ) ; } } catch ( JSONException e ) { Respoke . postTaskError ( completionListener , "Unable to encode message to JSON" ) ; } } else { Respoke . postTaskError ( completionListener , "DataChannel not in an open state" ) ; } }
Send a message to the remote client through the direct connection .