moonbridge

changeset 194:822ccaeccccb

Removed example chat application
author jbe
date Sat Jun 20 00:00:32 2015 +0200 (2015-06-20)
parents d338068fad0d
children 05fd82e3cfb7
files example_application.lua example_chat.html
line diff
     1.1 --- a/example_application.lua	Fri Jun 19 20:20:48 2015 +0200
     1.2 +++ b/example_application.lua	Sat Jun 20 00:00:32 2015 +0200
     1.3 @@ -7,150 +7,12 @@
     1.4  
     1.5  -- preparation before forking:
     1.6  local documents = {}
     1.7 -for i, document_name in ipairs{"example_webpage.html", "example_chat.html", "example_webpage.css"} do
     1.8 +for i, document_name in ipairs{"example_webpage.html", "example_webpage.css"} do
     1.9    local file = assert(io.open(document_name))
    1.10    documents[document_name] = file:read("*a")  -- store file contents in memory
    1.11    file:close()
    1.12  end
    1.13  
    1.14 -local function chat_server(request)
    1.15 -
    1.16 -  
    1.17 -  local listener = assert(moonbridge_io.tcplisten("localhost", 8081))
    1.18 -  local clients = {}
    1.19 -  local io_listener = { [listener] = true }
    1.20 -  local io_writer = {}
    1.21 -  local sessions = {}
    1.22 -  local msgs = {}
    1.23 -  local last_msg = 0
    1.24 - 
    1.25 -  while true do
    1.26 -    
    1.27 -    local new_conn = listener:accept_nb()
    1.28 -    
    1.29 -    if new_conn then
    1.30 -      clients[new_conn] = {
    1.31 -        read_buffer = ""
    1.32 -      }
    1.33 -      io_listener[new_conn] = true
    1.34 -    end
    1.35 -    
    1.36 -    for conn, client in pairs(clients) do
    1.37 -      
    1.38 -      local line = conn:read_nb(nil, "\n")
    1.39 -      if not line then
    1.40 -        clients[conn] = nil
    1.41 -        io_listener[conn] = nil
    1.42 -
    1.43 -      elseif line ~= "" then
    1.44 -        local line, terminator = line:match("([^\n]*)(\n?)")
    1.45 -        
    1.46 -        if terminator ~= "\n" then
    1.47 -          client.read_buffer = client.read_buffer .. line
    1.48 -        else
    1.49 -          local line = client.read_buffer .. line
    1.50 -          client.read_buffer = ""
    1.51 -          
    1.52 -          if not client.type then
    1.53 -            if line == "events" then
    1.54 -              client.type = "events"
    1.55 -              client.session = "sesam" .. math.random(10000000,99999999)
    1.56 -              client.name = "user".. math.random(10000000,99999999)
    1.57 -              client.last_msg = 0
    1.58 -              client.send_session = true
    1.59 -              client.send_name = true
    1.60 -              client.last_msg = #msgs
    1.61 -              sessions[client.session] = conn
    1.62 -              io_listener[conn] = nil
    1.63 -            else
    1.64 -              client.type = "chat"
    1.65 -              if sessions[line] then
    1.66 -                client.session = line
    1.67 -                io_listener[conn] = true
    1.68 -              else
    1.69 -                conn:close()
    1.70 -                clients[conn] = nil
    1.71 -              end
    1.72 -            end
    1.73 -          else
    1.74 -            if client.type == "chat" then
    1.75 -              local event_client = clients[sessions[client.session]]
    1.76 -              local command, arg = line:match("([^:]+):(.*)")
    1.77 -              if not command then
    1.78 -              elseif command == "NAME" then
    1.79 -                local name = arg
    1.80 -                local success
    1.81 -                repeat
    1.82 -                  success = true
    1.83 -                  for conn2, client2 in pairs(clients) do
    1.84 -                    if client2.name == name then
    1.85 -                      name = name .. math.random(0,9)
    1.86 -                      success = false
    1.87 -                      break
    1.88 -                    end
    1.89 -                  end
    1.90 -                until success
    1.91 -                last_msg = last_msg + 1
    1.92 -                msgs[last_msg] = {
    1.93 -                  name = event_client.name,
    1.94 -                  msg = "is now known as " .. name
    1.95 -                }
    1.96 -                event_client.name = name
    1.97 -                event_client.send_name = true
    1.98 -              elseif command == "MSG" then
    1.99 -                if #arg > 0 then
   1.100 -                  last_msg = last_msg + 1
   1.101 -                  msgs[last_msg] = {
   1.102 -                    name = event_client.name,
   1.103 -                    msg = arg
   1.104 -                  }
   1.105 -                end
   1.106 -              end
   1.107 -            end
   1.108 -            
   1.109 -          end
   1.110 -        end
   1.111 -      end
   1.112 -    end
   1.113 -
   1.114 -    for conn, client in pairs(clients) do
   1.115 -      if client.type == "events" then
   1.116 -        if client.send_session then
   1.117 -          assert(conn:write_nb("SESSION:" .. client.session .. "\n"))
   1.118 -          client.send_session = false
   1.119 -        end
   1.120 -        if client.send_name then
   1.121 -          assert(conn:write_nb("NAME:" .. client.name .. "\n"))
   1.122 -          client.send_name = false
   1.123 -        end
   1.124 -        if client.last_msg < last_msg then
   1.125 -          for i = client.last_msg + 1, last_msg do
   1.126 -            assert(conn:write_nb("MSG:" .. msgs[i].name .. " " .. msgs[i].msg .. "\n"))
   1.127 -          end
   1.128 -          client.last_msg = last_msg
   1.129 -        end
   1.130 -        assert(conn:write_nb("TIME:" .. os.time() .. "\n"))
   1.131 -        local bytes_left = assert(conn:flush_nb())
   1.132 -        if bytes_left > 0 then
   1.133 -          io_writer[conn] = true
   1.134 -        else
   1.135 -          io_writer[conn] = false
   1.136 -        end
   1.137 -      end
   1.138 -      
   1.139 -    end
   1.140 -    
   1.141 -    moonbridge_io.poll(io_listener, io_writer, 5)
   1.142 -    
   1.143 -  end
   1.144 -  
   1.145 -end
   1.146 -
   1.147 -listen{
   1.148 -  { proto = "main" },
   1.149 -  connect = chat_server
   1.150 -}
   1.151 -
   1.152  listen{
   1.153    -- listen to a tcp version 4 socket
   1.154    --{ proto = "tcp", host = "0.0.0.0", port = 8080 },
   1.155 @@ -217,22 +79,6 @@
   1.156            request:send_status("303 See Other")
   1.157            request:send_header("Location", "http://" .. request.headers_value.host .. "/example_webpage.html")
   1.158  
   1.159 -        elseif request.path == "chat" then
   1.160 -          request:send_status("200 OK")
   1.161 -          request:send_header("Content-Type", "text/chat; charset=UTF-8")
   1.162 -          request:send_data("MULTIUSERCHAT:protocol version 1\n")
   1.163 -          
   1.164 -          local conn = assert(moonbridge_io.tcpconnect("localhost", 8081))
   1.165 -          
   1.166 -          conn:write("events\n")
   1.167 -          conn:flush()
   1.168 -          
   1.169 -          while true do
   1.170 -            local line = conn:read(nil, "\n")
   1.171 -            request:send_data(line)
   1.172 -            request:flush()
   1.173 -          end
   1.174 -          
   1.175          else
   1.176            local document_name = request.path
   1.177            local document_extension = string.match(document_name, "%.([^.])$")
   1.178 @@ -274,7 +120,7 @@
   1.179            end
   1.180            
   1.181            request:send_status("200 OK")
   1.182 -          request:send_header("Content-Type", "text/html; chatset=UTF-8")
   1.183 +          request:send_header("Content-Type", "text/html; charset=UTF-8")
   1.184            request:send_data("<html>\n<head>\n")
   1.185            request:send_data('<link href="example_webpage.css" rel="stylesheet" type="text/css">\n')
   1.186            request:send_data("<title>Moonbridge Network Server for Lua Applications &ndash; Example Application</title>\n")
   1.187 @@ -294,37 +140,6 @@
   1.188            request:send_data("<p>Submitted comment: ", http.encode_html(request.post_params.comment), "</p>\n")
   1.189            request:send_data("</body>\n</html>\n")
   1.190  
   1.191 -        elseif request.path == "chat_send" then
   1.192 -          local conn = assert(moonbridge_io.tcpconnect("localhost", 8081))
   1.193 -          local body = request.body
   1.194 -          local session
   1.195 -          for line in body:gmatch("[^\r\n]+") do
   1.196 -            local command, arg = line:match("([^:]+):(.*)")
   1.197 -            if not command then
   1.198 -              -- TODO error handling
   1.199 -              return
   1.200 -            elseif command == "SESSION" then
   1.201 -              session = arg
   1.202 -            else
   1.203 -              if not session then
   1.204 -                -- TODO error handling
   1.205 -                return
   1.206 -              end
   1.207 -              conn:write(session .. "\n")
   1.208 -              if command == "NAME" then
   1.209 -                local name = arg:gsub(" ", "_")
   1.210 -                conn:write("NAME:" .. name .. "\n")
   1.211 -              elseif command == "MSG" then
   1.212 -                local msg = arg
   1.213 -                conn:write("MSG:" .. msg .. "\n")
   1.214 -              end
   1.215 -            end
   1.216 -          end
   1.217 -          conn:flush()
   1.218 -          conn:close()
   1.219 -          request:send_status("200 OK")
   1.220 -          request:send_header("Content-Type", "text/xml; chatset=UTF-8")
   1.221 -
   1.222          else
   1.223            error_response("404 Not Found")
   1.224  
     2.1 --- a/example_chat.html	Fri Jun 19 20:20:48 2015 +0200
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,134 +0,0 @@
     2.4 -<html lang="en">
     2.5 -  <head>
     2.6 -    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     2.7 -    <link href="example_webpage.css" rel="stylesheet" type="text/css">
     2.8 -    <title>Moonbridge Network Server for Lua Applications &ndash; Example Application</title>
     2.9 -  </head>
    2.10 -  <body>
    2.11 -    <h1>Moonbridge Network Server for Lua &ndash; Example Application</h1>
    2.12 -    <h2>Multiuser chat</h2>
    2.13 -    <div id="status">Connecting to server...</div>
    2.14 -    <div id="chat">
    2.15 -    </div>
    2.16 -    <form action="#" onsubmit="chatSend(); return false;">
    2.17 -      Message: <textarea id="msg" name="message" cols="80" rows="3"></textarea><br />
    2.18 -      Name: <input type="text" name="name" id="name">
    2.19 -      <input type="submit">
    2.20 -      Current server time: <span id="time">...</span>
    2.21 -    </form>
    2.22 -    
    2.23 -    <script>
    2.24 -
    2.25 -      function xmlhttprpc() {
    2.26 -        var req;
    2.27 -        try {
    2.28 -          req = new XMLHttpRequest();
    2.29 -        } catch(e) { try {
    2.30 -          req = new ActiveXObject("Microsoft.XMLHTTP");
    2.31 -        } catch(e) { try {
    2.32 -          req  = new ActiveXObject("Msxml2.XMLHTTP");
    2.33 -        } catch(e) { } } }
    2.34 -
    2.35 -        return req;
    2.36 -      }
    2.37 -
    2.38 -      var chatName;
    2.39 -      var chatSession;
    2.40 -    
    2.41 -      function chatPrint(name, msg) {
    2.42 -        var chatEl = document.getElementById("chat");
    2.43 -        var el = document.createElement("div");
    2.44 -        el.className = "line";
    2.45 -        var nameEl = document.createElement("span");
    2.46 -        nameEl.className = "name";
    2.47 -        var msgEl = document.createElement("span");
    2.48 -        msgEl.className = "msg";
    2.49 -        nameEl.innerHTML = name;
    2.50 -        msgEl.innerHTML = msg;
    2.51 -        el.appendChild(nameEl);
    2.52 -        el.appendChild(msgEl);
    2.53 -        chatEl.appendChild(el);
    2.54 -      }
    2.55 -      
    2.56 -      function chatSetSession(session) {
    2.57 -        chatSession = session;
    2.58 -        document.getElementById("status").innerHTML = "connected";
    2.59 -        document.getElementById("msg").focus();
    2.60 -      }
    2.61 -      
    2.62 -      function chatSetName(name) {
    2.63 -        var nameEl = document.getElementById("name");
    2.64 -        nameEl.value = name;
    2.65 -        chatName = name;
    2.66 -        document.getElementById("msg").focus();
    2.67 -      }
    2.68 -    
    2.69 -      function chatSetTime(time) {
    2.70 -        var timeEl = document.getElementById("time");
    2.71 -        timeEl.innerHTML = time;
    2.72 -      }
    2.73 -      
    2.74 -      function chatSend() {
    2.75 -        var req = xmlhttprpc();
    2.76 -        req.open('POST', 'chat_send', true);
    2.77 -        req.onreadystatechange = function() { console.log(req.responseText);};
    2.78 -        var name = document.getElementById("name").value;
    2.79 -        var msg = document.getElementById("msg").value;
    2.80 -        var datagram = "SESSION:" + chatSession + "\n"
    2.81 -        if (name != chatName) {
    2.82 -          datagram += "NAME:" + name + "\n";
    2.83 -        }
    2.84 -        datagram +=  "MSG:" + msg.replace(/\n/g, "<br />") + "\n";
    2.85 -        req.send(datagram);
    2.86 -        document.getElementById("msg").value = "";
    2.87 -      }
    2.88 -    
    2.89 -      function chat() {
    2.90 -      
    2.91 -        document.getElementById("msg").focus();
    2.92 -      
    2.93 -        var req = xmlhttprpc();
    2.94 -        req.open('GET', 'chat', true);
    2.95 -        
    2.96 -        var processedChars = 0;
    2.97 -        
    2.98 -        req.onreadystatechange = function () {
    2.99 -          
   2.100 -          if (req.readyState == 3 || req.readyStateChange == 4) {
   2.101 -            var re = new RegExp("([^\r\n]+)\r?\n", "g")
   2.102 -            var unprocessedResponseText = req.responseText.substring(processedChars);
   2.103 -            while ((line = re.exec(unprocessedResponseText)) !== null) {
   2.104 -              var line = line[1];
   2.105 -              processedChars += line.length + 1;
   2.106 -              var parts = line.split(":");
   2.107 -              var command = parts[0];
   2.108 -              var arg = parts.slice(1).join(':');
   2.109 -              if (command == "MULTIUSERCHAT") {
   2.110 -              } else if (command == "SESSION") {
   2.111 -                chatSetSession(arg);
   2.112 -              } else if (command == "NAME") {
   2.113 -                chatSetName(arg);
   2.114 -              } else if (command == "TIME") {
   2.115 -                chatSetTime(arg);
   2.116 -              } else if (command == "MSG") {
   2.117 -                var parts = arg.split(" ");
   2.118 -                var from = parts[0];
   2.119 -                var msg = parts.slice(1).join(" ");
   2.120 -                chatPrint(from, msg);
   2.121 -              } else {
   2.122 -                chatPrint("SERVER" + arg)
   2.123 -              }
   2.124 -            }
   2.125 -          }
   2.126 -          
   2.127 -          
   2.128 -        }
   2.129 -        req.send();
   2.130 -      }
   2.131 -      
   2.132 -      chat();
   2.133 -      
   2.134 -    </script>
   2.135 -    
   2.136 -  </body>
   2.137 -</html>

Impressum / About Us