Created
July 17, 2012 06:55
-
-
Save hekyou/3127701 to your computer and use it in GitHub Desktop.
gevent+websocket+ginkgo+Flask
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from ginkgo import Service | |
from ginkgo.async.gevent import WSGIServer | |
from geventwebsocket.handler import WebSocketHandler | |
from flask import Flask, render_template | |
class WebSocket(Service): | |
def __init__(self): | |
self.add_service(WSGIServer(('127.0.0.1', 8001), self.handle, | |
handler_class=WebSocketHandler)) | |
def handle(self, environ, start_response): | |
if environ.get('wsgi.websocket'): | |
ws = environ["wsgi.websocket"] | |
while True: | |
src = ws.receive() | |
if src is None: | |
break | |
ws.send(src) | |
class Http(Service): | |
app = Flask(__name__) | |
def __init__(self): | |
self.add_service(WSGIServer(('127.0.0.1', 8000), self.app)) | |
@app.route("/", methods=['GET']) | |
def index(): | |
return render_template('index.html') | |
class Application(Service): | |
def __init__(self): | |
self.add_service(Http()) | |
self.add_service(WebSocket()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ginkgo app.Application |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>ginkgo test</title> | |
</head> | |
<body> | |
<h1>test</h1> | |
<div> | |
<input name="send_input" id="send_input" /> | |
<input type="button" value="push" onclick="send_data()" /> | |
</div> | |
<div> | |
<textarea id="view"></textarea> | |
</div> | |
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> | |
<script type="text/javascript"> | |
var ws = new WebSocket("ws://127.0.0.1:8001/"); | |
ws.onmessage = function(e) { | |
$("#view").html($("#view").val() + e.data + "\n"); | |
}; | |
function send_data() { | |
ws.send($("#send_input").val()); | |
}; | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Flask==0.9 | |
Ginkgo==0.5.0dev | |
Jinja2==2.6 | |
Werkzeug==0.8.3 | |
distribute==0.6.24 | |
gevent==0.13.7 | |
gevent-websocket==0.3.6 | |
git-remote-helpers==0.1.0 | |
greenlet==0.4.0 | |
wsgiref==0.1.2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment