File: echo_cherrypy_server_nojquery.py

package info (click to toggle)
python-ws4py 0.4.2%2Bdfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 688 kB
  • sloc: python: 4,500; makefile: 140; javascript: 96
file content (120 lines) | stat: -rw-r--r-- 4,248 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# -*- coding: utf-8 -*-
import argparse
import random
import os

import cherrypy

from ws4py.server.cherrypyserver import WebSocketPlugin, WebSocketTool
from ws4py.websocket import WebSocket
from ws4py.messaging import TextMessage

class ChatWebSocketHandler(WebSocket):
    def received_message(self, m):
        cherrypy.engine.publish('websocket-broadcast', m)

    def closed(self, code, reason="A client left the room without a proper explanation."):
        cherrypy.engine.publish('websocket-broadcast', TextMessage(reason))

class Root(object):
    def __init__(self, host, port, ssl=False):
        self.host = host
        self.port = port
        self.scheme = 'wss' if ssl else 'ws'

    @cherrypy.expose
    def index(self):
        return """<html>
    <head>
    </head>
    <body>
    <p>Note: If viewing this as localhost via SSL and it doesn't work, try using 127.0.0.1 directly</p>
    <div>
      <textarea id='chat' cols='35' rows='10'></textarea>
      <br />
      <label for='message'>%(username)s: </label><input id='message' />
      <button type="button" id='send' >Send</button>
      </div>
           <script type='application/javascript'>
               websocket = '%(scheme)s://%(host)s:%(port)s/ws';
               if (window.WebSocket) {
                 ws = new WebSocket(websocket);
               }
               else if (window.MozWebSocket) {
                 ws = MozWebSocket(websocket);
               }
               else {
                 console.log('WebSocket Not Supported');
               }
               var c = document.getElementById('chat');

               window.onbeforeunload = function(e) {
                 c.value=c.value + 'Bye bye...\\n';
                 ws.close(1000, '%(username)s left the room');

                 if(!e) e = window.event;
                 e.stopPropagation();
                 e.preventDefault();
               };
               ws.onmessage = function (evt) {
                      c.value=c.value + evt.data + '\\n';
               };
               ws.onopen = function() {
                  ws.send("%(username)s entered the room");
               };
               ws.onclose = function(evt) {
                      c.value=c.value + 'Connection closed by server: ' + evt.code + ' \"' + evt.reason + '\"\\n';
               };

              document.getElementById('send').onclick = function() {
                  console.log(document.getElementById('message').value);
                  ws.send("%(username)s: " +document.getElementById('message').value);
                   document.getElementById('message').value ="";
                  return false;
               };


           </script>
    </body>
    </html>
    """ % {'username': "User%d" % random.randint(0, 100), 'host': self.host, 'port': self.port, 'scheme': self.scheme}

    @cherrypy.expose
    def ws(self):
        cherrypy.log("Handler created: %s" % repr(cherrypy.request.ws_handler))

if __name__ == '__main__':
    import logging
    from ws4py import configure_logger
    configure_logger(level=logging.DEBUG)

    parser = argparse.ArgumentParser(description='Echo CherryPy Server')
    parser.add_argument('--host', default='127.0.0.1')
    parser.add_argument('-p', '--port', default=9000, type=int)
    parser.add_argument('--ssl', action='store_true')
    args = parser.parse_args()

    cherrypy.config.update({'server.socket_host': args.host,
                            'server.socket_port': args.port,
                            'tools.staticdir.root': os.path.abspath(os.path.join(os.path.dirname(__file__), 'static'))})

    if args.ssl:
        cherrypy.config.update({
        'server.ssl_module':'builtin',
'server.ssl_certificate': 'server.crt',
                                'server.ssl_private_key': 'server.key'})

    WebSocketPlugin(cherrypy.engine).subscribe()
    cherrypy.tools.websocket = WebSocketTool()

    cherrypy.quickstart(Root(args.host, args.port, args.ssl), '', config={
        '/ws': {
            'tools.websocket.on': True,
            'tools.websocket.handler_cls': ChatWebSocketHandler
            },
        '/js': {
              'tools.staticdir.on': True,
              'tools.staticdir.dir': 'js'
            }
        }
    )