gluon/contrib/comet_messaging.py -> gluon/contrib/websocket_messaging.py
This commit is contained in:
@@ -1 +1 @@
|
|||||||
Version 2.0.9 (2012-09-24 15:54:52) stable
|
Version 2.0.9 (2012-09-24 16:21:05) stable
|
||||||
|
|||||||
@@ -155,7 +155,7 @@ function web2py_component(action, target, timeout, times){
|
|||||||
web2py_ajax_page('get', action, null, target);
|
web2py_ajax_page('get', action, null, target);
|
||||||
} }); }
|
} }); }
|
||||||
|
|
||||||
function web2py_comet(url,onmessage,onopen,onclose) {
|
function web2py_websocket(url,onmessage,onopen,onclose) {
|
||||||
if ("WebSocket" in window) {
|
if ("WebSocket" in window) {
|
||||||
var ws = new WebSocket(url);
|
var ws = new WebSocket(url);
|
||||||
ws.onopen = onopen?onopen:(function(){});
|
ws.onopen = onopen?onopen:(function(){});
|
||||||
|
|||||||
@@ -12,18 +12,18 @@ Attention: Requires Chrome or Safari. For IE of Firefox you need https://github.
|
|||||||
|
|
||||||
2) start this app:
|
2) start this app:
|
||||||
|
|
||||||
python gluon/contrib/comet_messaging.py -k mykey -p 8888
|
python gluon/contrib/websocket_messaging.py -k mykey -p 8888
|
||||||
|
|
||||||
3) from any web2py app you can post messages with
|
3) from any web2py app you can post messages with
|
||||||
|
|
||||||
from gluon.contrib.comet_messaging import comet_send
|
from gluon.contrib.websocket_messaging import websocket_send
|
||||||
comet_send('http://127.0.0.1:8888','Hello World','mykey','mygroup')
|
websocket_send('http://127.0.0.1:8888','Hello World','mykey','mygroup')
|
||||||
|
|
||||||
4) from any template you can receive them with
|
4) from any template you can receive them with
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
$(document).ready(function(){
|
$(document).ready(function(){
|
||||||
if(!web2py_comet('ws://127.0.0.1:8888/realtime/mygroup',function(e){alert(e.data)}))
|
if(!web2py_websocket('ws://127.0.0.1:8888/realtime/mygroup',function(e){alert(e.data)}))
|
||||||
alert("html5 websocket not supported by your browser, try Google Chrome");
|
alert("html5 websocket not supported by your browser, try Google Chrome");
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
@@ -34,14 +34,14 @@ Or if you want to send json messages and store evaluated json in a var called da
|
|||||||
<script>
|
<script>
|
||||||
$(document).ready(function(){
|
$(document).ready(function(){
|
||||||
var data;
|
var data;
|
||||||
web2py_comet('ws://127.0.0.1:8888/realtime/mygroup',function(e){data=eval('('+e.data+')')});
|
web2py_websocket('ws://127.0.0.1:8888/realtime/mygroup',function(e){data=eval('('+e.data+')')});
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
- All communications between web2py and comet_messaging will be digitally signed with hmac.
|
- All communications between web2py and websocket_messaging will be digitally signed with hmac.
|
||||||
- All validation is handled on the web2py side and there is no need to modify comet_messaging.py
|
- All validation is handled on the web2py side and there is no need to modify websocket_messaging.py
|
||||||
- Multiple web2py instances can talk with one or more comet_messaging servers.
|
- Multiple web2py instances can talk with one or more websocket_messaging servers.
|
||||||
- "ws://127.0.0.1:8888/realtime/" must be contain the IP of the comet_messaging server.
|
- "ws://127.0.0.1:8888/realtime/" must be contain the IP of the websocket_messaging server.
|
||||||
- Via group='mygroup' name you can support multiple groups of clients (think of many chat-rooms)
|
- Via group='mygroup' name you can support multiple groups of clients (think of many chat-rooms)
|
||||||
|
|
||||||
Here is a complete sample web2py action:
|
Here is a complete sample web2py action:
|
||||||
@@ -51,7 +51,7 @@ Here is a complete sample web2py action:
|
|||||||
script=SCRIPT('''
|
script=SCRIPT('''
|
||||||
jQuery(document).ready(function(){
|
jQuery(document).ready(function(){
|
||||||
var callback=function(e){alert(e.data)};
|
var callback=function(e){alert(e.data)};
|
||||||
if(!web2py_comet('ws://127.0.0.1:8888/realtime/mygroup',callback))
|
if(!web2py_websocket('ws://127.0.0.1:8888/realtime/mygroup',callback))
|
||||||
alert("html5 websocket not supported by your browser, try Google Chrome");
|
alert("html5 websocket not supported by your browser, try Google Chrome");
|
||||||
});
|
});
|
||||||
''')
|
''')
|
||||||
@@ -60,8 +60,8 @@ Here is a complete sample web2py action:
|
|||||||
def ajax_form():
|
def ajax_form():
|
||||||
form=SQLFORM.factory(Field('message'))
|
form=SQLFORM.factory(Field('message'))
|
||||||
if form.accepts(request,session):
|
if form.accepts(request,session):
|
||||||
from gluon.contrib.comet_messaging import comet_send
|
from gluon.contrib.websocket_messaging import websocket_send
|
||||||
comet_send('http://127.0.0.1:8888',form.vars.message,'mykey','mygroup')
|
websocket_send('http://127.0.0.1:8888',form.vars.message,'mykey','mygroup')
|
||||||
return form
|
return form
|
||||||
|
|
||||||
Acknowledgements:
|
Acknowledgements:
|
||||||
@@ -83,7 +83,7 @@ listeners = {}
|
|||||||
names = {}
|
names = {}
|
||||||
tokens = {}
|
tokens = {}
|
||||||
|
|
||||||
def comet_send(url,message,hmac_key=None,group='default'):
|
def websocket_send(url,message,hmac_key=None,group='default'):
|
||||||
sig = hmac_key and hmac.new(hmac_key,message).hexdigest() or ''
|
sig = hmac_key and hmac.new(hmac_key,message).hexdigest() or ''
|
||||||
params = urllib.urlencode({'message': message, 'signature': sig, 'group':group})
|
params = urllib.urlencode({'message': message, 'signature': sig, 'group':group})
|
||||||
f = urllib.urlopen(url, params)
|
f = urllib.urlopen(url, params)
|
||||||
Reference in New Issue
Block a user