本文整理汇总了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;
}
示例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);
}
}
示例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()");
}
}
示例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;
}
示例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");
}
示例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);
}
}
});
}
示例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());
}
}
示例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());
}
}
示例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());
}
}
示例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()));
}
示例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);
}
}
示例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);
}
}
示例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());
}
示例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());
}
示例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());
}