Last active
September 7, 2015 03:10
-
-
Save ryanchang/9a8ee4eafb79b4cda4eb to your computer and use it in GitHub Desktop.
Python client for websocket_rails.
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 thread | |
import time | |
import json | |
def on_message(ws, message): | |
print "recieved: " + message | |
response = json.loads(message)[0] | |
event = response[0] | |
print "event: " + event | |
if event == 'websocket_rails.channel_token': | |
channel_token = response[1]["data"]["token"] | |
print "channel_token: " + channel_token | |
#TODO: handle ping request here. | |
def on_error(ws, error): | |
print error | |
def on_close(ws): | |
print "### closed ###" | |
def on_open(ws): | |
def run(*args): | |
time.sleep(1) | |
#subscribe("TestDev") | |
channel = "TestDev" | |
ws.send('["websocket_rails.subscribe", {"data": {"channel": "%s"}}]' % channel) | |
ws.send('["device.event", {"data": {"id": 1, "weight": 70.0, "temperature": 30.0, "humidity": 60.0}}]') | |
thread.start_new_thread(run, ()) | |
def subscribe(channel): | |
ws.send('["websocket_rails.subscribe", {"channel": "%s"}]' % channel) | |
if __name__ == "__main__": | |
websocket.enableTrace(True) | |
ws = websocket.WebSocketApp("ws://civilizationdata.com:7000/websocket", | |
on_message = on_message, | |
on_error = on_error, | |
on_close = on_close) | |
ws.on_open = on_open | |
ws.run_forever() |
Author
ryanchang
commented
Sep 3, 2015
- Client -> Server without channel
- Server -> Client without channel
- Client subscribe to channel
- Server send channel token to client
- Client send to a specific channel
- Ping
- Pong
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment