# HG changeset patch # User bsw # Date 1428704433 -7200 # Node ID 20e0d4f51381f09a8452ad58cb06c182cd9da9af # Parent 3bbcd75eefcd7d4080e2b88586bd3b9112eca31d Updated chat example diff -r 3bbcd75eefcd -r 20e0d4f51381 example_application.lua --- a/example_application.lua Sat Apr 11 00:18:12 2015 +0200 +++ b/example_application.lua Sat Apr 11 00:20:33 2015 +0200 @@ -37,82 +37,80 @@ for conn, client in pairs(clients) do - repeat - local line = conn:read_nb(nil, "\n") - if not line then - clients[conn] = nil - io_listener[conn] = nil + 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?)") + 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 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 + 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 - client.type = "chat" - if sessions[line] then - client.session = line - io_listener[conn] = true - else - conn:close() - clients[conn] = nil - end + conn:close() + clients[conn] = nil 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 + 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 - until success + 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 = "is now known as " .. name + msg = arg } - 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 - until not line or line == "" + end end for conn, client in pairs(clients) do @@ -142,7 +140,7 @@ end - moonbridge_io.poll(io_listener, io_writer) + moonbridge_io.poll(io_listener, io_writer, 5) end