|
13 | 13 | import com.facebook.react.bridge.ReactContext; |
14 | 14 | import com.facebook.react.bridge.ReactContextBaseJavaModule; |
15 | 15 | import com.facebook.react.bridge.ReactMethod; |
| 16 | +import com.facebook.react.bridge.ReadableArray; |
16 | 17 | import com.facebook.react.bridge.ReadableMap; |
| 18 | +import com.facebook.react.bridge.ReadableMapKeySetIterator; |
| 19 | +import com.facebook.react.bridge.ReadableType; |
17 | 20 | import com.facebook.react.bridge.WritableMap; |
18 | 21 | import com.facebook.react.bridge.WritableNativeMap; |
19 | 22 | import com.facebook.react.modules.core.DeviceEventManagerModule; |
20 | 23 |
|
21 | 24 | import org.json.JSONException; |
22 | 25 | import org.json.JSONObject; |
23 | 26 |
|
| 27 | +import java.util.ArrayList; |
24 | 28 | import java.util.HashMap; |
25 | 29 | import java.util.Iterator; |
| 30 | +import java.util.List; |
26 | 31 | import java.util.Map; |
27 | 32 | import java.util.Objects; |
28 | 33 |
|
|
35 | 40 | import ibt.ortc.extensibility.OnDisconnected; |
36 | 41 | import ibt.ortc.extensibility.OnException; |
37 | 42 | import ibt.ortc.extensibility.OnMessage; |
| 43 | +import ibt.ortc.extensibility.OnMessageWithBuffer; |
38 | 44 | import ibt.ortc.extensibility.OnMessageWithFilter; |
| 45 | +import ibt.ortc.extensibility.OnMessageWithOptions; |
| 46 | +import ibt.ortc.extensibility.OnPublishResult; |
39 | 47 | import ibt.ortc.extensibility.OnReconnected; |
40 | 48 | import ibt.ortc.extensibility.OnReconnecting; |
41 | 49 | import ibt.ortc.extensibility.OnSubscribed; |
@@ -217,6 +225,133 @@ public void disconnect(Integer id){ |
217 | 225 | } |
218 | 226 | } |
219 | 227 |
|
| 228 | + private Map<String, Object> recursivelyDeconstructReadableMap(ReadableMap readableMap) { |
| 229 | + ReadableMapKeySetIterator iterator = readableMap.keySetIterator(); |
| 230 | + Map<String, Object> deconstructedMap = new HashMap<>(); |
| 231 | + while (iterator.hasNextKey()) { |
| 232 | + String key = iterator.nextKey(); |
| 233 | + ReadableType type = readableMap.getType(key); |
| 234 | + switch (type) { |
| 235 | + case Null: |
| 236 | + deconstructedMap.put(key, null); |
| 237 | + break; |
| 238 | + case Boolean: |
| 239 | + deconstructedMap.put(key, readableMap.getBoolean(key)); |
| 240 | + break; |
| 241 | + case Number: |
| 242 | + deconstructedMap.put(key, readableMap.getDouble(key)); |
| 243 | + break; |
| 244 | + case String: |
| 245 | + deconstructedMap.put(key, readableMap.getString(key)); |
| 246 | + break; |
| 247 | + case Map: |
| 248 | + deconstructedMap.put(key, recursivelyDeconstructReadableMap(readableMap.getMap(key))); |
| 249 | + break; |
| 250 | + case Array: |
| 251 | + deconstructedMap.put(key, recursivelyDeconstructReadableArray(readableMap.getArray(key))); |
| 252 | + break; |
| 253 | + default: |
| 254 | + throw new IllegalArgumentException("Could not convert object with key: " + key + "."); |
| 255 | + } |
| 256 | + |
| 257 | + } |
| 258 | + return deconstructedMap; |
| 259 | + } |
| 260 | + |
| 261 | + private List<Object> recursivelyDeconstructReadableArray(ReadableArray readableArray) { |
| 262 | + List<Object> deconstructedList = new ArrayList<>(readableArray.size()); |
| 263 | + for (int i = 0; i < readableArray.size(); i++) { |
| 264 | + ReadableType indexType = readableArray.getType(i); |
| 265 | + switch(indexType) { |
| 266 | + case Null: |
| 267 | + deconstructedList.add(i, null); |
| 268 | + break; |
| 269 | + case Boolean: |
| 270 | + deconstructedList.add(i, readableArray.getBoolean(i)); |
| 271 | + break; |
| 272 | + case Number: |
| 273 | + deconstructedList.add(i, readableArray.getDouble(i)); |
| 274 | + break; |
| 275 | + case String: |
| 276 | + deconstructedList.add(i, readableArray.getString(i)); |
| 277 | + break; |
| 278 | + case Map: |
| 279 | + deconstructedList.add(i, recursivelyDeconstructReadableMap(readableArray.getMap(i))); |
| 280 | + break; |
| 281 | + case Array: |
| 282 | + deconstructedList.add(i, recursivelyDeconstructReadableArray(readableArray.getArray(i))); |
| 283 | + break; |
| 284 | + default: |
| 285 | + throw new IllegalArgumentException("Could not convert object at index " + i + "."); |
| 286 | + } |
| 287 | + } |
| 288 | + return deconstructedList; |
| 289 | + } |
| 290 | + |
| 291 | + |
| 292 | + @ReactMethod |
| 293 | + public void subscribeWithOptions(ReadableMap options, Integer id){ |
| 294 | + Map newOptions = recursivelyDeconstructReadableMap(options); |
| 295 | + OrtcClient client = null; |
| 296 | + if (queue.containsKey(id)) { |
| 297 | + client = queue.get(id); |
| 298 | + client.subscribeWithOptions(newOptions, new OnMessageWithOptions() { |
| 299 | + @Override |
| 300 | + public void run(OrtcClient sender, Map msgOptions) { |
| 301 | + String thisId = "" + RealtimeMessagingAndroid.getKeyByValue(queue, sender); |
| 302 | + WritableMap params = new WritableNativeMap(); |
| 303 | + params.putString("channel", (String)msgOptions.get("channel")); |
| 304 | + params.putString("message", (String)msgOptions.get("message")); |
| 305 | + if (msgOptions.containsKey("filtered")) |
| 306 | + params.putBoolean("filtered", (Boolean) msgOptions.get("filtered")); |
| 307 | + if (msgOptions.containsKey("seqId")) |
| 308 | + params.putString("seqId", (String) msgOptions.get("seqId")); |
| 309 | + |
| 310 | + sendEvent(getReactApplicationContext(), thisId + "-onMessageWithOptions", params); |
| 311 | + } |
| 312 | + }); |
| 313 | + } |
| 314 | + } |
| 315 | + |
| 316 | + @ReactMethod |
| 317 | + public void subscribeWithBuffer(String channel, String subscriberId, Integer id){ |
| 318 | + OrtcClient client = null; |
| 319 | + if (queue.containsKey(id)) { |
| 320 | + client = queue.get(id); |
| 321 | + client.subscribeWithBuffer(channel, subscriberId, new OnMessageWithBuffer() { |
| 322 | + @Override |
| 323 | + public void run(OrtcClient sender, String channel, String seqId, String message) { |
| 324 | + String thisId = "" + RealtimeMessagingAndroid.getKeyByValue(queue, sender); |
| 325 | + WritableMap params = new WritableNativeMap(); |
| 326 | + params.putString("channel", channel); |
| 327 | + params.putString("message", message); |
| 328 | + params.putString("seqId", seqId); |
| 329 | + sendEvent(getReactApplicationContext(), thisId + "-onMessageWithBuffer", params); |
| 330 | + } |
| 331 | + }); |
| 332 | + } |
| 333 | + } |
| 334 | + |
| 335 | + @ReactMethod |
| 336 | + public void publish(String channel, String message, Integer ttl, Integer id, final Callback callBack){ |
| 337 | + if (queue.containsKey(id)) { |
| 338 | + final OrtcClient client = queue.get(id); |
| 339 | + client.publish(channel, message, ttl, new OnPublishResult() { |
| 340 | + @Override |
| 341 | + public void run(String error, String seqId) { |
| 342 | + String pError = ""; |
| 343 | + String pSeqId = ""; |
| 344 | + if (error != null) |
| 345 | + pError = error; |
| 346 | + if (seqId != null) |
| 347 | + pSeqId = seqId; |
| 348 | + callBack.invoke(pError, pSeqId); |
| 349 | + } |
| 350 | + }); |
| 351 | + } |
| 352 | + } |
| 353 | + |
| 354 | + |
220 | 355 | @ReactMethod |
221 | 356 | public void subscribeWithFilter(String channel, Boolean subscribeOnReconnect, String filter, Integer id){ |
222 | 357 | OrtcClient client = null; |
@@ -461,6 +596,10 @@ private static JSONObject convertBundleToJson(Bundle extras) |
461 | 596 | } |
462 | 597 | else{ |
463 | 598 | String message = extras.getString("M"); |
| 599 | + if (message.charAt(0) == '#'){ |
| 600 | + String seqId = message.substring(message.indexOf("#") + 1,message.indexOf(":")); |
| 601 | + json.put("seqId", seqId); |
| 602 | + } |
464 | 603 | String newMsg = message.substring(message.indexOf("_", message.indexOf("_") + 1) + 1); |
465 | 604 | json.put("message", newMsg); |
466 | 605 | } |
@@ -502,8 +641,16 @@ public void sendJavascript(JSONObject json) { |
502 | 641 | gCachedExtras = null; |
503 | 642 | } catch (JSONException ex) { |
504 | 643 | WritableMap params = new WritableNativeMap(); |
505 | | - params.putString("message", json.getString("message")); |
506 | | - params.putString("channel", json.getString("channel")); |
| 644 | + |
| 645 | + Iterator<String> iter = json.keys(); |
| 646 | + while (iter.hasNext()) { |
| 647 | + String key = iter.next(); |
| 648 | + try { |
| 649 | + params.putString(key, json.getString(key)); |
| 650 | + } catch (JSONException e) { |
| 651 | + // Something went wrong! |
| 652 | + } |
| 653 | + } |
507 | 654 |
|
508 | 655 | if (queue != null){ |
509 | 656 | for (int id : this.queue.keySet()){ |
|
0 commit comments