summaryrefslogtreecommitdiff
path: root/main.lua
blob: cf0342858b5f10f995d3081d6f8ba088d36995d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
print("it is already far, far too late.")

local cqueues = require("cqueues")
local cq = cqueues.new()

local httpserver = require("http.server")
local websocket = require("http.websocket")
local headers = require "http.headers"

function onstream(server, stream)
    print("stream you.")
	local ws = websocket.new_from_stream(stream, stream:get_headers())
	if not ws then
		local response = headers.new()
		response:append(":status", "400")
		stream:write_headers(response, false)
		stream:write_chunk("bad request: not a websocket handshake", true)
		return
	end
	ws:accept()
	while true do
		local data = ws:receive()
		if not data then return end
		ws:send(data)
	end
end

local sv = httpserver.listen {
	host = "127.0.0.1",
	port = "61111",
	onstream = onstream,
}

sv:listen()
cq:wrap(function()
	assert(sv:loop())
end)

assert(cq:loop())