当我尝试在Okhttp中为Websocket执行以下代码时,出现错误消息:
原因:java.lang.IllegalArgumentException:意外的url:wws://www.mydomain.com/websocket
String WEBSOCKET_URL = "wws://www.mydomain.com/websocket";
OkHttpClient client = new OkHttpClient.Builder()
.readTimeout(0, TimeUnit.MILLISECONDS)
.build();
Request request = new Request.Builder().url(WEBSOCKET_URL).build();
WebSocket webSocket = client.newWebSocket(request, new WebSocketListener() {
@Override
public void onOpen(WebSocket webSocket, okhttp3.Response response) {
Toast.makeText(HomeCoach.this, "Connected to the Websocket server", Toast.LENGTH_SHORT).show();
}
@Override
public void onMessage(WebSocket webSocket, String text) {
super.onMessage(webSocket, text);
}
@Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
super.onMessage(webSocket, bytes);
}
@Override
public void onClosed(WebSocket webSocket, int code, String reason) {
super.onClosed(webSocket, code, reason);
}
@Override
public void onFailure(WebSocket webSocket, Throwable t, @Nullable okhttp3.Response response) {
Toast.makeText(HomeCoach.this, "Error ", Toast.LENGTH_SHORT).show();
}
});
答案 0 :(得分:1)
不是WWS,而是WSS。将您的网址更改为String WEBSOCKET_URL = "wss://www.mydomain.com/websocket";