moonbridge

diff example_application.lua @ 194:822ccaeccccb

Removed example chat application
author jbe
date Sat Jun 20 00:00:32 2015 +0200 (2015-06-20)
parents 5adfc36ca73f
children bae067af56d4
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  

Impressum / About Us