11package com .foxdebug .websocket ;
22
3+ import android .util .Base64 ;
34import android .util .Log ;
45
56import androidx .annotation .NonNull ;
1718import okio .ByteString ;
1819
1920public class WebSocketInstance extends WebSocketListener {
21+ private static final String TAG = "WebSocketInstance" ;
2022 private static final int DEFAULT_CLOSE_CODE = 1000 ;
2123 private static final String DEFAULT_CLOSE_REASON = "Normal closure" ;
2224
@@ -73,21 +75,34 @@ public void setCallback(CallbackContext callbackContext) {
7375 callbackContext .sendPluginResult (result );
7476 }
7577
76- public void send (String message ) {
78+ public void send (String message , boolean isBinary ) {
7779 if (this .webSocket != null ) {
80+ Log .d (TAG , "websocket instanceId=" + this .instanceId + " received send(..., isBinary=" + isBinary + ") action call, sending message=" + message );
81+ if (isBinary ) {
82+ this .sendBinary (message );
83+ return ;
84+ }
7885 this .webSocket .send (message );
79- Log .d ("WebSocketInstance" , "websocket instanceId=" + this .instanceId + " received send() action call, sending message=" + message );
8086 } else {
81- Log .d ("WebSocketInstance" , "websocket instanceId=" + this .instanceId + " received send() action call, ignoring... as webSocket is null (not present)" );
87+ Log .d (TAG , "websocket instanceId=" + this .instanceId + " received send(..., isBinary=" + isBinary + ") ignoring... as webSocket is null (not present/connected )" );
8288 }
8389 }
8490
91+ /**
92+ * Sends bytes as the data of a binary (type 0x2) message.
93+ * @param base64Data Binary Data received from JS bridge encoded as base64 String
94+ */
95+ private void sendBinary (String base64Data ) {
96+ byte [] data = Base64 .decode (base64Data , Base64 .DEFAULT );
97+ this .webSocket .send (ByteString .of (data ));
98+ }
99+
85100 public String close (int code , String reason ) {
86101 if (this .webSocket != null ) {
87102 this .readyState = 2 ; // CLOSING
88103 try {
89104 boolean result = this .webSocket .close (code , reason );
90- Log .d ("WebSocketInstance" , "websocket instanceId=" + this .instanceId + " received close() action call" );
105+ Log .d (TAG , "websocket instanceId=" + this .instanceId + " received close() action call, code=" + code + " reason=" + reason + " close method result: " + result );
91106
92107 // if a graceful shutdown was already underway...
93108 // or if the web socket is already closed or canceled. do nothing.
@@ -100,14 +115,14 @@ public String close(int code, String reason) {
100115
101116 return null ;
102117 } else {
103- Log .d ("WebSocketInstance" , "websocket instanceId=" + this .instanceId + " received close() action call, ignoring... as webSocket is null (not present)" );
118+ Log .d (TAG , "websocket instanceId=" + this .instanceId + " received close() action call, ignoring... as webSocket is null (not present)" );
104119 // TODO: finding a better way of telling it wasn't successful.
105120 return "" ;
106121 }
107122 }
108123
109124 public String close () {
110- Log .d ("WebSocketInstance" , "WebSocket instanceId=" + this .instanceId + " close() called with no arguments. Using defaults." );
125+ Log .d (TAG , "WebSocket instanceId=" + this .instanceId + " close() called with no arguments. Using defaults." );
111126 // Calls the more specific version with default values
112127 return close (DEFAULT_CLOSE_CODE , DEFAULT_CLOSE_REASON );
113128 }
@@ -118,76 +133,81 @@ public void onOpen(@NonNull WebSocket webSocket, Response response) {
118133 this .readyState = 1 ; // OPEN
119134 this .extensions = response .headers ("Sec-WebSocket-Extensions" ).toString ();
120135 this .protocol = response .header ("Sec-WebSocket-Protocol" );
121- Log .i ("WebSocketInstance" , "websocket instanceId=" + this .instanceId + " Opened" + "received extensions=" + this .extensions );
122- sendEvent ("open" , null , false );
136+ Log .i (TAG , "websocket instanceId=" + this .instanceId + " Opened" + "received extensions=" + this .extensions );
137+ sendEvent ("open" , null , false , false );
123138 }
124139
125140 @ Override
126141 public void onMessage (@ NonNull WebSocket webSocket , @ NonNull String text ) {
127- Log .d ("WebSocketInstance" , "websocket instanceId=" + this .instanceId + " Received message: " + text );
128- sendEvent ("message" , text , false );
142+ Log .d (TAG , "websocket instanceId=" + this .instanceId + " Received message: " + text );
143+ sendEvent ("message" , text , false , false );
129144 }
130145
131146 // This is called when the Websocket server sends a binary(type 0x2) message.
132147 @ Override
133148 public void onMessage (@ NonNull WebSocket webSocket , @ NonNull ByteString bytes ) {
134- Log .d ("WebSocketInstance" , "websocket instanceId=" + this .instanceId + " Received message(bytes): " + bytes .toString ());
149+ Log .d (TAG , "websocket instanceId=" + this .instanceId + " Received message(bytes/binary payload ): " + bytes .toString ());
135150
136151 try {
137152 if ("arraybuffer" .equals (this .binaryType )) {
138153 String base64 = bytes .base64 ();
139- sendEvent ("message" , base64 , true );
154+ sendEvent ("message" , base64 , true , false );
140155 } else {
141- sendEvent ("message" , bytes .utf8 (), true );
156+ sendEvent ("message" , bytes .utf8 (), true , true );
142157 }
143158 } catch (Exception e ) {
144- Log .e ("WebSocketInstance" , "Error sending message" , e );
159+ Log .e (TAG , "Error sending message" , e );
145160 }
146161
147162 }
148163
149164 @ Override
150165 public void onClosing (@ NonNull WebSocket webSocket , int code , @ NonNull String reason ) {
151166 this .readyState = 2 ; // CLOSING
152- Log .i ("WebSocketInstance" , "websocket instanceId=" + this .instanceId + " is Closing code: " + code + " reason: " + reason );
167+ Log .i (TAG , "websocket instanceId=" + this .instanceId + " is Closing code: " + code + " reason: " + reason );
153168 }
154169
155170 @ Override
156171 public void onClosed (@ NonNull WebSocket webSocket , int code , @ NonNull String reason ) {
157172 this .readyState = 3 ; // CLOSED
158- Log .i ("WebSocketInstance" , "websocket instanceId=" + this .instanceId + " Closed code: " + code + " reason: " + reason );
173+ Log .i (TAG , "websocket instanceId=" + this .instanceId + " Closed code: " + code + " reason: " + reason );
159174 JSONObject closedEvent = new JSONObject ();
160175 try {
161176 closedEvent .put ("code" , code );
162177 closedEvent .put ("reason" , reason );
163178 } catch (JSONException e ) {
164- Log .e ("WebSocketInstance" , "Error creating close event" , e );
179+ Log .e (TAG , "Error creating close event" , e );
165180 }
166- sendEvent ("close" , closedEvent .toString (), false );
181+ sendEvent ("close" , closedEvent .toString (), false , false );
167182 }
168183
169184 @ Override
170185 public void onFailure (@ NonNull WebSocket webSocket , Throwable t , Response response ) {
171186 this .readyState = 3 ; // CLOSED
172- sendEvent ("error" , t .getMessage (), false );
173- Log .e ("WebSocketInstance" , "websocket instanceId=" + this .instanceId + " Error: " + t .getMessage ());
187+ sendEvent ("error" , t .getMessage (), false , false );
188+ Log .e (TAG , "websocket instanceId=" + this .instanceId + " Error: " + t .getMessage ());
189+ }
190+
191+ public void setBinaryType (String binaryType ) {
192+ this .binaryType = binaryType ;
174193 }
175194
176- private void sendEvent (String type , String data , boolean isBinary ) {
195+ private void sendEvent (String type , String data , boolean isBinary , boolean parseAsText ) {
177196 if (callbackContext != null ) {
178197 try {
179198 JSONObject event = new JSONObject ();
180199 event .put ("type" , type );
181200 event .put ("extensions" , this .extensions );
182201 event .put ("readyState" , this .readyState );
183- event .put ("isBinary" , isBinary ? true : false );
202+ event .put ("isBinary" , isBinary );
203+ event .put ("parseAsText" , parseAsText );
184204 if (data != null ) event .put ("data" , data );
185- Log .d ("WebSocketInstance" , "sending event: " + type + " eventObj " + event .toString ());
205+ Log .d (TAG , "sending event: " + type + " eventObj " + event .toString ());
186206 PluginResult result = new PluginResult (PluginResult .Status .OK , event );
187207 result .setKeepCallback (true );
188208 callbackContext .sendPluginResult (result );
189209 } catch (Exception e ) {
190- Log .e ("WebSocketInstance" , "Error sending event" , e );
210+ Log .e (TAG , "Error sending event" , e );
191211 }
192212 }
193213 }
0 commit comments