I am attempting to create a simple connection between a python SocketIO server on repl.it and an Android Java app. When I attempt to connect to the socket nothing is happening.
Client:
Socket socket;
try {
socket = IO.socket("https://444ab0e8-2ef4-4b65-b8f8-c1116f6bd6bd-00-2z34bd8dh1mjs.riker.replit.dev:5000/");
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
Toast.makeText(MainActivity.this, "Connected", Toast.LENGTH_SHORT).show();
}
});
Server:
import eventlet
import socketio
sio = socketio.Server()
app = socketio.WSGIApp(sio)
clients = []
@sio.event
def connect(sid):
clients.append(sid)
print('connect ', sid)
@sio.event
def disconnect(sid):
clients.remove(sid)
print('disconnect ', sid)
if __name__ == '__main__':
eventlet.wsgi.server(eventlet.listen(('', 5000)), app)
When I add socket.connect() or socket.open() under the socket initialization, it connects very briefly but the app crashes immediately. When I leave it out, nothing happens. I've also tried connecting with a python client and it works fine. The problem seems to be solely with the Android app.
I have fixed the problem. The reason it was crashing on socket.connect() was because I was attempting to Toast within the thread handling the connection event which was crashing the app. I fixed this by simple replacing the Toast with a Log message
Socket socket;
try {
socket = IO.socket("https://444ab0e8-2ef4-4b65-b8f8-c1116f6bd6bd-00-2z34bd8dh1mjs.riker.replit.dev:5000/");
socket.connect();
} catch (Exception e) {
Log.e("STATE", "e", e);
throw new RuntimeException("dasoi");
}
socket.on("message", new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.d("STATE", "connected");
}
});