1+ import asyncio
12import sys
3+ import time
4+ import threading
5+ from types import SimpleNamespace
26from unrealircd_rpc_py .Live import LiveWebsocket
37
48##########################################
59# How to connect using callback function #
610##########################################
711
8- def callback_function_irc (response ):
12+ def callback_function_irc (response : SimpleNamespace ) -> None :
13+ # Response model: https://www.unrealircd.org/docs/JSON-RPC:Log#log.subscribe
14+ # Possible responses: https://www.unrealircd.org/docs/List_of_all_log_messages
15+
16+ # High level error
17+ if liverpc .get_error .code != 0 :
18+ print ("ERROR:" , liverpc .get_error .code , liverpc .get_error .message )
19+ return
20+
21+ # Because of the concurrent approach! there is also local errors
22+ if hasattr (response , 'error' ):
23+ # Check if you are allowed to use log endpoint
24+ print (response .error .code , response .error .message )
25+ return
926
1027 if hasattr (response , 'result' ):
11- if isinstance (response .result , bool ) and response . result :
12- # Check if you are allowed to use log endpoint
13- print (liverpc . get_error )
28+ if isinstance (response .result , bool ):
29+ if response . result :
30+ print (f" { response . method } has been activated" )
1431 return
1532
1633 level = response .result .level if hasattr (response .result , 'level' ) else ''
@@ -21,10 +38,6 @@ def callback_function_irc(response):
2138
2239 build_msg = f"{ log_source } : [{ level } ] { subsystem } .{ event_id } - { msg } "
2340
24- # Check if there is an error
25- if liverpc .get_error .code != 0 :
26- print (f"Your Error message: { liverpc .get_error .message } " )
27-
2841 print (build_msg )
2942
3043# Init your live stream using websocket
@@ -36,12 +49,28 @@ def callback_function_irc(response):
3649 callback_method_or_function_name = 'callback_function_irc'
3750 )
3851
39- # Check if connection is correct
40- if liverpc . get_error . code != 0 :
41- print (liverpc .get_error . message , liverpc . get_error . code )
42- sys . exit ( 1 )
52+ def thread_subscribe () -> None :
53+ # This will run until the thread_unsubscribe is called!
54+ response = asyncio . run (liverpc .subscribe ([ "all" ]) )
55+ print ( f"FINAL RESPONSE OF SUBSCRIBE: { response } " )
4356
44- liverpc .subscribe (["all" ])
57+ def thread_unsubscribe () -> None :
58+ response = asyncio .run (liverpc .unsubscribe ())
59+ print (f"FINAL RESPONSE OF UNSUBSCRIBE: { response } " )
60+
61+
62+ th_subscribe = threading .Thread (target = thread_subscribe , daemon = False )
63+ th_unsubscribe = threading .Thread (target = thread_unsubscribe , daemon = False )
64+
65+ # Subscribe to the stream
66+ th_subscribe .start ()
67+
68+ # Do some stuff
69+ time .sleep (2 )
70+
71+ # If you want to disconnect from the stream
72+ # run your new unsubscribe thread
73+ th_unsubscribe .start ()
4574
4675########################################
4776# How to connect using callback method #
@@ -63,13 +92,37 @@ def __init__(self):
6392 print (self .liverpc .get_error .message , self .liverpc .get_error .code )
6493 return
6594
66- self .liverpc .subscribe (['all' ])
95+ def thread_subscribe (self ) -> None :
96+
97+ response : dict [str , dict ] = asyncio .run (self .liverpc .subscribe ())
98+ print ("[JSONRPC SUBSCRIBE] Subscribe to the stream!" )
99+ print (response )
67100
68- def callback_method_irc (self , response ):
101+ def thread_unsubscribe (self ) -> None :
102+
103+ response : dict [str , dict ] = asyncio .run (self .liverpc .unsubscribe ())
104+ print ("[JSONRPC UNSUBSCRIBE] Unsubscribe from the stream!" )
105+ print (response )
106+
107+ def callback_method_irc (self , response : SimpleNamespace ) -> None :
108+ # Response model: https://www.unrealircd.org/docs/JSON-RPC:Log#log.subscribe
109+ # Possible responses: https://www.unrealircd.org/docs/List_of_all_log_messages
110+
111+ # High level error
112+ if self .liverpc .get_error .code != 0 :
113+ print ("ERROR:" , self .liverpc .get_error .code , self .liverpc .get_error .message )
114+ return
115+
116+ # Because of the concurrent approach! there is also local errors
117+ if hasattr (response , 'error' ):
118+ # Check if you are allowed to use log endpoint
119+ print (response .error .code , response .error .message )
120+ return
69121
70122 if hasattr (response , 'result' ):
71- if isinstance (response .result , bool ) and response .result :
72- print (self .liverpc .get_error )
123+ if isinstance (response .result , bool ):
124+ if response .result :
125+ print (f"{ response .method } has been activated" )
73126 return
74127
75128 level = response .result .level if hasattr (response .result , 'level' ) else ''
@@ -80,7 +133,18 @@ def callback_method_irc(self, response):
80133
81134 build_msg = f"{ log_source } : [{ level } ] { subsystem } .{ event_id } - { msg } "
82135 print (build_msg )
83- print (self .liverpc .get_error )
84136
85137
86- CallBack ()
138+ live_stream = CallBack ()
139+ th_subscribe = threading .Thread (target = live_stream .thread_subscribe )
140+ th_unsubscribe = threading .Thread (target = live_stream .thread_unsubscribe )
141+
142+ # Subscribe to the stream
143+ th_subscribe .start ()
144+
145+ # Do some stuff
146+ time .sleep (2 )
147+
148+ # If you want to disconnect from the stream
149+ # run your unsubscribe thread
150+ th_unsubscribe .start ()
0 commit comments