lune-org/lune

WebSocket objects do not allow for receiving and sending at the same time

Closed this issue · 1 comments

Due to web socket objects aquiring an exclusive lock, this piece of code currently does not work:

local task = require("@lune/task")
local net = require("@lune/net")

local socket = net.socket("wss://...")

task.spawn(function()
    while true do
        local message = socket.next()

        -- ...
    end
end)

task.defer(function()
    print("0")

    -- This will yield indefinitely since `socket.next` completely locks the socket
    socket.send(...)

    print("1")
end)

I see two possible solutions here:

  1. Split the websocket into send & receive parts, lock them separately when their methods are called
  2. Handle receiving/sending of messages internally, not directly exposing it to lua. This would mean wrapping the websocket in some kind of message queue so that when we call next from multiple lua threads, and receive a message, all waiting lua threads will get the same message

The second solution seems more robust to me but may also require us to integrate with the async scheduler in some capacity

Fixed in #68