Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking moves w/ websockets and python / django ( / twisted? )
    text
    copied!<p>The fun part of websockets is sending essentially unsolicited content from the server to the browser right?</p> <p>Well, I'm using django-websocket by Gregor Müllegger. It's a really wonderful early crack at making websockets work in Django.</p> <p>I have accomplished "hello world." The way this works is: when a request is a websocket, an object, websocket, is appended to the request object. Thus, I can, in the view interpreting the websocket, do something like:</p> <pre><code>request.websocket.send('We are the knights who say ni!') </code></pre> <p>That works fine. I get the message back in the browser like a charm.</p> <p>But what if I want to do that without issuing a request from the browser at all?</p> <p>OK, so first I save the websocket in the session dictionary:</p> <pre><code>request.session['websocket'] = request.websocket </code></pre> <p>Then, in a shell, I go and grab the session by session key. Sure enough, there's a websocket object in the session dictionary. Happy!</p> <p>However, when I try to do:</p> <pre><code>&gt;&gt;&gt; session.get_decoded()['websocket'].send('With a herring!') </code></pre> <p>I get:</p> <pre><code>Traceback (most recent call last): File "&lt;console&gt;", line 1, in &lt;module&gt; error: [Errno 9] Bad file descriptor </code></pre> <p>Sad. :-(</p> <p>OK, so I don't know much of anything about sockets, but I know enough to sniff around in a debugger, and lo and behold, I see that the socket in my debugger (which is tied to the genuine websocket from the request) has fd=6, while the one that I grabbed from the session-saved websocket has fd=-1.</p> <p>Can a socket-oriented person help me sort this stuff out?</p>
 

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