diff --git a/doc/lua-api/index.rst b/doc/lua-api/index.rst index b9edce62b..e8d494b02 100644 --- a/doc/lua-api/index.rst +++ b/doc/lua-api/index.rst @@ -3916,21 +3916,25 @@ AppletTCP class *size* is missing, the function tries to read all the content of the stream until the end. An optional timeout may be specified in milliseconds. In this case the function will return no longer than this delay, with the amount of - available data (possibly none). + available data, or nil if there is no data. An empty string is returned if the + connection is closed. :param class_AppletTCP applet: An :ref:`applettcp_class` :param integer size: the required read size. - :returns: always return a string, the string can be empty if the connection is - closed. + :returns: return nil if the timeout has expired and no data was available but + can still be received. Otherwise, a string is returned, possibly an empty + string if the connection is closed. .. js:function:: AppletTCP.try_receive(applet) Reads available data from the TCP stream and returns immediately. Returns a - string containing read bytes that may possibly be empty if no bytes are - available at that time. + string containing read bytes or nil if no bytes are available at that time. An + empty string is returned if the connection is closed. :param class_AppletTCP applet: An :ref:`applettcp_class` - :returns: always return a string, the string can be empty. + :returns: return nil if no data was available but can still be + received. Otherwise, a string is returned, possibly an empty string if the + connection is closed. .. js:function:: AppletTCP.send(appletmsg) diff --git a/examples/lua/trisdemo.lua b/examples/lua/trisdemo.lua index 4b60bb5ac..e591b996a 100644 --- a/examples/lua/trisdemo.lua +++ b/examples/lua/trisdemo.lua @@ -215,7 +215,7 @@ function handler(applet) local input = applet:receive(1, delay) if input then - if input == "q" then + if input == "" or input == "q" then game_over = true elseif input == "\27" then local a = applet:receive(1, delay) diff --git a/src/hlua.c b/src/hlua.c index 6c61762e9..bdf31da76 100644 --- a/src/hlua.c +++ b/src/hlua.c @@ -5405,7 +5405,7 @@ __LJMP static int hlua_applet_tcp_recv_try(lua_State *L) if (ret == 0) { if (tick_is_expired(exp_date, now_ms)) { /* return the result. */ - luaL_pushresult(&luactx->b); + lua_pushnil(L); return 1; } @@ -5545,7 +5545,7 @@ __LJMP static int hlua_applet_tcp_try_recv(lua_State *L) lua_pushinteger(L, -1); /* set the expiration date (mandatory arg but not relevant here) */ - lua_pushinteger(L, TICK_ETERNITY); + lua_pushinteger(L, now_ms); /* Initialise the string catenation. */ luaL_buffinit(L, &luactx->b);