File: test.lua

package info (click to toggle)
lua-event 0.4.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 260 kB
  • sloc: ansic: 741; makefile: 40; sh: 1
file content (35 lines) | stat: -rw-r--r-- 834 bytes parent folder | download | duplicates (6)
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
-- Tests Copas with a simple Echo server
--
-- Run the test file and the connect to the server by telnet on the used port
-- to stop the test just send the command "quit"

local luaevent = require("luaevent")
local socket = require("socket")

local oldPrint = print
print = function(...)
	oldPrint("SRV", ...)
end

local function echoHandler(skt)
  while true do
    local data,ret = luaevent.receive(skt, 10)
    --print("GOT: ", data, ret)
    if data == "quit" or ret == 'closed' then
      break
    end
    luaevent.send(skt, data)
    collectgarbage()
  end
  skt:close()
  --print("DONE")
end
local server = assert(socket.bind("localhost", 20000))
server:settimeout(0)
local coro = coroutine.create
coroutine.create = function(...)
	local ret = coro(...)
	return ret
end
luaevent.addserver(server, echoHandler)
luaevent.loop()