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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
<!DOCTYPE html>
<html>
<head>
<title>WebSocket example</title>
<meta charset="utf-8" />
<style type="text/css">
body {
width: 800px;
margin: auto;
}
header {
font-size: 36pt;
width: 100%;
text-align: center;
margin-bottom: 1em;
color: #541F14;
}
section.proto {
float: left;
/*background-color: #f8f8f8;*/
}
section#json {
width: 380px;
margin-right: 20px;
}
section#raw {
width: 380px;
padding-left: 16px;
border-left: 4px solid #626266;
}
div.desc {
margin: 0px;
}
pre.sent, pre.received {
margin-top: 0px;
border-radius: 4px;
padding: 5px;
}
pre.sent {
border: 1px solid #938172;
background-color: white;
}
pre.received {
border: 1px solid #CC9E61;
text-align: right;
}
</style>
</head>
<body>
<header>Webdis with HTML5 WebSockets</header>
<section class="proto" id="json">
<h3>JSON</h3>
<div class="log" id="json-log">
Connecting...
</div>
</section>
<section class="proto" id="raw">
<h3>Raw</h3>
<div class="log" id="raw-log">
Connecting...
</div>
</section>
<script type="text/javascript">
$ = function(id) {return document.getElementById(id);};
var host = "127.0.0.1";
var port = 7379;
function log(id, dir, msg) {
var desc = document.createElement("div");
desc.setAttribute("class", "desc");
desc.innerHTML = dir;
$(id).appendChild(desc);
var e = document.createElement("pre");
e.setAttribute("class", dir);
e.innerHTML = msg;
$(id).appendChild(e);
}
function testJSON() {
if(typeof(WebSocket) == 'function')
f = WebSocket;
if(typeof(MozWebSocket) == 'function')
f = MozWebSocket;
var jsonSocket = new f("ws://"+host+":"+port+"/.json");
var self = this;
send = function(j) {
var json = JSON.stringify(j);
jsonSocket.send(json);
log("json-log", "sent", json);
};
jsonSocket.onopen = function() {
$("json-log").innerHTML = "";
self.send(["SET", "hello", "world"]);
self.send(["GET", "hello"]);
};
jsonSocket.onmessage = function(messageEvent) {
log("json-log", "received", messageEvent.data);
};
}
function testRAW() {
if(typeof(WebSocket) == 'function')
f = WebSocket;
if(typeof(MozWebSocket) == 'function')
f = MozWebSocket;
var rawSocket = new f("ws://"+host+":"+port+"/.raw");
var self = this;
sendRaw = function(raw) {
rawSocket.send(raw);
log("raw-log", "sent", raw);
};
rawSocket.onopen = function() {
$("raw-log").innerHTML = "";
self.sendRaw("*3\r\n$3\r\nSET\r\n$5\r\nhello\r\n$5\r\nworld\r\n");
self.sendRaw("*2\r\n$3\r\nGET\r\n$5\r\nhello\r\n");
};
rawSocket.onmessage = function(messageEvent) {
log("raw-log", "received", messageEvent.data);
};
}
addEventListener("DOMContentLoaded", function(){
testJSON();
testRAW();
}, null);
</script>
</body>
</html>
|