use sio.sleep in example. add note about sio.wait

This commit is contained in:
onyx-and-iris 2022-11-13 14:41:03 +00:00
parent feaf06e639
commit 54a323fc81
2 changed files with 14 additions and 22 deletions

View File

@ -43,25 +43,21 @@ def on_twitch_event(event, msg):
print(f"{event}: {msg.attrs()}")
def register_callbacks(client):
def main():
with streamlabsio.connect(token="<apikey>") as client:
client.obs.on("streamlabs", on_twitch_event)
client.obs.on("twitch_account", on_twitch_event)
def main():
with streamlabsio.connect(token="<apikey>") as client:
worker = Thread(target=register_callbacks, args=(client,), daemon=True)
worker.start()
while cmd := input("<Enter> to exit\n"):
if not cmd:
break
# run for 30 seconds then disconnect client from server
client.sio.sleep(30)
if __name__ == "__main__":
main()
```
note. From the [SocketIO docs](https://python-socketio.readthedocs.io/en/latest/client.html#managing-background-tasks), `client.sio.wait()` may be used if your application has nothing to do in the main thread.
### Attributes
For event messages you may inspect the available attributes using `attrs()`.

View File

@ -1,5 +1,4 @@
import logging
from threading import Thread
import streamlabsio
@ -15,22 +14,19 @@ def on_twitch_event(event, msg):
print(f"Received follow from {msg.name}")
elif event == "bits":
print(f"{msg.name} donated {msg.amount} bits! With message: {msg.message}")
elif event == "donation":
print(f"{msg.name} donated {msg.formatted_amount}! With message: {msg.message}")
def register_callbacks(client):
def main():
# read token from config.toml
with streamlabsio.connect() as client:
client.obs.on("streamlabs", on_twitch_event)
client.obs.on("twitch_account", on_twitch_event)
client.obs.on("youtube_account", on_youtube_event)
def main():
with streamlabsio.connect() as client:
worker = Thread(target=register_callbacks, args=(client,), daemon=True)
worker.start()
while cmd := input("<Enter> to exit\n"):
if not cmd:
break
# run for 30 seconds then disconnect client from server
client.sio.sleep(30)
if __name__ == "__main__":