Skip to content

Commit

Permalink
Use same code to reconnect to MS WebSocket; resolve #950 -autopull
Browse files Browse the repository at this point in the history
  • Loading branch information
makyen committed Feb 29, 2020
1 parent ddd5438 commit 1365be9
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions metasmoke.py
Expand Up @@ -37,20 +37,22 @@

# noinspection PyClassHasNoInit,PyBroadException,PyUnresolvedReferences,PyProtectedMember
class Metasmoke:
@staticmethod
def connect_websocket():
GlobalVars.metasmoke_ws = websocket.create_connection(GlobalVars.metasmoke_ws_host,
origin=GlobalVars.metasmoke_host)
payload = json.dumps({"command": "subscribe",
"identifier": "{\"channel\":\"SmokeDetectorChannel\","
"\"key\":\"" + GlobalVars.metasmoke_key + "\"}"})
GlobalVars.metasmoke_ws.send(payload)
GlobalVars.metasmoke_ws.settimeout(10)

@staticmethod
def init_websocket():
has_succeeded = False
while True:
try:
GlobalVars.metasmoke_ws = websocket.create_connection(GlobalVars.metasmoke_ws_host,
origin=GlobalVars.metasmoke_host)
payload = json.dumps({"command": "subscribe",
"identifier": "{\"channel\":\"SmokeDetectorChannel\","
"\"key\":\"" + GlobalVars.metasmoke_key + "\"}"})
GlobalVars.metasmoke_ws.send(payload)

GlobalVars.metasmoke_ws.settimeout(10)

Metasmoke.connect_websocket()
has_succeeded = True
while True:
a = GlobalVars.metasmoke_ws.recv()
Expand All @@ -60,11 +62,7 @@ def init_websocket():
Metasmoke.handle_websocket_data(data)
Metasmoke.reset_failure_count()
except Exception as e:
GlobalVars.metasmoke_ws = websocket.create_connection(GlobalVars.metasmoke_ws_host,
origin=GlobalVars.metasmoke_host)
payload = json.dumps({"command": "subscribe",
"identifier": "{\"channel\":\"SmokeDetectorChannel\"}"})
GlobalVars.metasmoke_ws.send(payload)
Metasmoke.connect_websocket()
GlobalVars.metasmoke_failures += 1
log('error', e, f=True)
traceback.print_exc()
Expand Down

0 comments on commit 1365be9

Please sign in to comment.