heroku-python / flask-sockets

[DEPRECATED] Alternative: https://github.com/miguelgrinberg/flask-sock
MIT License
1.74k stars 167 forks source link

Serving only WS: disconnected immediately #44

Open mariotti opened 7 years ago

mariotti commented 7 years ago

I am trying to serve on websocket only websockets. The code looks like this:

from flask import Flask, render_template, Response
from flask_sockets import Sockets

app = Flask(__name__)
sockets = Sockets(app)

@sockets.route('/wscrd')
def echo_crd(ws):
    print "HERE!"
    vcamera = VideoCamera()
    pframes = ProcFrames(vcamera)
    while True:
        message = "UP"
        ws.send(message)
        ws.send("11")
        yield True

if __name__ == '__main__':
    from gevent import pywsgi
    from geventwebsocket.handler import WebSocketHandler

    print "HERE! SERVER!"
    #app.run(host='0.0.0.0', debug=True)

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

Or very close.

The js code gets connected and immediately disconnected. The /wscrd route is never hit.

The js code is very plain. Please note that the content of the loop while True: had many different versions... a kind of trial and error .. this is just one. But it never gets called anyway.

I add the js code for refs:

$(document).ready(function(){

var WEBSOCKET_ROUTE_CRD = "/wscrd";
var wscrd = new WebSocket("ws://" + "127.0.0.1:5000" + WEBSOCKET_ROUTE_CRD);

    wscrd.onopen = function(evt) {
        $("#wscrd-status").append("Connected");
        alert("On_open")
        };

    wscrd.onmessage = function(evt) {
        $("#wscrd-status").append("NewMessage");
        alert("On_message")
        };

    wscrd.onclose = function(evt) {
        $("#wscrd-status").append("Disconnected");
        alert("On_close")
        };

    wscrd.onerror = function(evt) {
        alert("On_error")
        };
};

On the js client side I see the 2 alert: "On_open" and "On_close".

o3bvv commented 7 years ago

@mariotti please, format your snippet and add invocation example.

Respect time of other people. Thanks

mariotti commented 7 years ago

@oblalex I am very sorry, formatted! I was trying but got troubles and had to leave the keyboard.

Came to my mind a possible problem, does the client need to start the exchange? Like sending a first message like "ready to receive".

mariotti commented 7 years ago

Solved part of the problem, I mean part of mine ;) , but I am not sure about the actual behaviour of flask-sockets. This snippet works:

@sockets.route('/wscrd')
def echo_crd(ws):
    print "HERE!"
    vcamera = VideoCamera()
    pframes = ProcFrames(vcamera)
    while not ws.closed:
        #message = ws.receive()
        ws.send("|Start loop|")
        while True:
            xred, yred, xyellow, yyellow = pframes.get_frame_crd(ws)
            ws.send(str(xred))

But wait... the client now is sending a first message to the server, even if it doesn't get read.

Once opened a ws connection, is it possible to start with just the server sending data? I mean, even by protocol. Does websocket need to have the client starting with a first data?

This in relation to my last comment.

mariotti commented 7 years ago

From this document mozilla.org / Writing_WebSocket_servers

it seems that the server can start sending data. Quote:

" Either the client or the server can choose to send a message at any time — that's the magic of WebSockets. "