Created
April 6, 2015 14:07
-
-
Save Siedrix/4471a8cf859156b803a0 to your computer and use it in GitHub Desktop.
Tutum websocket examples
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
var WebSocket = require('ws'); | |
var userPublicToken = 'apikey'; | |
var username = 'username'; | |
var ws = new WebSocket('wss://stream.tutum.co/v1/events?token='+ userPublicToken +'&user=' +username); | |
ws.on('open', function() { | |
console.log('Connected'); | |
}); | |
ws.on('error', function(message) { | |
console.log('Error: %s', message); | |
}); | |
ws.on('message', function(message) { | |
console.log('received: %s', message); | |
}); | |
ws.on('close', function() { | |
console.log('Socket closed', arguments); | |
}); |
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 websocket | |
import json | |
def on_error(ws, error): | |
print error | |
def on_close(ws): | |
print "### closed ###" | |
def on_message(ws, message): | |
msg_as_JSON = json.loads(message) | |
type = msg_as_JSON.get("type") | |
if type: | |
if type == "auth": | |
print("Auth completed") | |
elif type != "user-notifications": | |
print("{}:{}:{}".format(type, msg_as_JSON.get("state"), msg_as_JSON.get("resource_uri"))) | |
def on_open(ws): | |
print "Connected" | |
if __name__ == "__main__": | |
websocket.enableTrace(True) | |
token = 'apikey'; | |
username = 'username'; | |
ws = websocket.WebSocketApp('wss://stream.tutum.co/v1/events?token={}&user={}'.format(token, username), | |
on_message = on_message, | |
on_error = on_error, | |
on_close = on_close, | |
on_open = on_open) | |
ws.run_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment