heroku-python / flask-sockets

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

WebSocketError if I don't use try, except #17

Closed mmcclellan closed 8 years ago

mmcclellan commented 10 years ago

If I change the example code to this:

while True:
    try:
        message = ws.receive()
        ws.send("Your message was: %r" % message)
    except:
        break

All works well. Otherwise I get WebSocketErrors when the client disconnects. Not sure if others have experienced this.

ssfrr commented 10 years ago

If you want to be a little more discriminating in your exception catching you can do:

from geventwebsocket import WebSocketError

while True:
    try:
        ws.send("some data")
    except WebSocketError as e:
        print('Caught WebSocketError: %s' % e)
        break
kennethreitz commented 8 years ago

It's best to do while not ws.closed.

josephernest commented 7 years ago

@kennethreitz while not ws.closed isn't enough in some situations. try: ... except WebSocketError: still helps.