You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A webserver is NOT part of lua-websockets. If you are looking for a feature rich webserver framework, have a look at [orbit](http://keplerproject.github.com/orbit/) or others. It is no problem to work with a "normal" webserver and lua-websockets side by side (two processes, different ports), since websockets are not subject of the 'Same origin policy'.
26
-
27
-
# Usage
28
-
## copas echo server
29
-
This implements a basic echo server via Websockets protocol. Once you are connected with the server, all messages you send will be returned ('echoed') by the server immediately.
-- this callback is called, whenever a new client connects.
44
-
-- ws is a new websocket instance
45
-
echo=function(ws)
46
-
whiletruedo
47
-
localmessage=ws:receive()
48
-
ifmessagethen
49
-
ws:send(message)
50
-
else
51
-
ws:close()
52
-
return
53
-
end
54
-
end
55
-
end
56
-
}
57
-
}
58
-
59
-
-- use the copas loop
60
-
copas.loop()
61
-
```
62
-
63
-
## lua-ev echo server
64
-
This implements a basic echo server via Websockets protocol. Once you are connected with the server, all messages you send will be returned ('echoed') by the server immediately.
65
-
66
-
```lua
67
-
localev=require'ev'
68
-
69
-
-- create a copas webserver and start listening
70
-
localserver=require'websocket'.server.ev.listen
71
-
{
72
-
-- listen on port 8080
73
-
port=8080,
74
-
-- the protocols field holds
75
-
-- key: protocol name
76
-
-- value: callback on new connection
77
-
protocols= {
78
-
-- this callback is called, whenever a new client connects.
79
-
-- ws is a new websocket instance
80
-
echo=function(ws)
81
-
ws:on_message(function(ws,message)
82
-
ws:send(message)
83
-
end)
84
-
85
-
-- this is optional
86
-
ws:on_close(function()
87
-
ws:close()
88
-
end)
89
-
end
90
-
}
91
-
}
92
-
93
-
-- use the lua-ev loop
94
-
ev.Loop.default:loop()
95
-
96
-
```
97
-
98
-
## Running test-server examples
99
-
100
-
The folder test-server contains two re-implementations of the [libwebsocket](http://git.warmcat.com/cgi-bin/cgit/libwebsockets/) test-server.c example.
101
-
102
-
```shell
103
-
cd test-server
104
-
lua test-server-ev.lua
105
-
```
106
-
107
-
```shell
108
-
cd test-server
109
-
lua test-server-copas.lua
110
-
```
111
-
112
-
Connect to the from Javascript (e.g. chrome's debugging console) like this:
113
-
```Javascript
114
-
var echoWs =newWebSocket('ws://127.0.0.1:8002','echo');
0 commit comments