I have created a websocket server on android using Tootallnate java-websocket. I have hosted it in 10.0.2.2 and port number 15. How can I connect my client who is also running in the same emulator? When I use the below code, it connects even when the server is not running! why is that? what am I doing wrong? I have tested my server. Printed the instance and address and all. Everything works fine in server side. Please advice.
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);
}
}
Following is how I create my client.
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();
}