How to go about using okhttp3 to connect to a flask-sockets backend

139 views Asked by At

I'm fairly confident that this is a new question, which is a little troubling, but if this has been addressed before please let me know. I also am just beginning with websockets. I know the difference between socket.io and normal websockets. I tried using the former for streaming jpeg data however it wasn't up to the task (I am looking for 30 fps and this would have huge lag spikes after keeping connection for less than a minute) I found this article so I didn't proceed any further with that. https://github.com/socketio/socket.io/issues/1175

https://github.com/TwilioDevEd/mediastreams-consume-websockets-flask/blob/master/app.py taught me how to set up the flask-sockets backend and https://medium.com/@ssaurel/learn-to-use-websockets-on-android-with-okhttp-ba5f00aea988 taught me the okhttp3. Also note the only reason i use okhttp3 is because i know of it, and assumed that websockets would work out of the box with client/server but that isn't the case.

Here is the print out of the onFailure listener on okhttp3 client.

failed to connect: Failed to connect /localip:5000

And on the server side i can report that no connection is being made.

Here is my code on the backend.(intending on sending jpeg frames through websocket hence camera Object)

HTTP_SERVER_PORT = 5000
camera = Camera(app, './get.py', 960, 720)
socket = SocketLocal(app, camera)
thread = None

@sockets.route('/data')
def echo(ws):
    app.logger.info("Connected accepted")
    has_seen_media = False
    #send through to client
    while not ws.closed:
        gevent.sleep(0.1)
        try:
            data = gevent.with_timeout(0.1, ws.receive, timeout_value="")
            if data is None:
                raise Exception("socket closed")
        except:
            break
        message = ws.receive()
        if message is None:
            app.logger.info("No message received...")
            continue

        data = loads("message")
        if data['event'] == "connected":
            app.logger.info("Connected Message Received: {}".format(message))
        if data['event'] == "start":
            app.logger.info("Start Message received: {}".format(message))
        if data['event'] == "media":
            global socket
            global thread

            socket.ws = ws
            thread = threading.Thread(target=socket._camerathread)
            thread.start()
        if data['event'] == 'closed':
            app.logger.info("Closed Message received: {}".format(message))
            break

    thread = None
    app.logger.info("Connection closed")

if __name__== "__main__":
    #app.run(host='0.0.0.0', threaded=True)
    config['thermal']['url'] = url_for('video_feed')
    from gevent import pywsgi
    from geventwebsocket.handler import WebSocketHandler

    server = pywsgi.WSGIServer(('', HTTP_SERVER_PORT), app, handler_class=WebSocketHandler)
    server.serve_forever()

I'm running the flask backend through gunicorn. the command i use is straight from flask-sockets page.

gunicorn -k flask_sockets.worker --access-logfile gunicorn_access.log --error-logfile gunicorn_error.log --log-level=debug wsgi:application

if there's a better way to do this then i'm all ears.

0

There are 0 answers