Created
May 31, 2017 15:31
-
-
Save kcowgill/b823953bfc74b80057afc3d3ca786aed 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
# DDPClient from here: https://github.com/hharnisc/python-ddp | |
from DDPClient import DDPClient | |
import hashlib | |
import time | |
# these are callbacks | |
def show_data(data, *more): | |
print("Data received: '{}':'{}'".format(data, more)) | |
def received_message(data): | |
print("MESSAGE received: '{}'".format(data)) | |
def connected(): | |
print('* Hey there good lookin, whatcha got cookin\'?') | |
def closed(code, reason): | |
print('* CONNECTION CLOSED {}:{}'.format(code,reason)) | |
def failed(collection, data): | |
print('* FAILED - data: {}'.format(str(data))) | |
def subscribe(data, more): | |
print('* subscribe data: {}:{}'.format(str(data),more)) | |
server_url = 'ws://your.server.here:3000/websocket' | |
client = DDPClient(server_url, auto_reconnect=True, auto_reconnect_timeout=.5, debug=True) | |
client.on('connected', connected) | |
client.on('reconnected', connected) | |
client.on('socket_closed', closed) | |
client.on('failed', failed) | |
client.connect() | |
# I don't yet know how to look these up programatically | |
username = "username" | |
user_id = "SomeUserID" | |
room_id = "SomeRoomID" | |
passw = "SomePassword" | |
hashedPass = hashlib.sha256(passw.encode('utf-8')) | |
hashDigest = hashedPass.hexdigest() | |
ret = client.call('login', [{ "user": {"username": username}, | |
"password": { "digest": hashDigest, "algorithm":"sha-256" } }], show_data) | |
# this lets your program receive direct messages | |
client.subscribe('stream-notify-user', ["{}/notification".format(user_id), False], subscribe) | |
# this lets your program receive messages from any room it's in | |
client.subscribe('stream-room-messages', [room_id, False], subscribe) | |
client.call('joinRoom', [room_id], show_data | |
client.call('sendMessage', [{"rid":room_id, "msg":'hi there!'}], show_data) | |
while True: | |
time.sleep(2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment