【问题标题】:while loop in socket io套接字io中的while循环
【发布时间】:2021-10-29 13:29:52
【问题描述】:

hii 我正在尝试使用 socketio python 构建客户端。 捕获事件时如何打破循环

def login():
index = 0
while index == 0:
    username = input("please enter your username\n")
    password = input("please enter your password\n")
    sio.emit('send user name and password', {"username": username, "password": password})

    @sio.on('wrong username')
    def print_message():
        print("this username is not exist")

    @sio.on('wrong password')
    def print_message():
        print("your password is wrong please try again")

    @sio.on('connected')
    def print_message():
        global index
        index = 1
        print("LOGIN OK")

    sio.sleep(2)

【问题讨论】:

    标签: python socket.io client python-socketio


    【解决方案1】:

    您不必将这些函数放在一个循环中。他们会自动捕捉分配给他们的事件:

    def login():
        index = 0
        username = input("please enter your username\n")
        password = input("please enter your password\n")
        sio.emit('send user name and password', {"username": username, "password": password})
    
    @sio.on('wrong username')
    def print_message():
        print("this username is not exist")
    
    @sio.on('wrong password')
    def print_message():
        print("your password is wrong please try again")
    
    @sio.on('connected')
    def print_message():
        global index
        index = 1
        print("LOGIN OK")
    
       # sio.sleep(2) 
    

    【讨论】:

    • 但如果用户不存在或密码已磨损,我希望该功能再次询问用户名和密码
    【解决方案2】:

    Socket.IO 是异步的,您不要将事件处理程序放在 while 循环中。如果您需要将主线程与收到的事件同步,您可以使用Event 对象。像这样的:

    login_ok = None
    event = threading.Event()
    
    @sio.on('wrong username')
    def print_message():
        print("this username is not exist")
        login_ok = False
        event.set()
    
    @sio.on('wrong password')
    def print_message():
        print("your password is wrong please try again")
        login_ok = False
        event.set()
    
    @sio.on('connected')
    def print_message():
        global index
        index = 1
        print("LOGIN OK")
        login_ok = True
        event.set()
    
    def login():
        index = 0
        while index == 0:
            username = input("please enter your username\n")
            password = input("please enter your password\n")
            sio.emit('send user name and password', {"username": username, "password": password})
    
            event.wait()
            event.clear()
            if login_ok:
                break
    

    假设您还编写了服务器,您可能要考虑的另一个选项是使用 Socket.IO 的 ACK 功能。您可以将 ACK 响应设置为登录结果,而不是使用单独的事件响应登录。那么你就不必在客户端处理任何事件,只需从事件响应中获取登录结果即可。这是一个例子:

    def login():
        index = 0
        while index == 0:
            username = input("please enter your username\n")
            password = input("please enter your password\n")
            response = sio.call('send user name and password', {"username": username, "password": password})
            if response['login_ok']:
                break
            print(response['error'])
    

    要使第二个选项起作用,必须修改您的服务器以发送带有login_okerror 的字典作为对登录事件的响应。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-01
      • 1970-01-01
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-26
      相关资源
      最近更新 更多