我使用Tootallnate java-websocket在android上创建了一个websocket服务器。我已将其托管在10.0.2.2和端口号15中。如何连接同样在同一模拟器中运行的客户端?当我使用下面的代码时,即使服务器没有运行它也会连接!这是为什么?我究竟做错了什么?我测试了我的服务器。打印实例和地址等。在服务器端一切正常。请指教。
package com.example.android_websocketclient_test;
import java.net.URI;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.handshake.ServerHandshake;
import android.util.Log;
public class EmptyClient extends WebSocketClient{
public EmptyClient(URI serverUri, Draft draft) {
super(serverUri, draft);
}
public EmptyClient(URI serverURI) {
super(serverURI);
}
@Override
public void onOpen(ServerHandshake handshakedata) {
Log.d("test app","new connection opened");
System.out.println("new connection opened");
}
@Override
public void onClose(int code, String reason, boolean remote) {
System.out.println("closed with exit code " + code + " additional info: " + reason);
}
@Override
public void onMessage(String message) {
System.out.println("received message: " + message);
}
@Override
public void onError(Exception ex) {
System.err.println("an error occurred:" + ex);
}
}
以下是我创建客户端的方式。
public static void main(String[] args) throws URISyntaxException {
WebSocketClient client = new EmptyClient(new URI("ws://10.0.2.2:15"), new Draft_10());
client.connect();
}