'Cannot connect Flask-SocketIO with unity
I need to connect unity(.net) with flask_socketIo, In short, the connection works well between the server and the client locally, but never online.
I used a unity wrapper for this link very simple to use:
public void testIO()
{
KyleDulce.SocketIo.Socket s;
s = SocketIo.establishSocketConnection("ws://localhost:5000");
s.connect();
s.on("OCR", OnConnectRespond);
}
public void OnConnectRespond(string m){ Debug.Log("msg: " + m);
and the flask application looks like this
from flask import Flask
from flask_socketio import SocketIO, emit
async_mode = None
#eventlet
import eventlet
import eventlet.wsgi
eventlet.monkey_patch()
app = Flask(__name__)
app.config['SECRET_KEY'] = '<hiddenValue>'
socketio = SocketIO(app, async_mode='eventlet')
@socketio.event()
def connect():
print(f'you are CONNECTED!')
emit('OCR',"a Msg from the server")
if __name__ == '__main__':
socketio.run(app,debug=True,port=5000)
that works locally without any issues, as soon as I fire testIO() the server receives the connection and OCR() gets fired without any issues.
but as soon as I tried to deploy that on Cpanel python app the server never received any connection attempts. of course I have changed the server from localhost to the actual domain name, here is what I have tried so far on the client side :
s = SocketIo.establishSocketConnection("ws://example.com:5000");
s = SocketIo.establishSocketConnection("https://example.com:5000");
s = SocketIo.establishSocketConnection("http://example.com:5000");
s = SocketIo.establishSocketConnection("ws://example.com");
s = SocketIo.establishSocketConnection("https://example.com");
none of them worked.
I read somewhere that socketIo server host parameter default is localhost so I've tried to change that to the website domain.
s = SocketIo.establishSocketConnection("http://example.com");
if __name__ == '__main__':
socketio.run(app,host='https://example.com',debug=True,port=5000)
unfortunately, it didn't work either.
there's nothing useful on the server console (online or locally), it just doesn't connect when something goes wrong.
I got a gut feeling that could be due to some certification/headers, but I can't be sure and I don't know how to add those to SocketIO, I can make WWW request from unity just fine and I get the server response everytime.
Update:
Logs on cpanel are slow for some reason ( it takes from minutes - hours ), I've logged today and I have noticed too many of this error are pileing up
[ERROR] [UID:9279][1527766] wsgiAppHandler pApp->start_response()
return NULL.
[ERROR] [UID:9279][1527766] wsgiAppHandler pApp->start_response()
return NULL.
UPDATE:
I ended up making my own Logging system, here is what I found.
2022-04-12 13:50:02,570:WARNING:foobar: File "/home//virtualenv/ppy/3.8/lib/python3.8/site-packages/flask/app.py", line 2091, in call 2022-04-12 13:50:02,572:WARNING:foobar:return self.wsgi_app(environ, start_response) 2022-04-12 13:50:02,572:WARNING:foobar: File "/home//virtualenv/ppy/3.8/lib/python3.8/site-packages/flask_socketio/init.py", line 43, in call 2022-04-12 13:50:02,572:WARNING:foobar:return super(_SocketIOMiddleware, self).call(environ, 2022-04-12 13:50:02,572:WARNING:foobar: File "/home//virtualenv/ppy/3.8/lib/python3.8/site-packages/engineio/middleware.py", line 63, in call 2022-04-12 13:50:02,573:WARNING:foobar:return self.engineio_app.handle_request(environ, start_response) 2022-04-12 13:50:02,573:WARNING:foobar: File "/home//virtualenv/ppy/3.8/lib/python3.8/site-packages/socketio/server.py", line 597, in handle_request 2022-04-12 13:50:02,574:WARNING:foobar:return self.eio.handle_request(environ, start_response) 2022-04-12 13:50:02,574:WARNING:foobar: File "/home//virtualenv/ppy/3.8/lib/python3.8/site-packages/engineio/server.py", line 398, in handle_request 2022-04-12 13:50:02,575:WARNING:foobar:r = self._handle_connect(environ, start_response, 2022-04-12 13:50:02,575:WARNING:foobar: File "/home//virtualenv/ppy/3.8/lib/python3.8/site-packages/engineio/server.py", line 573, in _handle_connect 2022-04-12 13:50:02,576:WARNING:foobar:ret = s.handle_get_request(environ, start_response) 2022-04-12 13:50:02,576:WARNING:foobar: File "/home//virtualenv/ppy/3.8/lib/python3.8/site-packages/engineio/socket.py", line 103, in handle_get_request
2022-04-12 13:50:02,576:WARNING:foobar:return getattr(self, 'upgrade' + transport)(environ, 2022-04-12 13:50:02,576:WARNING:foobar: File "/home//virtualenv/ppy/3.8/lib/python3.8/site-packages/engineio/socket.py", line 158, in _upgrade_websocket
2022-04-12 13:50:02,577:WARNING:foobar:return ws(environ, start_response) 2022-04-12 13:50:02,577:WARNING:foobar: File "/home//virtualenv/ppy/3.8/lib/python3.8/site-packages/engineio/async_drivers/eventlet.py", line 16, in call
2022-04-12 13:50:02,578:WARNING:foobar:raise RuntimeError('You need to use the eventlet server. ' 2022-04-12 13:50:02,578:WARNING:foobar:RuntimeError 2022-04-12 13:50:02,578:WARNING:foobar:You need to use the eventlet server. See the Deployment section of the documentation for more information. 2022-04-12 13:50:02,578:WARNING:foobar:<wsgi.LoggerWriter object at 0x2b77bd4eb3a0>
it seems to me that eventlet is not implemented correctly for some reason, I found online that socketIO.run() would look for eventLet or gevent and if any exists, it would use them instead, but that is not what is happening here appearntly.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
