~ubuntu-branches/ubuntu/trusty/python-eventlet/trusty-proposed

« back to all changes in this revision

Viewing changes to examples/websocket_chat.py

  • Committer: Bazaar Package Importer
  • Author(s): Soren Hansen
  • Date: 2010-09-28 21:20:32 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100928212032-c4n67olxdoqzygmt
Tags: 0.9.12-0ubuntu1
New upstream release. (FFe: LP: #645899)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import eventlet
 
2
from eventlet import wsgi
 
3
from eventlet import websocket
 
4
 
 
5
participants = set()
 
6
 
 
7
@websocket.WebSocketWSGI
 
8
def handle(ws):
 
9
    participants.add(ws)
 
10
    try:
 
11
        while True:
 
12
            m = ws.wait()
 
13
            if m is None:
 
14
                break
 
15
            for p in participants:
 
16
                p.send(m)
 
17
    finally:
 
18
        participants.remove(ws)
 
19
                  
 
20
def dispatch(environ, start_response):
 
21
    """Resolves to the web page or the websocket depending on the path."""
 
22
    if environ['PATH_INFO'] == '/chat':
 
23
        return handle(environ, start_response)
 
24
    else:
 
25
        start_response('200 OK', [('content-type', 'text/html')])
 
26
        return [open(os.path.join(
 
27
                     os.path.dirname(__file__), 
 
28
                     'websocket_chat.html')).read()]
 
29
        
 
30
if __name__ == "__main__":
 
31
    # run an example app from the command line            
 
32
    listener = eventlet.listen(('127.0.0.1', 7000))
 
33
    print "\nVisit http://localhost:7000/ in your websocket-capable browser.\n"
 
34
    wsgi.server(listener, dispatch)