Comments (6)
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.
@EnTerr Added this as feature request. It's not that trivial, as you must think of partially received messages/frames.
from lua-websockets.
great. i'll keep this issue open, until ws:receive() accepts an optional timeout
from lua-websockets.
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.
nice! can you make a PR from this?
from lua-websockets.
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)
- Can't install via luarocks HOT 1
- wss client connect error HOT 1
- copas looping issue while connecting the server
- How can I use lua-websockets with an socket.io server? HOT 1
- Need help on unity integration
- Compatibility with Lua 5.3? HOT 1
- luarocks outdated as hell
- Build error: Failed compiling object src/mime.obj HOT 2
- ERROR_NOT_ENOUGH_MEMORY
- Ive been wandering why didnt you guys set a heartbeat (ping pong)
- client:getpeername() but with websocket:client?
- websocket does not close when wlan/lan is disconnected. HOT 6
- Reserve `websocket.extension` name HOT 1
- How to handle authentication? HOT 2
- Sync receive check - masked or unmasked? HOT 2
- PING causes receive() problems HOT 10
- No way to pass Origin in sync client
- Support Message Fragmentation HOT 2
- Will this ever have SSL support for the server? HOT 2
- Add a license? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lua-websockets.