Skip to content

Commit

Permalink
add default handler
Browse files Browse the repository at this point in the history
  • Loading branch information
lipp committed May 23, 2013
1 parent c61001e commit 5b8ff0b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
29 changes: 17 additions & 12 deletions examples/echo-server-copas.lua
@@ -1,5 +1,19 @@
local copas = require'copas'

-- this callback is called, whenever a new client connects.
-- ws is a new websocket instance
local echo_handler = function(ws)
while true do
local message = ws:receive()
if message then
ws:send(message)
else
ws:close()
return
end
end
end

-- create a copas webserver and start listening
local server = require'websocket'.server.copas.listen
{
Expand All @@ -11,18 +25,9 @@ local server = require'websocket'.server.copas.listen
protocols = {
-- this callback is called, whenever a new client connects.
-- ws is a new websocket instance
echo = function(ws)
while true do
local message = ws:receive()
if message then
ws:send(message)
else
ws:close()
return
end
end
end
}
echo = echo_handler
},
default = echo_handler
}

-- use the copas loop
Expand Down
19 changes: 11 additions & 8 deletions examples/echo-server-ev.lua
@@ -1,5 +1,13 @@
local ev = require'ev'

-- this callback is called, whenever a new client connects.
-- ws is a new websocket instance
local echo_handler = function(ws)
ws:on_message(function(ws,message)
ws:send(message)
end)
end

-- create a copas webserver and start listening
local server = require'websocket'.server.ev.listen
{
Expand All @@ -9,14 +17,9 @@ local server = require'websocket'.server.ev.listen
-- key: protocol name
-- value: callback on new connection
protocols = {
-- this callback is called, whenever a new client connects.
-- ws is a new websocket instance
echo = function(ws)
ws:on_message(function(ws,message)
ws:send(message)
end)
end
}
echo = echo_handler
},
default = echo_handler
}

-- use the lua-ev loop
Expand Down

0 comments on commit 5b8ff0b

Please sign in to comment.