Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 define a variable before looped websocket
#1
Hello ,
I have a wroking websocket and I wnat to only do something when I get new reaply / new data

async def hello(websocket, path):
    #1 OldClientResponse="none"
    ClientResponse = await websocket.recv()
    print ("OLD is - " + OldClientResponse + "\r\nNew is  " + ClientResponse) 
    if (OldClientResponse != ClientResponse):
        print("New Client Response Is:\n\r" + ClientResponse)
        #going to do some code here 
        OldClientResponse = ClientResponse

#2 OldClientResponse="none"   
start_server = websockets.serve(hello, '10.0.0.79', 1234)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
The problem is that the program is allways enter the "if" , and its seem that old!=new all the time
why?
when #1 is on - I can understand why
when #2 is on - I get error

what am I missing ?


when I do this
async def hello(websocket, path):
    global OldClientResponse
    print (OldClientResponse)
    ClientResponse = await websocket.recv()
    print ("OLD is - " + OldClientResponse + "\r\nNew is  " + ClientResponse) 
    if (OldClientResponse != ClientResponse):
        print("New Client Response Is:\n\r" + ClientResponse)
        #going to do some code here 
        OldClientResponse = ClientResponse

OldClientResponse="none"   
start_server = websockets.serve(hello, '10.0.0.79', 1234)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
it print me the value "None" but doens't do the comapring saying :
    if (OldClientResponse != ClientResponse):
UnboundLocalError: local variable 'OldClientResponse' referenced before assignment
Thanks ,
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Websocket server not async? korenron 0 174 Sep-23-2019, 01:40 PM
Last Post: korenron
  need to understand better __setup__.py, websocket-client, packaging penright 0 818 Jul-08-2018, 09:40 PM
Last Post: penright
  Pro's and Con's of some different websocket libraries penright 0 608 Jun-29-2018, 12:07 PM
Last Post: penright

Forum Jump:


Users browsing this thread: 1 Guest(s)