After referencing the ws connection in the NetsBlox web client (wss://cloud.netsblox.org/network/.../connect), I have tried the following code to listen() to a robot and get the messages for the various usages in the course, though it seems that either RoboScape.listen() or @Client.on_message do not function as expected, resulting in one-way communication between the computer and the robot.
Code:
import netsblox
client = netsblox.Client(run_forever=True)
# {"type":"message","msgType":"robot command","content":{"robot":"0004f31d4e7d","command":"set key 125 22 76 34 98 91 64 20 23 104 22 114 110 82 116 83 35"}} -- assume .on_message runs function based on message['msgType'] sent to ws
@client.on_message('robot message', 'robot command')
def handle_message(msg_type, msg_args):
print(f"Received message: type={msg_type}, args={msg_args}")
def main():
robots = client.call("RoboScape", "getRobots") # alt. RoboScape class's get_robots() function
print(f"Authorized robots: {robots}")
if not robots:
print("No robots found.")
return
robot = robots[0] # pick the first one
print(robot)
client.robo_scape.listen(robot)
command = "is alive"
response = client.call("RoboScape", "send", robot=robot, command=command)
print(f"Sent '{command}' to {robot}, got response: {response}")
main()
Output:
Authorized robots: ['0004f3768fff']
0004f3768fff
Listening to robot: 0004f3768fff
Sent 'is alive' to 0004f3768fff, got response: True
Despite multiple other messages being sent to 0004f3768fff, shown by the web client's ws, there is never a message received in Python, or at least one that calls the necessary handler(s).
I also tried adding prints into Client._ws_message(), though the print statement is never triggered (in either of the try and excepts), which implies I'm looking into the wrong functions or the message never gets to that point.

After referencing the
wsconnection in the NetsBlox web client (wss://cloud.netsblox.org/network/.../connect), I have tried the following code tolisten()to a robot and get the messages for the various usages in the course, though it seems that eitherRoboScape.listen()or@Client.on_messagedo not function as expected, resulting in one-way communication between the computer and the robot.Code:
Output:
Despite multiple other messages being sent to
0004f3768fff, shown by the web client'sws, there is never a message received in Python, or at least one that calls the necessary handler(s).I also tried adding
prints intoClient._ws_message(), though the print statement is never triggered (in either of thetryandexcepts), which implies I'm looking into the wrong functions or the message never gets to that point.