Created
May 24, 2015 18:50
-
-
Save drasko/cb4bf36303399dda2ed2 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
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") | |
# 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