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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
# Simple example for websockets, start this script with httest and connect
# a browser to http://localhost:8888/
#
# By Christian Liesch <liesch@gmx.ch>
# To make a client happy we have to encrypt the sec key from client
# @param websocketKey IN websocket key from client
# @return result IN sha1 of websocket key
BLOCK:LUA WebsocketAccept websocketKey : result
return crypto.base64.encode(crypto.evp.digest("sha1", websocketKey.."258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true))
END
# Server delivering the html page for the browser
SERVER 8888
_RES
_WAIT
__HTTP/1.1 200 OK
__Content-Type: text/html
__Content-Length: AUTO
__
__<!DOCTYPE HTML>
__<html>
__<head>
__<script type="text/javascript">
__function WebSocketTest()
__{
__ if ("WebSocket" in window)
__ {
__ alert("WebSocket is supported by your Browser!");
__ // Let us open a web socket
__ var ws = new WebSocket("ws://localhost:9876/echo");
__ ws.onopen = function()
__ {
__ // Web Socket is connected, send data using send()
__ ws.send("Message to send");
__ alert("Message is sent...");
__ };
__ ws.onmessage = function (evt)
__ {
__ var received_msg = evt.data;
__ alert("Message is received..."+evt.data);
__ };
__ ws.onclose = function()
__ {
__ // websocket is closed.
__ alert("Connection is closed...");
__ };
__ }
__ else
__ {
__ // The browser doesnt support WebSocket
__ alert("WebSocket NOT supported by your Browser!");
__ }
__}
__</script>
__</head>
__<body>
__<div id="sse">
__ <a href="javascript:WebSocketTest()">Run WebSocket</a>
__</div>
__</body>
__</html>
__
END
# Server handling the websocket stuff
SERVER 9876
_RES
_MATCH headers "Sec-WebSocket-Key: (.*)" WebsocketKey
_WAIT
__HTTP/1.1 101 Switching Protocols
__Upgrade: websocket
__Connection: Upgrade
__Sec-WebSocket-Accept: $WebsocketAccept($WebsocketKey)
__Sec-WebSocket-Protocol: chat
__
_FLUSH
_WS:RECV
_WS:SEND FIN,TEXT AUTO "hallo welt"
_CLOSE
END
|