Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to force Tornado websockets to generate error page on handshake
    text
    copied!<p>I am writing a small app where I use Tornado websockets. I use cookies for authentication, and I therefore want to check this when establishing the web socket.</p> <p>So here is the challenge (building on the official websockets example):</p> <pre><code>class EchoWebSocket(tornado.websocket.WebSocketHandler): def open(self): userx = self.get_secure_cookie("user") if not userx: self.send_error(403) #I want to redirect or otherwise stop the user print "WebSocket opened" def on_message(self, message): userx = self.get_secure_cookie("user") self.write_message(u"%s said: %s" % (userx, message)) def on_close(self): print "WebSocket closed" </code></pre> <p>Turns out that this is not very popular with Tornado, throwing this error:</p> <pre><code>ERROR:root:Exception in callback &lt;tornado.stack_context._StackContextWrapper object at 0x10fa10db8&gt; Traceback (most recent call last): .... File "/Library/Python/2.7/site-packages/tornado-2.4-py2.7.egg/tornado/iostream.py", line 565, in _check_closed raise IOError("Stream is closed") IOError: Stream is closed </code></pre> <p>Is it even possible to "authenticate or cancel" during websockets handshake? Is this a problem with my code, or is there a issue/missing feature with Tornado? Is Tornado capable of sending some error condition over websockets to trigger the onerror handler at the client side in this situation (can not find any way to force an error condition in the manual)?</p> <p>The sequence of events I am trying to achieve is:</p> <ol> <li><p>the user logs in using regular web requests, and a session token is stored as a secure cookie</p></li> <li><p>the user loads a page that uses websockets, javascript attempts to establish the socket</p></li> <li><p>before the socket is established I want to check the presence of a valid session token</p> <ul> <li>if valid token, the socket is established and my app works as intended</li> <li>if invalid token, there is no socket, and the user gets an error (alternatively, an websocket error condition is raised by the server)</li> </ul></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload