mirror of
https://github.com/rwf2/Rocket.git
synced 2025-01-14 13:39:13 +00:00
bd482081ad
This is an initial example that showcases using the new connection upgrade API to implement WebSocket support outside of Rocket's core.
70 lines
1.4 KiB
HTML
70 lines
1.4 KiB
HTML
<!DOCTYPE html>
|
|
|
|
<head>
|
|
<title>WebSocket client test</title>
|
|
</head>
|
|
|
|
<body>
|
|
<h1>WebSocket Client Test</h1>
|
|
<div id="log"></div>
|
|
</body>
|
|
|
|
<script language="javascript" type="text/javascript">
|
|
|
|
var wsUri = "ws://127.0.0.1:8000/echo/";
|
|
var log;
|
|
|
|
function init()
|
|
{
|
|
log = document.getElementById("log");
|
|
testWebSocket();
|
|
}
|
|
|
|
function testWebSocket()
|
|
{
|
|
websocket = new WebSocket(wsUri);
|
|
websocket.onopen = function(evt) { onOpen(evt) };
|
|
websocket.onclose = function(evt) { onClose(evt) };
|
|
websocket.onmessage = function(evt) { onMessage(evt) };
|
|
websocket.onerror = function(evt) { onError(evt) };
|
|
}
|
|
|
|
function onOpen(evt)
|
|
{
|
|
writeLog("CONNECTED");
|
|
sendMessage("Hello world");
|
|
}
|
|
|
|
function onClose(evt)
|
|
{
|
|
writeLog("Websocket DISCONNECTED");
|
|
}
|
|
|
|
function onMessage(evt)
|
|
{
|
|
writeLog('<span style="color: blue;">RESPONSE: ' + evt.data+'</span>');
|
|
websocket.close();
|
|
}
|
|
|
|
function onError(evt)
|
|
{
|
|
writeLog('<span style="color: red;">ERROR:</span> ' + evt.data);
|
|
}
|
|
|
|
function sendMessage(message)
|
|
{
|
|
writeLog("SENT: " + message);
|
|
websocket.send(message);
|
|
}
|
|
|
|
function writeLog(message)
|
|
{
|
|
var pre = document.createElement("p");
|
|
pre.innerHTML = message;
|
|
log.appendChild(pre);
|
|
}
|
|
|
|
window.addEventListener("load", init, false);
|
|
|
|
</script>
|