Permalink
Browse files

Added setup.py and echo server example

  • Loading branch information...
zeekay committed Sep 26, 2011
1 parent 72cdbc2 commit 191f05f65cbb97e39c82b2124d5928ecef0491ea
Showing with 78 additions and 0 deletions.
  1. +1 −0 .hgignore
  2. +20 −0 examples/echo/echo.py
  3. +46 −0 examples/echo/index.tpl
  4. +11 −0 setup.py
View
@@ -1,2 +1,3 @@
syntax: glob
*.pyc
+build/
View
@@ -0,0 +1,20 @@
+from bottle import route, run, template
+from bottle_websocket.server import GeventWebSocketServer
+from bottle_websocket.plugin import websocket
+
+@route('/')
+def index():
+ return template('index')
+
+@route('/websocket', apply=[websocket])
+def echo(ws):
+ while True:
+ msg = ws.receive(msg_obj=True)
+ if msg is not None:
+ print 'WebSocket message recieved:', msg
+ ws.send(msg.data)
+ else:
+ break
+ ws.close()
+
+run(host='127.0.0.1', port=8080, server=GeventWebSocketServer, monkey=True)
View
@@ -0,0 +1,46 @@
+<!doctype html>
+<head>
+ <meta charset="utf-8" />
+ <title>WebSocket Echo Test</title>
+
+ <style>
+ li { list-style: none; }
+ </style>
+
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
+ <script>
+ $(document).ready(function() {
+ if (!window.WebSocket) {
+ if (window.MozWebSocket) {
+ window.WebSocket = window.MozWebSocket;
+ } else {
+ $('#messages').append("<li>Your browser doesn't support WebSockets.</li>");
+ }
+ }
+ ws = new WebSocket('ws://127.0.0.1:8080/websocket');
+ ws.onopen = function(evt) {
+ $('#messages').append('<li>WebSocket connection opened.</li>');
+ }
+ ws.onmessage = function(evt) {
+ $('#messages').append('<li>' + evt.data + '</li>');
+ }
+ ws.onclose = function(evt) {
+ $('#messages').append('<li>WebSocket connection closed.</li>');
+ }
+ $('#send').submit(function() {
+ ws.send($('input:first').val());
+ $('input:first').val('').focus();
+ return false;
+ });
+ });
+ </script>
+</head>
+<body>
+ <h2>Bottle Websockets!</h2>
+ <form id="send" action='.'>
+ <input type="text" value="message" />
+ <input type="submit" value="Send" />
+ </form>
+ <div id="messages"></div>
+</body>
+</html>
View
@@ -0,0 +1,11 @@
+from distutils.core import setup
+
+setup(
+ name='bottle-websocket',
+ version='0.1',
+ author='Zach Kelling',
+ author_email='zeekayy@gmail.com',
+ packages=['bottle_websocket',],
+ description='WebSockets for bottle',
+ long_description=open('README').read(),
+)

0 comments on commit 191f05f

Please sign in to comment.