Skip to content

Instantly share code, notes, and snippets.

@iamruinous
Created May 24, 2015 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamruinous/595e2b06c51c4656f968 to your computer and use it in GitHub Desktop.
Save iamruinous/595e2b06c51c4656f968 to your computer and use it in GitHub Desktop.
Python Meshblu Websocket
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web
import tornado.options
import sockjs.tornado
###
# Tornado Websocket Client Class
###
class WSClient():
conn = None
keepalive = None
def __init__(self, uri):
self.uri = uri
self.connectRemote()
def connectRemote(self):
print "URI: ", self.uri
w = tornado.websocket.websocket_connect(self.uri)
w.add_done_callback(self.wsConnectionCb)
def dokeepalive(self):
stream = self.conn.protocol.stream
#if not stream.closed():
# self.keepalive = stream.io_loop.add_timeout(timedelta(seconds=PING_TIMEOUT), self.dokeepalive)
# self.conn.protocol.write_ping("")
#else:
#self.keepalive = None # should never happen
def wsConnectionCb(self, conn):
self.conn = conn.result()
#weioRunnerGlobals.remoteConnected.value = True
self.conn.on_message = self.message
#self.conn.write_message('status')
#self.keepalive = IOLoop.instance().add_timeout(timedelta(seconds=PING_TIMEOUT), self.dokeepalive)
def message(self, message):
if message is not None:
print('>> %s' % message)
else:
self.close()
def close(self):
#weioRunnerGlobals.remoteConnected.value = False
print('conn closed')
#if self.keepalive is not None:
# keepalive = self.keepalive
# self.keepalive = None
# IOLoop.instance().remove_timeout(keepalive)
#self.connectRemote()
if __name__ == "__main__":
import logging
logging.getLogger().setLevel(logging.DEBUG)
wsc = WSClient("wss://meshblu.octoblu.com:443/socket.io/?EIO=3&transport=websocket")
# Start IOLoop
tornado.ioloop.IOLoop.instance().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment