Created
February 27, 2013 12:46
-
-
Save jpmens/5047669 to your computer and use it in GitHub Desktop.
Experiment with Websockets, MQTT (Mosquitto)
uses PyWebsocket
This file contains hidden or 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 mosquitto | |
def on_connect(mosq, userdata, rc): | |
userdata._logger.debug("***CONN "+str(rc)) | |
def on_subscribe(mosq, userdata, mid, granted_qos): | |
userdata._logger.debug("Subscribed: "+str(mid)+" "+str(granted_qos)) | |
def on_message(mosq, userdata, msg): | |
# print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload)) | |
userdata._logger.debug('+++++ MQTT: %s' % str(msg.payload)) | |
userdata.ws_stream.send_message(str(msg.payload), binary=False) | |
def web_socket_do_extra_handshake(request): | |
# This example handler accepts any request. See origin_check_wsh.py for how | |
# to reject access from untrusted scripts based on origin value. | |
pass # Always accept. | |
def web_socket_transfer_data(request): | |
mqttc = mosquitto.Mosquitto('ws-cli1', clean_session=True, userdata=request) | |
mqttc.on_connect = on_connect | |
mqttc.on_message = on_message | |
mqttc.on_subscribe = on_subscribe | |
mqttc.connect("hippo", 1883, 60) | |
mqttc.subscribe("twitter/#", 0) | |
while True: | |
rc = mqttc.loop() |
This file contains hidden or 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>WS demo</title> | |
<script src="jquery-1.3.2.min.js"></script> | |
<script> | |
$(document).ready(function(){ | |
var ws; | |
if ("WebSocket" in window) { | |
ws = new WebSocket("ws://localhost:12345/jp"); | |
ws.onopen = function() { | |
// ws.send("tesht"); | |
}; | |
ws.onmessage = function (evt) { | |
var data = evt.data; | |
$("#tweet").html(data); | |
}; | |
ws.onclose = function() { | |
// | |
}; | |
} else { | |
alert("You have no web sockets"); | |
}; | |
}); | |
</script> | |
</head> | |
<body> | |
<h1>WStweetsch</h1> | |
<fieldset> | |
<legend>Status</legend> | |
<div id="tweet">...</div> | |
</fieldset> | |
</body> | |
</html> |
This file contains hidden or 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
python mod_pywebsocket/standalone.py -p 12345 --allow-draft75 -d example --log-level debug |
This file contains hidden or 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
PyWebsocket http://code.google.com/p/pywebsocket/ | |
HTML from http://pedroassuncao.com/blog/2009/12/18/websockets-tutorialexample-with-pywebsocket/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment