okhttp3.WebSocket类的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(5.8k)|赞(0)|评价(0)|浏览(398)

本文整理了Java中okhttp3.WebSocket类的一些代码示例,展示了WebSocket类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。WebSocket类的具体详情如下:
包路径:okhttp3.WebSocket
类名称:WebSocket

WebSocket介绍

[英]A non-blocking interface to a web socket. Use the WebSocket.Factory to create instances; usually this is OkHttpClient.

Web Socket Lifecycle

Upon normal operation each web socket progresses through a sequence of states:

  • Connecting: the initial state of each web socket. Messages may be enqueued but they won't be transmitted until the web socket is open.

  • Open: the web socket has been accepted by the remote peer and is fully operational. Messages in either direction are enqueued for immediate transmission.

  • Closing: one of the peers on the web socket has initiated a graceful shutdown. The web socket will continue to transmit already-enqueued messages but will refuse to enqueue new ones.

  • Closed: the web socket has transmitted all of its messages and has received all messages from the peer.
    Web sockets may fail due to HTTP upgrade problems, connectivity problems, or if either peer chooses to short-circuit the graceful shutdown process:

  • Canceled: the web socket connection failed. Messages that were successfully enqueued by either peer may not have been transmitted to the other.
    Note that the state progression is independent for each peer. Arriving at a gracefully-closed state indicates that a peer has sent all of its outgoing messages and received all of its incoming messages. But it does not guarantee that the other peer will successfully receive all of its incoming messages.
    [中]web套接字的非阻塞接口。使用WebSocket。创建实例的工厂;通常这是OkHttpClient。
    ####Web套接字生命周期
    正常运行时,每个web套接字都会经历一系列状态:
    *连接:每个web套接字的初始状态。消息可以排队,但在web套接字打开之前不会传输。
    *打开:web套接字已被远程对等方接受,并已完全运行。两个方向的消息都排队等待立即传输。
    *关闭:web套接字上的一个对等方启动了正常关闭。web套接字将继续传输已排队的消息,但拒绝将新消息排队。
    *关闭:web套接字已传输其所有消息,并已接收来自对等方的所有消息。
    Web套接字可能会因HTTP升级问题、连接问题或任一对等方选择短路关闭过程而失败:
    *已取消:web套接字连接失败。任何一方成功排队的消息可能尚未传输到另一方。
    请注意,状态进程对于每个对等点都是独立的。到达优雅关闭状态表示对等方已发送其所有传出消息并接收其所有传入消息。但它不能保证另一个对等方将成功接收其所有传入消息。

代码示例

代码示例来源:origin: square/okhttp

@Override public void onMessage(final WebSocket webSocket, final String text) {
 webSocket.send(text);
}

代码示例来源:origin: square/okhttp

@Override public void close() throws IOException {
  if (webSocket == null) return;

  WebSocket webSocket;
  synchronized (this) {
   webSocket = this.webSocket;
  }

  if (webSocket != null) {
   webSocket.close(1000, "bye");
  }
 }
}

代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking

private void disconnectWebSocket() {
  if (webSocket != null) {
    webSocket.cancel();
  }
}

代码示例来源:origin: square/okhttp

@Override public void onOpen(WebSocket webSocket, Response response) {
 webSocket.send("Hello...");
 webSocket.send("...World!");
 webSocket.send(ByteString.decodeHex("deadbeef"));
 webSocket.close(1000, "Goodbye, World!");
}

代码示例来源:origin: watson-developer-cloud/java-sdk

while (socket.queueSize() > QUEUE_SIZE_LIMIT) {
 Thread.sleep(QUEUE_WAIT_MILLIS);
 socket.send(ByteString.of(buffer));
} else {
 socket.send(ByteString.of(Arrays.copyOfRange(buffer, 0, read)));

代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking

@Override
public void onOpen(WebSocket webSocket, Response response) {
  webSocket.send("Hello...");
  webSocket.send("...World!");
  webSocket.send(ByteString.decodeHex("deadbeef"));
  webSocket.close(1000, "Goodbye, World!");
}

代码示例来源:origin: com.ibm.watson.developer_cloud/speech-to-text

while (socket.queueSize() > QUEUE_SIZE_LIMIT) {
 Thread.sleep(QUEUE_WAIT_MILLIS);
 socket.send(ByteString.of(buffer));
} else {
 socket.send(ByteString.of(Arrays.copyOfRange(buffer, 0, read)));

代码示例来源:origin: square/okhttp

@Override public void onMessage(final WebSocket webSocket, final ByteString bytes) {
 webSocket.send(bytes);
}

代码示例来源:origin: square/okhttp

@Override public void onClosing(WebSocket webSocket, int code, String reason) {
 webSocket.close(1000, null);
 latch.countDown();
}

代码示例来源:origin: Rabtman/WsManager

@Override
public void onMessage(WebSocket webSocket, String string) {
  System.out.println("server onMessage");
  System.out.println("message:" + string);
  //接受到5条信息后,关闭消息定时发送器
  if (msgCount == 5) {
    mTimer.cancel();
    webSocket.close(1000, "close by server");
    return;
  }
  webSocket.send("response-" + string);
}

代码示例来源:origin: openshift/openshift-restclient-java

@Override
public void stop() {
  if (call != null) {
    call.cancel();
  } else {
    shouldStop = true;
  }
}

代码示例来源:origin: runelite/runelite

private void send(WebsocketMessage message)
{
  if (webSocket == null)
  {
    log.debug("Reconnecting to server");
    connect();
  }
  String json = GSON.toJson(message, WebsocketMessage.class);
  webSocket.send(json);
  log.debug("Sent: {}", json);
}

代码示例来源:origin: square/okhttp

@Override public void onClosing(WebSocket webSocket, int code, String reason) {
 webSocket.close(1000, null);
 latch.countDown();
}

代码示例来源:origin: watson-developer-cloud/java-sdk

serverSocket.send("{\"state\": {}}");
webSocketRecorder.assertExhausted();
serverSocket.close(1000, null);

代码示例来源:origin: com.openshift/openshift-restclient-java

@Override
public void stop() {
  if (call != null) {
    call.cancel();
  } else {
    shouldStop = true;
  }
}

代码示例来源:origin: apollographql/apollo-android

@Override
public void send(OperationClientMessage message) {
 WebSocket socket = webSocket.get();
 if (socket == null) {
  throw new IllegalStateException("Not connected");
 }
 socket.send(message.toJsonString());
}

代码示例来源:origin: square/okhttp

@Override public void onClosing(WebSocket webSocket, int code, String reason) {
 webSocket.close(1000, null);
 latch.countDown();
}

代码示例来源:origin: fedepaol/websocket-sample

public void disconnect() {
  mWebSocket.cancel();
  mListener = null;
  mMessageHandler.removeCallbacksAndMessages(null);
  mStatusHandler.removeCallbacksAndMessages(null);
}

代码示例来源:origin: fabric8io/kubernetes-client

private void send(byte[] bytes) throws IOException {
  if (bytes.length > 0) {
    WebSocket ws = webSocketRef.get();
    if (ws != null) {
      byte[] toSend = new byte[bytes.length + 1];
      toSend[0] = 0;
      System.arraycopy(bytes, 0, toSend, 1, bytes.length);
      ws.send(ByteString.of(toSend));
    }
  }
}

代码示例来源:origin: square/okhttp

@Override public void onClosing(WebSocket webSocket, int code, String reason) {
 webSocket.close(1000, null);
 System.out.println("onClose (" + code + "): " + reason);
}

相关文章

微信公众号

最新文章

更多