Skip to content

Commit 4399957

Browse files
author
Stephen Diehl
committed
Merge pull request sdiehl#7 from Lothiraldan/master
Add an example for gevent-websocket
2 parents eb9f995 + b62ee69 commit 4399957

File tree

2 files changed

+127
-1
lines changed

2 files changed

+127
-1
lines changed

index.html

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,67 @@ <h2 id="long-polling">Long Polling</h2>
11511151
</pre>
11521152

11531153
<h2 id="websockets">Websockets</h2>
1154+
<p>Websocket example which requires <a href="https://bitbucket.org/Jeffrey/gevent-websocket/src">gevent-websocket</a>.</p>
1155+
<pre>
1156+
<code class="python"># Simple gevent-websocket server
1157+
import json
1158+
import random
1159+
1160+
from gevent import pywsgi, sleep
1161+
from geventwebsocket.handler import WebSocketHandler
1162+
1163+
class WebSocketApp(object):
1164+
'''Send random data to the websocket'''
1165+
1166+
def __call__(self, environ, start_response):
1167+
ws = environ['wsgi.websocket']
1168+
x = 0
1169+
while True:
1170+
data = json.dumps({'x': x, 'y': random.randint(1, 5)})
1171+
ws.send(data)
1172+
x += 1
1173+
sleep(0.5)
1174+
1175+
server = pywsgi.WSGIServer(("", 10000), WebSocketApp(),
1176+
handler_class=WebSocketHandler)
1177+
server.serve_forever()
1178+
</code>
1179+
</pre>
1180+
1181+
<p>HTML Page:</p>
1182+
<pre><code>&lt;html&gt;
1183+
&lt;head&gt;
1184+
&lt;title&gt;Minimal websocket application&lt;/title&gt;
1185+
&lt;script type="text/javascript" src="jquery.min.js"&gt;&lt;/script&gt;
1186+
&lt;script type="text/javascript"&gt;
1187+
$(function() {
1188+
// Open up a connection to our server
1189+
var ws = new WebSocket("ws://localhost:10000/");
1190+
1191+
// What do we do when we get a message?
1192+
ws.onmessage = function(evt) {
1193+
$("#placeholder").append('&lt;p&gt;' + evt.data + '&lt;/p&gt;')
1194+
}
1195+
// Just update our conn_status field with the connection status
1196+
ws.onopen = function(evt) {
1197+
$('#conn_status').html('&lt;b&gt;Connected&lt;/b&gt;');
1198+
}
1199+
ws.onerror = function(evt) {
1200+
$('#conn_status').html('&lt;b&gt;Error&lt;/b&gt;');
1201+
}
1202+
ws.onclose = function(evt) {
1203+
$('#conn_status').html('&lt;b&gt;Closed&lt;/b&gt;');
1204+
}
1205+
});
1206+
&lt;/script&gt;
1207+
&lt;/head&gt;
1208+
&lt;body&gt;
1209+
&lt;h1&gt;WebSocket Example&lt;/h1&gt;
1210+
&lt;div id="conn_status"&gt;Not Connected&lt;/div&gt;
1211+
&lt;div id="placeholder" style="width:600px;height:300px;"&gt;&lt;/div&gt;
1212+
&lt;/body&gt;
1213+
&lt;/html&gt;
1214+
</code></pre>
11541215
<h2 id="chat-server">Chat Server</h2>
11551216
<p>The final motivating example, a realtime chat room. This example
11561217
requires <a href="http://flask.pocoo.org/">Flask</a> ( but not neccesarily so, you could use Django,

tutorial.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,10 +903,75 @@ gevent.spawn(producer)
903903
WSGIServer(('', 8000), ajax_endpoint).serve_forever()
904904

905905
</code>
906-
</pre>
906+
</pre>
907907

908908
## Websockets
909909

910+
Websocket example which requires <a href="https://bitbucket.org/Jeffrey/gevent-websocket/src">gevent-websocket</a>.
911+
912+
913+
<pre>
914+
<code class="python"># Simple gevent-websocket server
915+
import json
916+
import random
917+
918+
from gevent import pywsgi, sleep
919+
from geventwebsocket.handler import WebSocketHandler
920+
921+
class WebSocketApp(object):
922+
'''Send random data to the websocket'''
923+
924+
def __call__(self, environ, start_response):
925+
ws = environ['wsgi.websocket']
926+
x = 0
927+
while True:
928+
data = json.dumps({'x': x, 'y': random.randint(1, 5)})
929+
ws.send(data)
930+
x += 1
931+
sleep(0.5)
932+
933+
server = pywsgi.WSGIServer(("", 10000), WebSocketApp(),
934+
handler_class=WebSocketHandler)
935+
server.serve_forever()
936+
</code>
937+
</pre>
938+
939+
HTML Page:
940+
941+
<html>
942+
<head>
943+
<title>Minimal websocket application</title>
944+
<script type="text/javascript" src="jquery.min.js"></script>
945+
<script type="text/javascript">
946+
$(function() {
947+
// Open up a connection to our server
948+
var ws = new WebSocket("ws://localhost:10000/");
949+
950+
// What do we do when we get a message?
951+
ws.onmessage = function(evt) {
952+
$("#placeholder").append('<p>' + evt.data + '</p>')
953+
}
954+
// Just update our conn_status field with the connection status
955+
ws.onopen = function(evt) {
956+
$('#conn_status').html('<b>Connected</b>');
957+
}
958+
ws.onerror = function(evt) {
959+
$('#conn_status').html('<b>Error</b>');
960+
}
961+
ws.onclose = function(evt) {
962+
$('#conn_status').html('<b>Closed</b>');
963+
}
964+
});
965+
</script>
966+
</head>
967+
<body>
968+
<h1>WebSocket Example</h1>
969+
<div id="conn_status">Not Connected</div>
970+
<div id="placeholder" style="width:600px;height:300px;"></div>
971+
</body>
972+
</html>
973+
974+
910975
## Chat Server
911976

912977
The final motivating example, a realtime chat room. This example

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy