Okhttp websocket发送消息打开socket
Okhttp websocket send message to open socket
现在我有以下 class,它使用 OkHttp 与 WebSockets 一起工作,但它的设置方式是我无法保持 WebSocket 处于打开状态,因此我可以将新消息发送到 Web 套接字。如何在不创建新的 websocket 连接的情况下继续向同一个打开的 websocket 发送消息?
public class OkHttp extends WebSocketListener {
public void run() {
String hostname = "127.0.0.1";
int port = 8888;
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(hostname, port));
OkHttpClient client = new OkHttpClient.Builder().proxy(proxy).readTimeout(10, TimeUnit.SECONDS).build();
Request request = new Request.Builder().url("ws://echo.websocket.org").build();
client.newWebSocket(request, this);
// Trigger shutdown of the dispatcher's executor so this process can exit
// cleanly.
client.dispatcher().executorService().shutdown();
}
@Override
public void onOpen(WebSocket webSocket, Response response) {
webSocket.send("Hello...");
webSocket.send("...World!");
webSocket.send(ByteString.decodeHex("deadbeef"));
webSocket.close(1000, "Goodbye, World!");
}
@Override
public void onMessage(WebSocket webSocket, String text) {
System.out.println("MESSAGE: " + text);
}
@Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
System.out.println("MESSAGE: " + bytes.hex());
}
@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
webSocket.close(1000, null);
System.out.println("CLOSE: " + code + " " + reason);
}
@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
t.printStackTrace();
}
}
newWebSocket()
方法 returns 一个 WebSocket
可用于对出站邮件进行排队。
现在我有以下 class,它使用 OkHttp 与 WebSockets 一起工作,但它的设置方式是我无法保持 WebSocket 处于打开状态,因此我可以将新消息发送到 Web 套接字。如何在不创建新的 websocket 连接的情况下继续向同一个打开的 websocket 发送消息?
public class OkHttp extends WebSocketListener {
public void run() {
String hostname = "127.0.0.1";
int port = 8888;
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(hostname, port));
OkHttpClient client = new OkHttpClient.Builder().proxy(proxy).readTimeout(10, TimeUnit.SECONDS).build();
Request request = new Request.Builder().url("ws://echo.websocket.org").build();
client.newWebSocket(request, this);
// Trigger shutdown of the dispatcher's executor so this process can exit
// cleanly.
client.dispatcher().executorService().shutdown();
}
@Override
public void onOpen(WebSocket webSocket, Response response) {
webSocket.send("Hello...");
webSocket.send("...World!");
webSocket.send(ByteString.decodeHex("deadbeef"));
webSocket.close(1000, "Goodbye, World!");
}
@Override
public void onMessage(WebSocket webSocket, String text) {
System.out.println("MESSAGE: " + text);
}
@Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
System.out.println("MESSAGE: " + bytes.hex());
}
@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
webSocket.close(1000, null);
System.out.println("CLOSE: " + code + " " + reason);
}
@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
t.printStackTrace();
}
}
newWebSocket()
方法 returns 一个 WebSocket
可用于对出站邮件进行排队。