@Override public void onOpen(WebSocket webSocket, Request arg1, Response arg2) throws IOException { mWebSocket = webSocket; setEnvironment(WXEnvironment.getConfig()); WXSDKManager.getInstance().postOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(WXEnvironment.sApplication, "Has switched to DEBUG mode, you can see the DEBUG information on the browser!", Toast.LENGTH_SHORT).show(); } },0); for (JSDebuggerCallback callback : mCallbacks.values()) { callback.onSuccess(arg2); } WXLogUtils.e("into--[onOpen]"); }
@ReactMethod public void close(int code, String reason, int id) { WebSocket client = mWebSocketConnections.get(id); if (client == null) { // WebSocket is already closed // Don't do anything, mirror the behaviour on web FLog.w( ReactConstants.TAG, "Cannot close WebSocket. Unknown WebSocket id " + id); return; } try { client.close(code, reason); mWebSocketConnections.remove(id); } catch (Exception e) { FLog.e( ReactConstants.TAG, "Could not close WebSocket connection for id " + id, e); } }
/** * Sends a string over the websocket * * @param message the string to send */ private void send(String callId, final String message) { if (message == null) { throw new RuntimeException("You cannot send `null` messages"); } try { Timber.d("-->" + message); synchronized (mConnection) { RequestBody request = RequestBody.create(WebSocket.TEXT, message); mConnection.sendMessage(request); } } catch (Exception e) { final Listener listener = mListeners.remove(callId); if (listener != null) { if (listener instanceof ResultListener) { ((ResultListener) listener).onError(new MeteorException(e)); } } if (mCallback != null) { mCallback.onException(e); } } }
public void sendMessage(String message) { if(!isSupportWebSocket){ return; } if (mWebSocket == null) { return; } Buffer messageBuffer = new Buffer(); messageBuffer.writeUtf8(message); try { mWebSocket.sendMessage(WebSocket.PayloadType.TEXT, messageBuffer); } catch (IOException e) { } }
@Override public void onMessage(BufferedSource payload, WebSocket.PayloadType type) throws IOException { if (type != WebSocket.PayloadType.TEXT) { WXLogUtils.w( "Websocket received unexpected message with payload of type " + type); return; } for (JSDebuggerCallback callback : mCallbacks.values()) { callback.onMessage(payload, type); } String message = null; try { message = payload.readUtf8(); JSONObject jsonObject = JSONObject.parseObject(message); Object name = jsonObject.get("method"); Object value = jsonObject.get("arguments"); if (name == null || value == null) { return; } if (TextUtils.equals(name.toString(), "setLogLevel")) { JSONArray jsonArray = JSONObject.parseArray(value.toString()); String level = jsonArray.get(0).toString(); WXEnvironment.sLogLevel = LogLevel.valueOf(level.toUpperCase()); WXLogUtils.v("into--[onMessage]setLogLevel"); } } catch (Exception e) { } finally { payload.close(); } }
@Override public void onMessage(BufferedSource payload, WebSocket.PayloadType type) throws IOException { if (type == WebSocket.PayloadType.TEXT) { String temp = payload.readUtf8(); Log.e(TAG, "into--[onMessage] msg:" + temp); payload.close(); if (TextUtils.equals("refresh", temp) && mHandler != null) { mHandler.obtainMessage(Constants.HOT_REFRESH_REFRESH, 0, 0, mUrl).sendToTarget(); } } }
@ReactMethod public void send(String message, int id) { WebSocket client = mWebSocketConnections.get(id); if (client == null) { // This is a programmer error throw new RuntimeException("Cannot send a message. Unknown WebSocket id " + id); } try { client.sendMessage( WebSocket.PayloadType.TEXT, new Buffer().writeUtf8(message)); } catch (IOException | IllegalStateException e) { notifyWebSocketFailed(id, e.getMessage()); } }
private void sendMessage(int requestID, String message) { if (mWebSocket == null) { triggerRequestFailure( requestID, new IllegalStateException("WebSocket connection no longer valid")); return; } Buffer messageBuffer = new Buffer(); messageBuffer.writeUtf8(message); try { mWebSocket.sendMessage(WebSocket.PayloadType.TEXT, messageBuffer); } catch (IOException e) { triggerRequestFailure(requestID, e); } }
@Override public void onMessage(BufferedSource payload, WebSocket.PayloadType type) throws IOException { if (type != WebSocket.PayloadType.TEXT) { FLog.w(TAG, "Websocket received unexpected message with payload of type " + type); return; } String message = null; try { message = payload.readUtf8(); } finally { payload.close(); } Integer replyID = null; try { JsonParser parser = new JsonFactory().createParser(message); String result = null; while (parser.nextToken() != JsonToken.END_OBJECT) { String field = parser.getCurrentName(); if ("replyID".equals(field)) { parser.nextToken(); replyID = parser.getIntValue(); } else if ("result".equals(field)) { parser.nextToken(); result = parser.getText(); } } if (replyID != null) { triggerRequestSuccess(replyID, result); } } catch (IOException e) { if (replyID != null) { triggerRequestFailure(replyID, e); } else { abort("Parsing response message from websocket failed", e); } } }
@ReactMethod public void sendBinary(String base64String, int id) { WebSocket client = mWebSocketConnections.get(id); if (client == null) { // This is a programmer error throw new RuntimeException("Cannot send a message. Unknown WebSocket id " + id); } try { client.sendMessage( WebSocket.PayloadType.BINARY, new Buffer().write(ByteString.decodeBase64(base64String))); } catch (IOException | IllegalStateException e) { notifyWebSocketFailed(id, e.getMessage()); } }
public void sendMessage(int requestID, String message) { if (mWebSocket == null) { return; } Buffer messageBuffer = new Buffer(); messageBuffer.writeUtf8(message); try { Log.v(TAG, "sendMessage " + message); mWebSocket.sendMessage(WebSocket.PayloadType.TEXT, messageBuffer); } catch (IOException e) { Log.e(TAG, "sendMessage IOException " + e.toString()); } }
@Override public void onOpen(WebSocket webSocket, Request arg1, Response arg2) throws IOException { mWebSocket = webSocket; if (mConnectCallback != null) { mConnectCallback.onSuccess(null); } }
private void write(final Item item) { new Thread() { public void run() { try { socket.sendMessage( WebSocket.PayloadType.TEXT, new Buffer().writeUtf8(item.payload) ); } catch (IOException e) { item.channel.onError(e); } } }.start(); }
@Override public void onMessage(BufferedSource source, WebSocket.PayloadType payloadType) { try { String payload = source.readUtf8(); JSONObject item = new JSONObject(payload); onMessageAll(new AxEvent(client, item)); source.close(); } catch (IOException e) { onErrorAll(e); } }
@Override public void onMessage(BufferedSource source, WebSocket.PayloadType type) throws IOException { String payload = source.readUtf8(); if (payload.contains(pattern)) { res.set(payload); lock.countDown(); } source.close(); }
@Override public void onOpen(final WebSocket socket, final Response response) { new Thread() { public void run() { try { Thread.sleep(100); socket.sendMessage(TEXT, new Buffer().writeUtf8(payload)); } catch (IOException | InterruptedException e) { throw new AssertionError(e); } } }.start(); }
public void sendToServer(PewMessage msg) { final Buffer b = new Buffer(); b.writeUtf8(wireProtocol.wrapNetstring(msg)); pool.execute(new Runnable() { @Override public void run() { try { socket.sendMessage(WebSocket.PayloadType.TEXT, b); } catch (IOException e) { e.printStackTrace(); } } }); }
private void doSend(String data) { if (webSocket != null) { try { webSocket.sendMessage(WebSocket.PayloadType.TEXT, new Buffer().writeUtf8(data)); } catch (IOException e) { if (listener != null) { listener.onFailure(e); } } } }
@Test(timeout = TIMEOUT) public void createAfterOpeningConnection() throws URISyntaxException, IOException, InterruptedException { final BlockingQueue<String> events = new LinkedBlockingQueue<String>(); final MockWebServer mockWebServer = new MockWebServer(); final MockResponse response = new MockResponse(); response.withWebSocketUpgrade(new DefaultWebSocketListener() { @Override public void onMessage(BufferedSource payload, WebSocket.PayloadType type) throws IOException { events.offer("onMessage:" + payload.readUtf8()); payload.close(); } }); mockWebServer.enqueue(response); mockWebServer.start(); final Consumer consumer = new Consumer(mockWebServer.url("/").uri()); consumer.connect(); final Subscriptions subscriptions = consumer.getSubscriptions(); final Subscription subscription = subscriptions.create(new Channel("CommentsChannel")); // Callback test assertThat(events.take(), is("onMessage:" + Command.subscribe(subscription.getIdentifier()).toJson())); mockWebServer.shutdown(); }