Giter Club home page Giter Club logo

Comments (6)

EnTerr avatar EnTerr commented on August 16, 2024

After some debugging seems that sync.lua's receive()'s clean() is way too aggressive (why oh why?) in closing the connection on any kind of error. I added an if and it seems to work for me now:

  local clean = function(was_clean,code,reason)
    if reason ~= 'timeout' then
      self.state = 'CLOSED'
      self:sock_close()
      if self.on_close then
        self:on_close()
      end
    end
    return nil,nil,was_clean,code,reason or 'closed'
  end

PS. not sure if that won't cause problem if a timeout happens after the first sock_receive, when there is data in encoded and thus it will be lost?

from lua-websockets.

lipp avatar lipp commented on August 16, 2024

@EnTerr Added this as feature request. It's not that trivial, as you must think of partially received messages/frames.

from lua-websockets.

lipp avatar lipp commented on August 16, 2024

great. i'll keep this issue open, until ws:receive() accepts an optional timeout

from lua-websockets.

EnTerr avatar EnTerr commented on August 16, 2024

Ok, so here is a more complete fix i came up with, most code is round about https://github.com/lipp/lua-websockets/blob/master/src/websocket/sync.lua#L24

  ---
  if self._saved then
    local _ = self._saved
    first_opcode = _.first_opcode
    frames = _.frames
    bytes = _.bytes
    encoded = _.encoded
    self._saved = nil   -- erase the saved state
  end
  ---
  while true do
    local chunk, err, partial = self:sock_receive(bytes)

    if err then
      if err == 'timeout'  then
        if #partial > 0 then
          -- there was some partial data, update
          encoded = encoded .. partial
          bytes = bytes - #partial
        end
        -- save state for next call
        self._saved = {
          first_opcode = first_opcode, 
          frames = frames,
          bytes = bytes,
          encoded = encoded,
        }
      end
      return clean(false,1006,err)
    end

So every time receive() is called, it checks if there is a pending self._saved and if so restores state from it. Then on timeout error it saves partial data for the next time.

In addition, close() and connect() have to do self._saved = nil to ensure old state does not persist for potential next connection. And... that's it!

from lua-websockets.

lipp avatar lipp commented on August 16, 2024

nice! can you make a PR from this?

from lua-websockets.

EnTerr avatar EnTerr commented on August 16, 2024

Oops, afraid i can't. I don't have the code under git - besides i am working on almost a year old lua-websockets version, under svn. I did skim the /master/src/websocket/sync.lua here and there are no substantial/relevant changes - but i am nor really set up to PR. Maybe if one day i get current i will

from lua-websockets.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.