com.squareup.okhttp.ws.WebSocket Java Examples

The following examples show how to use com.squareup.okhttp.ws.WebSocket. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultWebSocketAdapter.java    From incubator-weex-playground with Apache License 2.0 6 votes vote down vote up
@Override
public void send(String data) {
    if (ws != null) {
        try {
            Buffer buffer = new Buffer().writeUtf8(data);
            ws.sendMessage(WebSocket.PayloadType.TEXT, buffer.buffer());
            buffer.flush();
            buffer.close();

            wsEventReporter.frameSent(data);
        } catch (Exception e) {
            e.printStackTrace();
            reportError(e.getMessage());
            wsEventReporter.frameError(e.getMessage());
        }
    } else {
        reportError("WebSocket is not ready");
    }
}
 
Example #2
Source File: WXWebSocketManager.java    From weex with Apache License 2.0 6 votes vote down vote up
@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]");
}
 
Example #3
Source File: DebugSocketClient.java    From weex with Apache License 2.0 5 votes vote down vote up
@Override
public void onOpen(WebSocket webSocket, Request arg1, Response arg2)
        throws IOException {
    mWebSocket = webSocket;
    if (mConnectCallback != null) {
        mConnectCallback.onSuccess(null);
    }
}
 
Example #4
Source File: SubscriptionTest.java    From actioncable-client-java with MIT License 5 votes vote down vote up
@Test(timeout = TIMEOUT)
public void performWithDataByDefaultInterface() throws URISyntaxException, InterruptedException, IOException {
    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(payload.readUtf8());
            payload.close();
        }
    });
    mockWebServer.enqueue(response);
    mockWebServer.start();

    final Consumer consumer = new Consumer(mockWebServer.url("/").uri());
    final Subscription subscription = consumer.getSubscriptions().create(new Channel("CommentsChannel"));
    consumer.connect();

    events.take(); // { command: subscribe }

    final JsonObject data = new JsonObject();
    data.addProperty("foo", "bar");
    subscription.perform("follow", data);

    final JsonObject expected = new JsonObject();
    expected.addProperty("command", "message");
    expected.addProperty("identifier", subscription.getIdentifier());
    expected.addProperty("data", data.toString());
    assertThat(events.take(), is(expected.toString()));

    mockWebServer.shutdown();
}
 
Example #5
Source File: SubscriptionsTest.java    From actioncable-client-java with MIT License 5 votes vote down vote up
@Test(timeout = TIMEOUT)
public void removeWhenIdentifierIsNotUnique() throws 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);

    final Consumer consumer = new Consumer(mockWebServer.url("/").uri());
    final Subscriptions subscriptions = consumer.getSubscriptions();

    final Subscription subscription1 = subscriptions.create(new Channel("CommentsChannel"));
    // Channel is same as subscription1
    final Subscription subscription2 = subscriptions.create(new Channel("CommentsChannel"));

    consumer.connect();

    events.take(); // WebSocketListener#onMessage (subscribe)
    events.take(); // WebSocketListener#onMessage (subscribe)

    subscriptions.remove(subscription1);

    assertThat(subscriptions.contains(subscription1), is(false));
    assertThat(subscriptions.contains(subscription2), is(true));

    assertThat(events.take(), is("onMessage:" + Command.unsubscribe(subscription1.getIdentifier()).toJson()));

    mockWebServer.shutdown();
}
 
Example #6
Source File: SubscriptionsTest.java    From actioncable-client-java with MIT License 5 votes vote down vote up
@Test(timeout = TIMEOUT)
public void removeWhenIdentifierIsUnique() throws 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);

    final Consumer consumer = new Consumer(mockWebServer.url("/").uri());
    final Subscriptions subscriptions = consumer.getSubscriptions();

    final Subscription subscription1 = subscriptions.create(new Channel("CommentsChannel"));
    final Subscription subscription2 = subscriptions.create(new Channel("NotificationChannel"));

    consumer.connect();

    events.take(); // WebSocketListener#onMessage (subscribe)
    events.take(); // WebSocketListener#onMessage (subscribe)

    subscriptions.remove(subscription1);

    assertThat(subscriptions.contains(subscription1), is(false));
    assertThat(subscriptions.contains(subscription2), is(true));

    assertThat(events.take(), is("onMessage:" + Command.unsubscribe(subscription1.getIdentifier()).toJson()));

    mockWebServer.shutdown();
}
 
Example #7
Source File: SubscriptionsTest.java    From actioncable-client-java with MIT License 5 votes vote down vote up
@Test(timeout = TIMEOUT)
public void createBeforeOpeningConnection() 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());

    final Subscriptions subscriptions = consumer.getSubscriptions();
    final Subscription subscription = subscriptions.create(new Channel("CommentsChannel"));

    consumer.connect();

    // Callback test
    assertThat(events.take(), is("onMessage:" + Command.subscribe(subscription.getIdentifier()).toJson()));

    mockWebServer.shutdown();
}
 
Example #8
Source File: SubscriptionsTest.java    From actioncable-client-java with MIT License 5 votes vote down vote up
@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();
}
 
Example #9
Source File: Connection.java    From actioncable-client-java with MIT License 5 votes vote down vote up
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);
            }
        }
    }
}
 
Example #10
Source File: HotRefreshManager.java    From weex with Apache License 2.0 5 votes vote down vote up
@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();
    }
  }
}
 
Example #11
Source File: SubscriptionTest.java    From actioncable-client-java with MIT License 5 votes vote down vote up
@Test(timeout = TIMEOUT)
public void performWithDataByCustomInterface() throws URISyntaxException, InterruptedException, IOException {
    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(payload.readUtf8());
            payload.close();
        }
    });
    mockWebServer.enqueue(response);
    mockWebServer.start();

    final Consumer consumer = new Consumer(mockWebServer.url("/").uri());
    final Subscription subscription = consumer.getSubscriptions().create(new Channel("CommentsChannel"), CustomSubscription.class);
    consumer.connect();

    events.take(); // { command: subscribe }

    final JsonObject data = new JsonObject();
    data.addProperty("foo", "bar");
    subscription.perform("follow", data);

    final JsonObject expected = new JsonObject();
    expected.addProperty("command", "message");
    expected.addProperty("identifier", subscription.getIdentifier());
    expected.addProperty("data", data.toString());
    assertThat(events.take(), is(expected.toString()));

    mockWebServer.shutdown();
}
 
Example #12
Source File: SubscriptionTest.java    From actioncable-client-java with MIT License 5 votes vote down vote up
@Test(timeout = TIMEOUT)
public void performByDefaultInterface() throws URISyntaxException, InterruptedException, IOException {
    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(payload.readUtf8());
            payload.close();
        }
    });
    mockWebServer.enqueue(response);
    mockWebServer.start();

    final Consumer consumer = new Consumer(mockWebServer.url("/").uri());
    final Subscription subscription = consumer.getSubscriptions().create(new Channel("CommentsChannel"));
    consumer.connect();

    events.take(); // { command: subscribe }

    subscription.perform("follow");

    final JsonObject expected = new JsonObject();
    expected.addProperty("command", "message");
    expected.addProperty("identifier", subscription.getIdentifier());
    expected.addProperty("data", "{\"action\":\"follow\"}");
    assertThat(events.take(), is(expected.toString()));

    mockWebServer.shutdown();
}
 
Example #13
Source File: DebugSocketClient.java    From weex with Apache License 2.0 5 votes vote down vote up
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());
    }
}
 
Example #14
Source File: SubscriptionTest.java    From actioncable-client-java with MIT License 5 votes vote down vote up
@Test(timeout = TIMEOUT)
public void performByCustomInterface() throws URISyntaxException, InterruptedException, IOException {
    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(payload.readUtf8());
            payload.close();
        }
    });
    mockWebServer.enqueue(response);
    mockWebServer.start();

    final Consumer consumer = new Consumer(mockWebServer.url("/").uri());
    final Subscription subscription = consumer.getSubscriptions().create(new Channel("CommentsChannel"), CustomSubscription.class);
    consumer.connect();

    events.take(); // { command: subscribe }

    subscription.perform("follow");

    final JsonObject expected = new JsonObject();
    expected.addProperty("command", "message");
    expected.addProperty("identifier", subscription.getIdentifier());
    expected.addProperty("data", "{\"action\":\"follow\"}");
    assertThat(events.take(), is(expected.toString()));

    mockWebServer.shutdown();
}
 
Example #15
Source File: WXWebSocketManager.java    From weex with Apache License 2.0 5 votes vote down vote up
@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();
  }
}
 
Example #16
Source File: WXWebSocketManager.java    From weex with Apache License 2.0 5 votes vote down vote up
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) {
  }
}
 
Example #17
Source File: HotRefreshManager.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 5 votes vote down vote up
@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();
    }
  }
}
 
Example #18
Source File: HotRefreshManager.java    From incubator-weex-playground with Apache License 2.0 5 votes vote down vote up
@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();
    }
  }
}
 
Example #19
Source File: SubscriptionTest.java    From actioncable-client-java with MIT License 4 votes vote down vote up
@Override
public void onMessage(BufferedSource payload, WebSocket.PayloadType type) throws IOException {
    payload.close();
}
 
Example #20
Source File: SubscriptionTest.java    From actioncable-client-java with MIT License 4 votes vote down vote up
@Override
public void onOpen(WebSocket webSocket, Response response) {
}
 
Example #21
Source File: ConsumerTest.java    From actioncable-client-java with MIT License 4 votes vote down vote up
@Override
public void onMessage(BufferedSource payload, WebSocket.PayloadType type) throws IOException {
    payload.close();
}
 
Example #22
Source File: ConsumerTest.java    From actioncable-client-java with MIT License 4 votes vote down vote up
@Override
public void onOpen(WebSocket webSocket, Response response) {
}
 
Example #23
Source File: ConnectionTest.java    From actioncable-client-java with MIT License 4 votes vote down vote up
@Override
public void onMessage(BufferedSource payload, WebSocket.PayloadType type) throws IOException {
}
 
Example #24
Source File: ConnectionTest.java    From actioncable-client-java with MIT License 4 votes vote down vote up
@Override
public void onOpen(WebSocket webSocket, Response response) {
}
 
Example #25
Source File: SubscriptionsTest.java    From actioncable-client-java with MIT License 4 votes vote down vote up
@Override
public void onMessage(BufferedSource payload, WebSocket.PayloadType type) throws IOException {
    payload.close();
}
 
Example #26
Source File: SubscriptionsTest.java    From actioncable-client-java with MIT License 4 votes vote down vote up
@Override
public void onOpen(WebSocket webSocket, Response response) {
}
 
Example #27
Source File: HotRefreshManager.java    From weex with Apache License 2.0 4 votes vote down vote up
@Override
public void onOpen(WebSocket webSocket, Request request, Response response) throws IOException {
  mWebSocket = webSocket;
}
 
Example #28
Source File: DebugSocketClient.java    From weex with Apache License 2.0 4 votes vote down vote up
@Override
public void onMessage(BufferedSource payload, WebSocket.PayloadType type)
        throws IOException {
    mProxy.handleMessage(payload, type);
}
 
Example #29
Source File: HotRefreshManager.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 4 votes vote down vote up
@Override
public void onOpen(WebSocket webSocket, Request request, Response response) throws IOException {
  mWebSocket = webSocket;
}
 
Example #30
Source File: HotRefreshManager.java    From incubator-weex-playground with Apache License 2.0 4 votes vote down vote up
@Override
public void onOpen(WebSocket webSocket, Request request, Response response) throws IOException {
  mWebSocket = webSocket;
}