当前位置: 首页>>代码示例>>Java>>正文


Java WebSocket类代码示例

本文整理汇总了Java中okhttp3.ws.WebSocket的典型用法代码示例。如果您正苦于以下问题:Java WebSocket类的具体用法?Java WebSocket怎么用?Java WebSocket使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


WebSocket类属于okhttp3.ws包,在下文中一共展示了WebSocket类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: socket

import okhttp3.ws.WebSocket; //导入依赖的package包/类
private WebSocket socket() {
  WebSocket mockedSocket = new WebSocket() {
    @Override public void sendMessage(RequestBody message) throws IOException {
      messages.onNext(new ResponseEnvelope());
    }

    @Override public void sendPing(Buffer payload) throws IOException {

    }

    @Override public void close(int code, String reason) throws IOException {
      messages.onCompleted();
    }
  };

  return mockedSocket;
}
 
开发者ID:csync,项目名称:csync-android,代码行数:18,代码来源:MockWebSocketConnection.java

示例2: close

import okhttp3.ws.WebSocket; //导入依赖的package包/类
@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
    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);
  }
}
 
开发者ID:qq565999484,项目名称:RNLearn_Project1,代码行数:19,代码来源:WebSocketModule.java

示例3: onMessage

import okhttp3.ws.WebSocket; //导入依赖的package包/类
@Override
public void onMessage(ResponseBody message) throws IOException {
    logInfo(" onMessage("+message+") while environment is " +
            "(state="+mState+", address="+address+", " +
            "SendMsgQueueSize="+mSendMessageThread.mMessageQueue.size()+")");

    try {
        if (mListener != null) {
            if (message.contentType() == WebSocket.TEXT) {
                String result = message.string();
                logData("onMessage() recv TEXT: " + result);
                mListener.onMessage(BJWebSocketClient.this, result);
            } else {
                mListener.onMessage(BJWebSocketClient.this, message.byteStream());
            }
        }

        message.close();
    } catch (IOException e) {
        e.printStackTrace();

        logException(e, "onMessage()");
    }

}
 
开发者ID:yangxlei,项目名称:bjnetwork,代码行数:26,代码来源:BJWebSocketClient.java

示例4: sendMessage

import okhttp3.ws.WebSocket; //导入依赖的package包/类
<Req extends Request, Resp extends Response> boolean sendMessage(Req message, Class<Resp> respCls,
                                                                 Callback<Resp> callback) {
    if (isClosed()) return false;
    try {
        final List<Req> messageList = Collections.singletonList(message);
        //noinspection unchecked
        final JsonMapper<Req> mapper = (JsonMapper<Req>) LoganSquare.mapperFor(message.getClass());
        final String messageJson = mapper.serialize(messageList);
        final String id = message.getId();
        resultHandlers.put(id, new SendResultHandler<>(id, respCls, callback));
        webSocket.sendMessage(RequestBody.create(WebSocket.TEXT, messageJson));
    } catch (IOException e) {
        faye.unconnected(e, -1, null);
        disconnect();
        return false;
    }
    return true;
}
 
开发者ID:mariotaku,项目名称:OkFaye,代码行数:19,代码来源:FayeImpl.java

示例5: onOpen

import okhttp3.ws.WebSocket; //导入依赖的package包/类
/************************************
 * WebSocketListener Implementation *
 ************************************/

@Override
public void onOpen(WebSocket webSocket, Response response) {
    retryCount.set(0);
    ws = webSocket;
    state.set(State.CONNECTED);
    logger.info("Connected to server: " + ws_url);

    // only fire connected event once and not for reconnects
    if (firstConnection) {
        connCallback.connected("");
        firstConnection = false;
        return;
    }

    connCallback.connected("reconnected");
}
 
开发者ID:nbusy,项目名称:nbusy-android,代码行数:21,代码来源:ConnImpl.java

示例6: sendMessage

import okhttp3.ws.WebSocket; //导入依赖的package包/类
public Observable<Boolean> sendMessage(String message) {
  return Observable.just(RequestBody.create(WebSocket.TEXT, message))
      .map(new Func1<RequestBody, Boolean>() {
        @Override public Boolean call(RequestBody requestBody) {
          try {
            socket.sendMessage(requestBody);
            return true;
          } catch (IOException e) {
            throw Exceptions.propagate(e);
          }
        }
      });
}
 
开发者ID:csync,项目名称:csync-android,代码行数:14,代码来源:OkHttpWebSocketConnection.java

示例7: send

import okhttp3.ws.WebSocket; //导入依赖的package包/类
@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(RequestBody.create(WebSocket.TEXT, message));
  } catch (IOException | IllegalStateException e) {
    notifyWebSocketFailed(id, e.getMessage());
  }
}
 
开发者ID:qq565999484,项目名称:RNLearn_Project1,代码行数:14,代码来源:WebSocketModule.java

示例8: sendBinary

import okhttp3.ws.WebSocket; //导入依赖的package包/类
@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(
      RequestBody.create(WebSocket.BINARY, ByteString.decodeBase64(base64String)));
  } catch (IOException | IllegalStateException e) {
    notifyWebSocketFailed(id, e.getMessage());
  }
}
 
开发者ID:qq565999484,项目名称:RNLearn_Project1,代码行数:15,代码来源:WebSocketModule.java

示例9: ping

import okhttp3.ws.WebSocket; //导入依赖的package包/类
@ReactMethod
public void ping(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 {
    Buffer buffer = new Buffer();
    client.sendPing(buffer);
  } catch (IOException | IllegalStateException e) {
    notifyWebSocketFailed(id, e.getMessage());
  }
}
 
开发者ID:qq565999484,项目名称:RNLearn_Project1,代码行数:15,代码来源:WebSocketModule.java

示例10: send

import okhttp3.ws.WebSocket; //导入依赖的package包/类
public void send(JSONObject object) throws IOException {
  if (mWebSocket == null) {
    return;
  }

  mWebSocket.sendMessage(RequestBody.create(WebSocket.TEXT, object.toString()));
}
 
开发者ID:qq565999484,项目名称:RNLearn_Project1,代码行数:8,代码来源:InspectorPackagerConnection.java

示例11: sendMessage

import okhttp3.ws.WebSocket; //导入依赖的package包/类
private void sendMessage(int requestID, String message) {
  if (mWebSocket == null) {
    triggerRequestFailure(
        requestID,
        new IllegalStateException("WebSocket connection no longer valid"));
    return;
  }
  try {
    mWebSocket.sendMessage(RequestBody.create(WebSocket.TEXT, message));
  } catch (IOException e) {
    triggerRequestFailure(requestID, e);
  }
}
 
开发者ID:qq565999484,项目名称:RNLearn_Project1,代码行数:14,代码来源:JSDebuggerWebSocketClient.java

示例12: respond

import okhttp3.ws.WebSocket; //导入依赖的package包/类
public void respond(Object result) {
  try {
    JSONObject message = new JSONObject();
    message.put("version", PROTOCOL_VERSION);
    message.put("id", mId);
    message.put("result", result);
    mWebSocket.sendMessage(RequestBody.create(WebSocket.TEXT, message.toString()));
  } catch (Exception e) {
    FLog.e(TAG, "Responding failed", e);
  }
}
 
开发者ID:qq565999484,项目名称:RNLearn_Project1,代码行数:12,代码来源:JSPackagerClient.java

示例13: test_onMessage_WithInvalidContentType_ShouldNotTriggerCallbacks

import okhttp3.ws.WebSocket; //导入依赖的package包/类
@Test
public void test_onMessage_WithInvalidContentType_ShouldNotTriggerCallbacks() throws Exception {
  JSDebuggerWebSocketClient client = PowerMockito.spy(new JSDebuggerWebSocketClient());

  client.onMessage(ResponseBody.create(WebSocket.BINARY, "{\"replyID\":0, \"result\":\"OK\"}"));
  PowerMockito.verifyPrivate(client, never()).invoke("triggerRequestSuccess", anyInt(), anyString());
  PowerMockito.verifyPrivate(client, never()).invoke("triggerRequestFailure", anyInt(), any());
}
 
开发者ID:qq565999484,项目名称:RNLearn_Project1,代码行数:9,代码来源:JSDebuggerWebSocketClientTest.java

示例14: test_onMessage_WithoutReplyId_ShouldNotTriggerCallbacks

import okhttp3.ws.WebSocket; //导入依赖的package包/类
@Test
public void test_onMessage_WithoutReplyId_ShouldNotTriggerCallbacks() throws Exception {
  JSDebuggerWebSocketClient client = PowerMockito.spy(new JSDebuggerWebSocketClient());

  client.onMessage(ResponseBody.create(WebSocket.TEXT, "{\"result\":\"OK\"}"));
  PowerMockito.verifyPrivate(client, never()).invoke("triggerRequestSuccess", anyInt(), anyString());
  PowerMockito.verifyPrivate(client, never()).invoke("triggerRequestFailure", anyInt(), any());
}
 
开发者ID:qq565999484,项目名称:RNLearn_Project1,代码行数:9,代码来源:JSDebuggerWebSocketClientTest.java

示例15: test_onMessage_With_Null_ReplyId_ShouldNotTriggerCallbacks

import okhttp3.ws.WebSocket; //导入依赖的package包/类
@Test
public void test_onMessage_With_Null_ReplyId_ShouldNotTriggerCallbacks() throws Exception {
  JSDebuggerWebSocketClient client = PowerMockito.spy(new JSDebuggerWebSocketClient());

  client.onMessage(ResponseBody.create(WebSocket.TEXT, "{\"replyID\":null, \"result\":\"OK\"}"));
  PowerMockito.verifyPrivate(client, never()).invoke("triggerRequestSuccess", anyInt(), anyString());
  PowerMockito.verifyPrivate(client, never()).invoke("triggerRequestFailure", anyInt(), any());
}
 
开发者ID:qq565999484,项目名称:RNLearn_Project1,代码行数:9,代码来源:JSDebuggerWebSocketClientTest.java


注:本文中的okhttp3.ws.WebSocket类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。