Skip to content

Instantly share code, notes, and snippets.

@jumperchen
Created May 3, 2017 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jumperchen/6f8410b871a6d3746fe1967340d7a1a1 to your computer and use it in GitHub Desktop.
Save jumperchen/6f8410b871a6d3746fe1967340d7a1a1 to your computer and use it in GitHub Desktop.
onClosing : 1001 reason
import java.util.concurrent.TimeUnit;
import okio.ByteString;
public class TestOKHttp {
private static OkHttpClient client;
private static WebSocket ws;
private static boolean success = false;
public static void main(String[] args) throws Exception {
client = new OkHttpClient.Builder().pingInterval(30, TimeUnit.SECONDS).build();
Request wsRequest = new Request.Builder()
.url("ws://echo.websocket.org")
.build();
ws = client.newWebSocket(wsRequest, new WebSocketListener() {
@Override
public void onOpen(WebSocket webSocket, Response response) {
System.out.println("onOpen");
for (int i = 0; i < 1000000; i++)
webSocket.send("Long.........words count: " + i);
}
@Override
public void onMessage(WebSocket webSocket, String text) {
System.out.println("onMessage : " + text);
success = true;
}
@Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
System.out.println("onMessage binary ");
}
@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
System.out.println("onClosing : " + code + " " + reason);
}
@Override
public void onClosed(WebSocket webSocket, int code, String reason) {
System.out.println("onClosed" + code + " " + reason);
}
@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
System.out.println("onFailure : " + t.getMessage());
t.printStackTrace();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment