kore/examples/websocket/assets/frontend.html
Joris Vink f867882f43 Add websocket support to Kore.
Introduces a few new api functions:

- kore_websocket_handshake(struct http_request *):
	Performs the handshake on an HTTP request (coming from page handler)

- kore_websocket_send(struct connection *, u_int8_t, void *, size_t):
	Sends data to a websocket connection.

- kore_websocket_broadcast(struct connection *, u_int8_t, void *, size_t, int):
	Broadcast the given websocket op and data to all connected
	websocket clients on the worker. Note that as of right now
	the WEBSOCKET_BROADCAST_GLOBAL scope option does not work
	yet and messages broadcasted will be restricted to workers
	only.

- kore_worker_websocket_broadcast(struct connection *, void *, void *):
	Backend function used by kore_websocket_broadcast().
	Could prove useful for developers to have access to.

A simple example is given under examples/websocket.

Known issues:
	Kore does not support PING or CONT frames just yet.
2014-11-24 11:08:34 +01:00

61 lines
1.0 KiB
HTML

<!DOCTYPE>
<html>
<head>
<script>
var socket = null;
var sent = 0;
var recv = 0;
var length = 65536;
function open(evt) {
var msg = "";
var alphabet = "abcdefghijklmnopqrstuvwxyz";
for (i = 0; i < length; i++)
msg += alphabet.charAt(Math.floor(Math.random() * alphabet.length));
message(msg);
}
function message(msg) {
socket.send(msg);
sent = sent + 1;
update();
}
function update() {
var cnt = document.getElementById("counter");
cnt.innerHTML = "Recv: " + recv + " Sent: " + sent;
}
function onmessage(evt) {
recv = recv + 1;
update();
message(evt.data);
}
function connect() {
socket = new WebSocket("wss://127.0.0.1:8888/connect");
socket.onopen = function(evt) { open(evt) };
socket.onclose = function(evt) { alert("closed"); };
socket.onmessage = function(evt) { onmessage(evt) };
socket.onerror = function(evt) { alert("onerror"); };
}
</script>
</head>
<body>
<form action="/" onsubmit="connect(); return false;">
<input type="submit" value="connect">
</form>
<div id="counter">
</div>
</body>
</html>