fromaiohttpimportwebimportjsonasyncdefindex(request):returnweb.FileResponse('index.html')asyncdefwscontroller(request):ws=web.WebSocketResponse()awaitws.prepare(request)asyncformsginws:ifmsg.type==web.WSMsgType.text:req=json.loads(msg.data)print(req)awaitws.send_str(json.dumps(req))elifmsg.type==web.WSMsgType.binary:print("Unknown binary data received.")breakelifmsg.type==web.WSMsgType.close:breakreturnwsapp=web.Application()app.add_routes([web.get('/',index),web.get('/controller',wscontroller),web.static('/static','static'),])if__name__=='__main__':web.run_app(app)
fromaiohttpimportwebimportjsonfromservocontrollerimportServoControllersvc=ServoController()x=0.0y=0.0defcontroller(cmd):globalxglobalyifcmd['type']=='keydown':ifcmd['key']=='ArrowUp':y+=0.01elifcmd['key']=='ArrowDown':y-=0.01elifcmd['key']=='ArrowLeft':x-=0.01elifcmd['key']=='ArrowRight':x+=0.01ifx>=1.0:x=1.0elifx<=-1.0:x=-1.0ify>=1.0:y=1.0elify<=-1.0:y=1.0svc.set_pos(x,y)asyncdefindex(request):returnweb.FileResponse('index.html')asyncdefdash(request):returnweb.FileResponse('dash.html')asyncdefwscontroller(request):globalxglobalyws=web.WebSocketResponse()awaitws.prepare(request)asyncformsginws:ifmsg.type==web.WSMsgType.text:req=json.loads(msg.data)print(req)controller(req)req["pos_x"]=xreq["pos_y"]=yawaitws.send_str(json.dumps(req))elifmsg.type==web.WSMsgType.binary:print("Unknown binary data received.")breakelifmsg.type==web.WSMsgType.close:breakreturnwsapp=web.Application()app.add_routes([web.get('/',index),web.get('/controller',wscontroller),web.static('/static','static'),])if__name__=='__main__':web.run_app(app)