FredyH / GWSockets

WebSockets for GLua
MIT License
87 stars 7 forks source link

Data written while connection is not open is ignored #4

Closed TrixterTheTux closed 5 years ago

TrixterTheTux commented 6 years ago

Tested both on Windows and Linux with the following code:

require('gwsockets')
RunConsoleCommand('sv_hibernate_think', 1)

local socket = GWSockets.createWebSocket("wss://echo.websocket.org/")
function socket:onMessage(txt)
    print("Received: ", txt)
end

function socket:onError(txt)
    print("Error: ", txt)
end

function socket:onConnected()
    print("Connected to echo server")

    timer.Simple(5, function()
        socket:close()
    end)
end

function socket:onDisconnected()
    print("WebSocket disconnected")
end

socket:write("hello world")
socket:open()

Same thing happens if you write data after connection closes and then reopen it.

Would there also be a possibility to skip the queue (so I can implement a custom one) or get some confirmation that the data was sent? It'd be useful for cases where you need critical data to be sent to the server, and this would allow it to persist between server restarts until it gets received (without requiring the websocket server to reply that the data was received).

FredyH commented 6 years ago

The queue of remaining write operations is cleared when you call open(). The reason for this is that when your socket gets closed then there's no real way of knowing what is left in the queue, hence to make sure we don't send any unwanted things the queue is cleared.

You cannot reliably get confirmation that the data was successfully received by the other end through TCP alone, you'd have to employ your own protocol for this (such as sending a confirmation message upon receipt). In general even this is unsafe though since the receipt message can be lost, so you might need to send a message twice and detect duplicates on the other end using sequence numbers or similar.