33import signal
44import sys
55import time
6- import asyncio
7- import json
8- import websockets
9- import struct
10-
11- uri = "ws://localhost:8765"
12- async def call_service (ws , service_name , service_id ):
13- # Call a service and return the response payload
14- msg = bytearray ([0x02 ]) # opcode
15- msg .extend (struct .pack ('<I' , service_id ))
16- msg .extend (struct .pack ('<I' , 1 )) # call_id
17- msg .extend (struct .pack ('<I' , 4 )) # encoding length
18- msg .extend (b"json" )
19- msg .extend (b"{}" )
20- await ws .send (bytes (msg ))
21-
22- response = await asyncio .wait_for (ws .recv (), timeout = 5.0 )
23- if isinstance (response , bytes ) and response [0 ] == 0x03 :
24- encoding_len = struct .unpack ('<I' , response [9 :13 ])[0 ]
25- payload = response [13 + encoding_len :]
26- return payload .decode ('utf-8' )
27- return None
28-
29- async def test_available_services ():
30- async with websockets .connect (uri , subprotocols = ["foxglove.websocket.v1" ]) as ws :
31- while True :
32- msg = json .loads (await asyncio .wait_for (ws .recv (), timeout = 5.0 ))
33- if msg .get ("op" ) == "advertiseServices" :
34- services = {s ['name' ]: s ['id' ] for s in msg .get ("services" , [])}
35- print (f"Available services: { list (services .keys ())} " )
36- break
37-
38- # Test each service
39- for service_name in services :
40- try :
41- result = await call_service (ws , service_name , services [service_name ])
42- print (f"{ service_name } : { result } " )
43- except Exception as e :
44- print (f"{ service_name } : ERROR - { e } " )
45-
466
477isRunning = True
488def stop ():
@@ -65,4 +25,3 @@ def testService(input):
6525remoteConnection .registerService ("myService" , testService )
6626while isRunning :
6727 time .sleep (1 )
68- asyncio .run (test_available_services ())
0 commit comments