Skip to content
This repository has been archived by the owner on Apr 29, 2022. It is now read-only.

Serving only WS: disconnected immediately #44

Open
mariotti opened this issue Feb 28, 2017 · 4 comments
Open

Serving only WS: disconnected immediately #44

mariotti opened this issue Feb 28, 2017 · 4 comments

Comments

@mariotti
Copy link

mariotti commented Feb 28, 2017

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
Copy link
Contributor

o3bvv commented Mar 1, 2017

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

Respect time of other people. Thanks

@mariotti
Copy link
Author

mariotti commented Mar 1, 2017

@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
Copy link
Author

mariotti commented Mar 9, 2017

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
Copy link
Author

mariotti commented Mar 9, 2017

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.
"

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

2 participants