Lua sockets - Asynchronous Events

前端 未结 4 626
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-16 19:46

In current lua sockets implementation, I see that we have to install a timer that calls back periodically so that we check in a non blocking API to see if we have received a

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-16 20:28

    You are probably using a non-blocking select() to "poll" sockets for any new data available. Luasocket doesn't provide any other interface to see if there is new data available (as far as I know), but if you are concerned that it's taking too much time when you are doing this 10 times per second, consider writing a simplified version that only checks one socket you need and avoids creating and throwing away Lua tables. If that's not an option, consider passing nil to select() instead of {} for those lists you don't need to read and pass static tables instead of temporary ones:

    local rset = {socket}
    ... later
    ...select(rset, nil, 0)
    

    instead of

    ...select({socket}, {}, 0)
    

提交回复
热议问题