Created
December 25, 2013 14:32
-
-
Save flowersinthesand/8123673 to your computer and use it in GitHub Desktop.
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.path | |
current_dir = os.path.dirname(os.path.abspath(__file__)) | |
import cherrypy | |
import simplejson | |
import time | |
def RPC_handler(*args, **kwargs): | |
cherrypy.response.headers['Content-Type'] = 'text/javascript' | |
#jsonp | |
cherrypy.log.error("qs is %s"%str(kwargs)) | |
data={ | |
'type': 'message', | |
'data': None, | |
'reply': False | |
} | |
i=0 | |
while True: | |
data['data']=i | |
json_data=simplejson.dumps(data) | |
yield("data: " + json_data + "\n\n") | |
i=i+1 | |
time.sleep(1) | |
cherrypy.log.error("json: %s"%json_data) | |
class Root(): | |
@cherrypy.expose | |
def index(self): | |
return """<html> | |
<head> | |
<title>CherryPy static example</title> | |
<link rel="stylesheet" type="text/css" href="css/style.css" type="text/css"></link> | |
<script type="application/javascript" src="scripts/jquery.js"></script> | |
<script type="application/javascript" src="scripts/portal.js"></script> | |
</head> | |
<html> | |
<body> | |
<p>Static example</p> | |
<div id="txt_message">???</div> | |
<script type="text/javascript"> | |
portal.open("rpc", {transports: ["streamxhr"]}).on({ | |
// Pseudo event | |
connecting: function(data) { | |
$('#txt_message').append("connecting<br/>"); | |
}, // The selected transport starts connecting to the server | |
waiting: function(delay, attempts) {}, // The socket waits out the reconnection delay | |
// Network event | |
open: function() { | |
$('#txt_message').append("open<br/>"); | |
}, // The connection is established successfully and communication is possible | |
close: function(reason) { | |
$('#txt_message').append("close<br/>"); | |
}, // The connection has been closed or could not be opened | |
// Message event | |
message: function(data) { | |
$('#txt_message').append(data + "<br/>"); | |
}, // Receive an event whose name is message sent by the server | |
event: function(data) { | |
$('#txt_message').append("event<br/>"); | |
} // Receive an event whose name is event sent by the server | |
}) | |
.send("greeting", "Hi"); // Send an event whose name is 'greeting' and data is 'Hi' to the server | |
</script> | |
</body> | |
</html>""" | |
@cherrypy.expose | |
def rpc(self,*args, **kwargs): | |
return RPC_handler(*args, **kwargs) | |
rpc._cp_config = {'response.stream': True} | |
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.path | |
current_dir = os.path.dirname(os.path.abspath(__file__)) | |
import cherrypy | |
import simplejson | |
import time | |
def RPC_handler(*args, **kwargs): | |
cherrypy.response.headers['Content-Type'] = 'text/javascript' | |
#jsonp | |
cherrypy.log.error("qs is %s"%str(kwargs)) | |
data={ | |
'type': 'message', | |
'data': None, | |
'id': kwargs['id'], | |
'reply': False | |
} | |
i=0 | |
while True: | |
data['data']=i | |
json_data=simplejson.dumps(data) | |
yield(json_data) | |
i=i+1 | |
time.sleep(1) | |
cherrypy.log.error("json: %s"%json_data) | |
class Root(): | |
@cherrypy.expose | |
def index(self): | |
return """<html> | |
<head> | |
<title>CherryPy static example</title> | |
<link rel="stylesheet" type="text/css" href="css/style.css" type="text/css"></link> | |
<script type="application/javascript" src="scripts/jquery.js"></script> | |
<script type="application/javascript" src="scripts/portal.js"></script> | |
</head> | |
<html> | |
<body> | |
<p>Static example</p> | |
<div id="txt_message">???</div> | |
<script type="text/javascript"> | |
portal.open("rpc").on({ | |
// Pseudo event | |
connecting: function(data) { | |
$('#txt_message').append("connecting<br/>"); | |
}, // The selected transport starts connecting to the server | |
waiting: function(delay, attempts) {}, // The socket waits out the reconnection delay | |
// Network event | |
open: function() { | |
$('#txt_message').append("open<br/>"); | |
}, // The connection is established successfully and communication is possible | |
close: function(reason) { | |
$('#txt_message').append("close<br/>"); | |
}, // The connection has been closed or could not be opened | |
// Message event | |
message: function(data) { | |
$('#txt_message').append("message<br/>"); | |
}, // Receive an event whose name is message sent by the server | |
event: function(data) { | |
$('#txt_message').append("event<br/>"); | |
} // Receive an event whose name is event sent by the server | |
}) | |
.send("greeting", "Hi"); // Send an event whose name is 'greeting' and data is 'Hi' to the server | |
</script> | |
</body> | |
</html>""" | |
@cherrypy.expose | |
def rpc(self,*args, **kwargs): | |
return RPC_handler(*args, **kwargs) | |
rpc._cp_config = {'response.stream': True} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment