Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first commit

  • Loading branch information...
commit dcee95ef91f2606c570e5334f65abd15d51773d7 0 parents
@samalba authored
3  README
@@ -0,0 +1,3 @@
+git clone git://github.com/samalba/geventwebsocket-on-dotcloud.git
+dotcloud create testwebsocket
+dotcloud push testwebsocket geventwebsocket-on-dotcloud
20 app.py
@@ -0,0 +1,20 @@
+
+import json
+from flask import Flask, request, render_template
+
+
+app = Flask(__name__)
+app.debug = True
+
+@app.route('/')
+def index():
+ return render_template('index.html')
+
+@app.route('/api')
+def api():
+ if request.environ.get('wsgi.websocket'):
+ ws = request.environ['wsgi.websocket']
+ while True:
+ message = ws.receive()
+ ws.send(message)
+ return
8 builder
@@ -0,0 +1,8 @@
+#!/bin/sh
+
+(cd $HOME ; [ ! -d env ] && virtualenv env)
+
+. $HOME/env/bin/activate
+
+pip install -r requirements.txt
+cp -Rf * $HOME/
9 dotcloud.yml
@@ -0,0 +1,9 @@
+www:
+ type: custom
+ buildscript: builder
+ processes:
+ app: /home/dotcloud/env/bin/gunicorn -k gunicornworker.GeventWebSocketWorker -b 0.0.0.0:$PORT_WWW app:app
+ ports:
+ www: http
+ systempackages:
+ - libevent-dev
6 gunicornworker.py
@@ -0,0 +1,6 @@
+from geventwebsocket.handler import WebSocketHandler
+from gunicorn.workers.ggevent import GeventPyWSGIWorker
+
+
+class GeventWebSocketWorker(GeventPyWSGIWorker):
+ wsgi_handler = WebSocketHandler
3  requirements.txt
@@ -0,0 +1,3 @@
+gunicorn
+flask
+gevent-websocket
32 templates/index.html
@@ -0,0 +1,32 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+ <title>Flask/Gevent WebSocket Test</title>
+ <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
+ <script type="text/javascript" charset="utf-8">
+ $(document).ready(function(){
+ $('form').submit(function(event){
+ ws.send($('#data').val())
+ return false;
+ });
+ if ("WebSocket" in window) {
+ ws = new WebSocket("ws://" + document.location.host + "/api");
+ ws.onmessage = function (msg) {
+ $("#log").append("<p>"+msg.data+"</p>")
+ };
+ } else {
+ alert("WebSocket not supported");
+ }
+ });
+ </script>
+</head>
+<body>
+ <h1>Send:</h1>
+ <form method='POST' action='#'>
+ <textarea name='data' id="data"></textarea>
+ <div><input type='submit'></div>
+ </form>
+ <h1>Receive:</h1>
+ <div id="log"></div>
+</body>
+</html>
Please sign in to comment.
Something went wrong with that request. Please try again.