-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Open
Labels
Description
Anyone have a problem with websockets? I have created my class but once i connect to the websocket, it close after about 2-3 seconds, with a code of 1006.
Here is my code, if anyone knows of anything i have done wrong.
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import fi.iki.elonen.NanoWSD;
public class MyHttpServer extends NanoWSD {
private Context context;
private static final int PORT = 8765;
private List<MyWebSocket> connections = new ArrayList<MyWebSocket>();
public MyHttpServer(Context context) throws IOException {
super(PORT);
this.context = context;
}
public List<MyWebSocket> getConnections() {
return this.connections;
}
@Override
protected WebSocket openWebSocket(IHTTPSession handshake) {
return new MyWebSocket(handshake, this);
}
@Override
public Response serveHttp(IHTTPSession session) {
String url = session.getUri();
// some custom code here for serving web responses
return super.serve(session);
}
}
And i have also created a webSocket class
import fi.iki.elonen.NanoWSD;
public class MyWebSocket extends NanoWSD.WebSocket {
MyHttpServer httpServer;
NanoHTTPD.IHTTPSession httpSession;
public MyWebSocket(NanoHTTPD.IHTTPSession handshakeRequest, MyHttpServer httpServer) {
super(handshakeRequest);
this.httpSession = handshakeRequest;
this.httpServer = httpServer;
}
@Override
protected void onOpen() {
httpServer.getConnections().add(this);
}
@Override
protected void onClose(NanoWSD.WebSocketFrame.CloseCode code, String reason, boolean initiatedByRemote) {
this.httpServer.getConnections().remove(this);
}
@Override
protected void onMessage(NanoWSD.WebSocketFrame message) {
}
@Override
protected void onPong(NanoWSD.WebSocketFrame pong) {
}
@Override
protected void onException(IOException exception) {
}
}
I am guessing this is all ok?? Why would the websocket be disconnecting after a couple seconds?