Skip to content

Commit c9d013b

Browse files
committed
Misskey: websocket切断時の再接続処理追加
1 parent 76419f9 commit c9d013b

1 file changed

Lines changed: 32 additions & 27 deletions

File tree

run.py

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import logging
99
import logging.config
1010
import sys
11+
import time
1112
from concurrent.futures import ThreadPoolExecutor
1213
from typing import Callable, List
1314

@@ -185,33 +186,37 @@ def main():
185186
misskey_client = Misskey(conf.MISSKEY_DOMAIN, i=conf.MISSKEY_API_TOKEN)
186187

187188
async def discord_runner():
188-
# pylint: disable=E1101
189-
async with websockets.connect(
190-
"wss://"
191-
+ misskey_client.address
192-
+ "/streaming"
193-
+ "?i="
194-
+ misskey_client.token
195-
) as ws:
196-
await ws.send(
197-
json.dumps(
198-
{"type": "connect", "body": {"channel": "main", "id": "main"}}
199-
)
200-
)
201-
while True:
202-
data = json.loads(await ws.recv())
203-
if data["type"] == "channel" and data["body"]["type"] == "mention":
204-
note = data["body"]["body"]
205-
host = note["user"].get("host")
206-
mentions = note.get("mentions")
207-
if (
208-
(host is None or host == conf.MISSKEY_DOMAIN)
209-
and mentions
210-
and misskey_client.i()["id"] in mentions
211-
):
212-
analyze.analyze_message(
213-
note["text"].replace("\xa0", " ").split(" ", 1)[1]
214-
)(MisskeyClient(misskey_client, note))
189+
while True:
190+
try:
191+
# pylint: disable=E1101
192+
async with websockets.connect(
193+
"wss://"
194+
+ misskey_client.address
195+
+ "/streaming"
196+
+ "?i="
197+
+ misskey_client.token
198+
) as ws:
199+
await ws.send(
200+
json.dumps(
201+
{"type": "connect", "body": {"channel": "main", "id": "main"}}
202+
)
203+
)
204+
while True:
205+
data = json.loads(await ws.recv())
206+
if data["type"] == "channel" and data["body"]["type"] == "mention":
207+
note = data["body"]["body"]
208+
host = note["user"].get("host")
209+
mentions = note.get("mentions")
210+
if (
211+
(host is None or host == conf.MISSKEY_DOMAIN)
212+
and mentions
213+
and misskey_client.i()["id"] in mentions
214+
):
215+
analyze.analyze_message(
216+
note["text"].replace("\xa0", " ").split(" ", 1)[1]
217+
)(MisskeyClient(misskey_client, note))
218+
except websockets.ConnectionClosedError:
219+
time.sleep(1)
215220

216221
asyncio.get_event_loop().run_until_complete(discord_runner())
217222
else:

0 commit comments

Comments
 (0)