PlutoLang / Pluto

A superset of Lua 5.4 with a focus on general-purpose programming.
https://pluto-lang.org/docs/Introduction
MIT License
339 stars 20 forks source link

Socket: TCP Server #758

Closed Sainan closed 3 months ago

Sainan commented 3 months ago

Preliminary API design:

local { scheduler, socket } = require "*"

function socket.bind(sched, port, callback)
    sched:add(function()
        local l = socket.listen(port)
        while s := l:accept() do
            sched:add(function()
                callback(s)
            end)
        end
    end)
end

local sched = new scheduler()
socket.bind(sched, 80, |s| -> do
    print(s:recv())
end)
--[[socket.bind(sched, 443, |s| -> do
    if s:starttls() then -- TODO: Certificate?
        print(s:recv())
    end
end)]]
sched:run()