Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For a solution based on tornado/tornadio, you SocketConnection class needs to maintain a list of connections at the class level. Your on_connect handler would add the connection to this list, and on_close would remove it. For sample pseudo-code see <a href="https://groups.google.com/forum/#!msg/python-tornado/iwGlIjOFRtQ/dDnVPZPFcEsJ" rel="nofollow">this post</a> by Serge S. Koval. The code is reproduce below:</p> <p>Declare your TornadIO connection class:</p> <pre class="lang-py prettyprint-override"><code>class MyConnection(SocketConnection): participants = set() @classmethod def broadcast(cls, msg): for p in cls.participants: p.send(msg) @classmethod def controller_msg(cls, msg): cls.broadcast(msg) </code></pre> <p>In your device polling thread, do something like:</p> <pre class="lang-py prettyprint-override"><code>while True: datum = file.readline() if len(datum) &gt; 2: t = json.loads(datum) ... def callback(): MyConnection.controller_msg(t) io_loop.add_callback(callback) </code></pre> <p>Additionally, <a href="https://bitbucket.org/Jeffrey/gevent-socketio" rel="nofollow">gevent-socketio</a> has support for message broadcast, but it's based on gevent, not tornado.</p> <h2>UPDATE:</h2> <p>tornadio2 already maintains a list of active sessions, so all you need to do is:</p> <pre class="lang-py prettyprint-override"><code>class MyConnection(SocketConnection): def broadcast(self, event, message): for session_id, session in self.session.server._sessions._items.iteritems(): session.conn.emit(event, message) </code></pre> <p>This works because each connection instance has a reference to its session, which has a reference to the global router used to create the application (stored as <code>server</code>), which maintains a list of sessions in a <code>SessionContainer</code> object in <code>_sessions</code>. Now whenever you want to broadcast a message within your connection class, just do:</p> <pre class="lang-py prettyprint-override"><code>self.broadcast('my_custom_event', 'my_event_args') </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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