# HG changeset patch # User jbe # Date 1434751232 -7200 # Node ID 822ccaeccccbccd0cd00219759c5986f3251726b # Parent d338068fad0db09f5b86b2a1a7ce9aa0e00cad4b Removed example chat application diff -r d338068fad0d -r 822ccaeccccb example_application.lua --- a/example_application.lua Fri Jun 19 20:20:48 2015 +0200 +++ b/example_application.lua Sat Jun 20 00:00:32 2015 +0200 @@ -7,150 +7,12 @@ -- preparation before forking: local documents = {} -for i, document_name in ipairs{"example_webpage.html", "example_chat.html", "example_webpage.css"} do +for i, document_name in ipairs{"example_webpage.html", "example_webpage.css"} do local file = assert(io.open(document_name)) documents[document_name] = file:read("*a") -- store file contents in memory file:close() end -local function chat_server(request) - - - local listener = assert(moonbridge_io.tcplisten("localhost", 8081)) - local clients = {} - local io_listener = { [listener] = true } - local io_writer = {} - local sessions = {} - local msgs = {} - local last_msg = 0 - - while true do - - local new_conn = listener:accept_nb() - - if new_conn then - clients[new_conn] = { - read_buffer = "" - } - io_listener[new_conn] = true - end - - for conn, client in pairs(clients) do - - local line = conn:read_nb(nil, "\n") - if not line then - clients[conn] = nil - io_listener[conn] = nil - - elseif line ~= "" then - local line, terminator = line:match("([^\n]*)(\n?)") - - if terminator ~= "\n" then - client.read_buffer = client.read_buffer .. line - else - local line = client.read_buffer .. line - client.read_buffer = "" - - if not client.type then - if line == "events" then - client.type = "events" - client.session = "sesam" .. math.random(10000000,99999999) - client.name = "user".. math.random(10000000,99999999) - client.last_msg = 0 - client.send_session = true - client.send_name = true - client.last_msg = #msgs - sessions[client.session] = conn - io_listener[conn] = nil - else - client.type = "chat" - if sessions[line] then - client.session = line - io_listener[conn] = true - else - conn:close() - clients[conn] = nil - end - end - else - if client.type == "chat" then - local event_client = clients[sessions[client.session]] - local command, arg = line:match("([^:]+):(.*)") - if not command then - elseif command == "NAME" then - local name = arg - local success - repeat - success = true - for conn2, client2 in pairs(clients) do - if client2.name == name then - name = name .. math.random(0,9) - success = false - break - end - end - until success - last_msg = last_msg + 1 - msgs[last_msg] = { - name = event_client.name, - msg = "is now known as " .. name - } - event_client.name = name - event_client.send_name = true - elseif command == "MSG" then - if #arg > 0 then - last_msg = last_msg + 1 - msgs[last_msg] = { - name = event_client.name, - msg = arg - } - end - end - end - - end - end - end - end - - for conn, client in pairs(clients) do - if client.type == "events" then - if client.send_session then - assert(conn:write_nb("SESSION:" .. client.session .. "\n")) - client.send_session = false - end - if client.send_name then - assert(conn:write_nb("NAME:" .. client.name .. "\n")) - client.send_name = false - end - if client.last_msg < last_msg then - for i = client.last_msg + 1, last_msg do - assert(conn:write_nb("MSG:" .. msgs[i].name .. " " .. msgs[i].msg .. "\n")) - end - client.last_msg = last_msg - end - assert(conn:write_nb("TIME:" .. os.time() .. "\n")) - local bytes_left = assert(conn:flush_nb()) - if bytes_left > 0 then - io_writer[conn] = true - else - io_writer[conn] = false - end - end - - end - - moonbridge_io.poll(io_listener, io_writer, 5) - - end - -end - -listen{ - { proto = "main" }, - connect = chat_server -} - listen{ -- listen to a tcp version 4 socket --{ proto = "tcp", host = "0.0.0.0", port = 8080 }, @@ -217,22 +79,6 @@ request:send_status("303 See Other") request:send_header("Location", "http://" .. request.headers_value.host .. "/example_webpage.html") - elseif request.path == "chat" then - request:send_status("200 OK") - request:send_header("Content-Type", "text/chat; charset=UTF-8") - request:send_data("MULTIUSERCHAT:protocol version 1\n") - - local conn = assert(moonbridge_io.tcpconnect("localhost", 8081)) - - conn:write("events\n") - conn:flush() - - while true do - local line = conn:read(nil, "\n") - request:send_data(line) - request:flush() - end - else local document_name = request.path local document_extension = string.match(document_name, "%.([^.])$") @@ -274,7 +120,7 @@ end request:send_status("200 OK") - request:send_header("Content-Type", "text/html; chatset=UTF-8") + request:send_header("Content-Type", "text/html; charset=UTF-8") request:send_data("\n\n") request:send_data('\n') request:send_data("Moonbridge Network Server for Lua Applications – Example Application\n") @@ -294,37 +140,6 @@ request:send_data("

Submitted comment: ", http.encode_html(request.post_params.comment), "

\n") request:send_data("\n\n") - elseif request.path == "chat_send" then - local conn = assert(moonbridge_io.tcpconnect("localhost", 8081)) - local body = request.body - local session - for line in body:gmatch("[^\r\n]+") do - local command, arg = line:match("([^:]+):(.*)") - if not command then - -- TODO error handling - return - elseif command == "SESSION" then - session = arg - else - if not session then - -- TODO error handling - return - end - conn:write(session .. "\n") - if command == "NAME" then - local name = arg:gsub(" ", "_") - conn:write("NAME:" .. name .. "\n") - elseif command == "MSG" then - local msg = arg - conn:write("MSG:" .. msg .. "\n") - end - end - end - conn:flush() - conn:close() - request:send_status("200 OK") - request:send_header("Content-Type", "text/xml; chatset=UTF-8") - else error_response("404 Not Found") diff -r d338068fad0d -r 822ccaeccccb example_chat.html --- a/example_chat.html Fri Jun 19 20:20:48 2015 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,134 +0,0 @@ - - - - - Moonbridge Network Server for Lua Applications – Example Application - - -

Moonbridge Network Server for Lua – Example Application

-

Multiuser chat

-
Connecting to server...
-
-
-
- Message:
- Name: - - Current server time: ... -
- - - - -