Created
December 7, 2017 03:50
-
-
Save eggman/03240ece78ea4f8363881da193f86dac to your computer and use it in GitHub Desktop.
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
# | |
# https://www.okex.com/ws_getStarted.html | |
# https://github.com/OKCoin/websocket | |
# | |
import sys | |
from datetime import datetime, timedelta, timezone | |
import sched, time | |
import json | |
import websocket | |
JST = timezone(timedelta(hours=+9), 'JST') | |
class OkexStream(): | |
endpoint = "wss://real.okex.com:10441/websocket" | |
def __init__(self): | |
#websocket.enableTrace(True) | |
self.ws = websocket.WebSocketApp( | |
OkexStream.endpoint, | |
on_message = self.on_message, | |
on_error = self.on_error, | |
on_close = self.on_close | |
) | |
self.ws.on_open = self.on_open | |
try: | |
self.run() | |
except KeyboardInterrupt: | |
self.ws.close() | |
def run(self): | |
print("### run ###") | |
self.ws.run_forever() | |
pass | |
def on_message(self, ws, message): | |
now = datetime.now(JST) | |
print(str(now), message) | |
def on_error(self, ws, error): | |
print(error) | |
sys.exit() | |
def on_close(self, ws): | |
print("### closed ###") | |
def on_open(self, ws): | |
print("### open ###") | |
ws.send(json.dumps({'event':'addChannel','channel':'ok_sub_spot_btc_usdt_deals'})) | |
ws.send(json.dumps({'event':'addChannel','channel':'ok_sub_futureusd_btc_trade_this_week'})) | |
ws.send(json.dumps({'event':'addChannel','channel':'ok_sub_futureusd_btc_trade_next_week'})) | |
ws.send(json.dumps({'event':'addChannel','channel':'ok_sub_futureusd_btc_trade_quarter'})) | |
if __name__=="__main__": | |
OkexStream() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment