2014-01-23 10:36:08 +00:00
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<meta charset="utf-8" />
|
|
|
|
|
<title>WebSocket Test</title>
|
|
|
|
|
<script language="javascript" type="text/javascript">
|
|
|
|
|
|
|
|
|
|
var out = function(message) {
|
|
|
|
|
var div = document.createElement('div');
|
|
|
|
|
div.innerHTML = message;
|
|
|
|
|
document.getElementById('output').appendChild(div);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
window.onload = function() {
|
|
|
|
|
var url = 'ws://' + location.host + '/ws';
|
|
|
|
|
var num_messages = 0;
|
|
|
|
|
|
|
|
|
|
websocket = new WebSocket(url);
|
|
|
|
|
websocket.onopen = function(ev) {
|
|
|
|
|
out('CONNECTED');
|
|
|
|
|
var msg = 'Не всё подчиняется разуму. Но всё подчиняется упорству. ';
|
|
|
|
|
out('SENT: ' + msg);
|
|
|
|
|
websocket.send(msg);
|
|
|
|
|
};
|
|
|
|
|
websocket.onclose = function(ev) {
|
|
|
|
|
out('DISCONNECTED');
|
|
|
|
|
};
|
|
|
|
|
websocket.onmessage = function(ev) {
|
2014-01-28 12:42:13 +00:00
|
|
|
|
if (!ev.data) {
|
|
|
|
|
out('<span style="color: blue;">PING... </span>');
|
|
|
|
|
} else {
|
|
|
|
|
out('<span style="color: blue;">RESPONSE: ' + ev.data + ' </span>');
|
|
|
|
|
num_messages++;
|
|
|
|
|
}
|
2014-02-07 13:03:28 +00:00
|
|
|
|
if (num_messages > 3) {
|
2014-01-23 10:36:08 +00:00
|
|
|
|
websocket.send('exit');
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
websocket.onerror = function(ev) {
|
|
|
|
|
out('<span style="color: red; ">ERROR: </span> ' + ev.data);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
</script>
|
|
|
|
|
<style> div {font: small Verdana; } </style>
|
|
|
|
|
<h2>Mongoose WebSocket Test</h2>
|
|
|
|
|
|
|
|
|
|
<div id="output"></div>
|
|
|
|
|
</html>
|