| 1 | -- Tests Copas with a simple Echo server |
| 2 | -- |
| 3 | -- Run the test file and the connect to the server by telnet on the used port |
| 4 | -- to stop the test just send the command "quit" |
| 5 | |
| 6 | require"luaevent" |
| 7 | require"socket" |
| 8 | local oldPrint = print |
| 9 | print = function(...) |
| 10 | oldPrint("SRV", ...) |
| 11 | end |
| 12 | |
| 13 | local function echoHandler(skt) |
| 14 | while true do |
| 15 | local data,ret = luaevent.receive(skt, 10) |
| 16 | --print("GOT: ", data, ret) |
| 17 | if data == "quit" or ret == 'closed' then |
| 18 | break |
| 19 | end |
| 20 | luaevent.send(skt, data) |
| 21 | collectgarbage() |
| 22 | end |
| 23 | skt:close() |
| 24 | --print("DONE") |
| 25 | end |
| 26 | local server = assert(socket.bind("localhost", 20000)) |
| 27 | server:settimeout(0) |
| 28 | local coro = coroutine.create |
| 29 | coroutine.create = function(...) |
| 30 | local ret = coro(...) |
| 31 | return ret |
| 32 | end |
| 33 | luaevent.addserver(server, echoHandler) |
| 34 | luaevent.loop() |