2014-06-10 00:25:08 +08:00
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<html lang="en">
|
|
|
|
|
<head>
|
|
|
|
|
<meta charset="utf-8" />
|
|
|
|
|
<title>WebSocket Test</title>
|
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
|
|
|
<style type="text/css">
|
|
|
|
|
body {
|
2015-09-08 19:49:03 +08:00
|
|
|
|
background-color: #789; margin: 0;
|
2014-06-10 00:25:08 +08:00
|
|
|
|
padding: 0; font: 14px Helvetica, Arial, sans-serif;
|
|
|
|
|
}
|
|
|
|
|
div.content {
|
|
|
|
|
width: 800px; margin: 2em auto; padding: 20px 50px;
|
|
|
|
|
background-color: #fff; border-radius: 1em;
|
|
|
|
|
}
|
|
|
|
|
#messages {
|
|
|
|
|
border: 2px solid #fec; border-radius: 1em;
|
|
|
|
|
height: 10em; overflow: scroll; padding: 0.5em 1em;
|
|
|
|
|
}
|
|
|
|
|
a:link, a:visited { color: #69c; text-decoration: none; }
|
|
|
|
|
@media (max-width: 700px) {
|
|
|
|
|
body { background-color: #fff; }
|
|
|
|
|
div.content {
|
|
|
|
|
width: auto; margin: 0 auto; border-radius: 0;
|
|
|
|
|
padding: 1em;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
|
|
|
|
|
<script language="javascript" type="text/javascript">
|
|
|
|
|
|
|
|
|
|
var rooms = [];
|
|
|
|
|
var ws = new WebSocket('ws://' + location.host + '/ws');
|
|
|
|
|
|
|
|
|
|
if (!window.console) { window.console = { log: function() {} } };
|
|
|
|
|
|
|
|
|
|
ws.onopen = function(ev) { console.log(ev); };
|
|
|
|
|
ws.onerror = function(ev) { console.log(ev); };
|
|
|
|
|
ws.onclose = function(ev) { console.log(ev); };
|
|
|
|
|
ws.onmessage = function(ev) {
|
|
|
|
|
console.log(ev);
|
2015-09-08 19:49:03 +08:00
|
|
|
|
var div = document.createElement('div');
|
|
|
|
|
div.innerHTML = ev.data;
|
|
|
|
|
document.getElementById('messages').appendChild(div);
|
|
|
|
|
|
2014-06-10 00:25:08 +08:00
|
|
|
|
};
|
2014-09-09 06:29:34 +08:00
|
|
|
|
|
2014-06-10 00:25:08 +08:00
|
|
|
|
window.onload = function() {
|
|
|
|
|
document.getElementById('send_button').onclick = function(ev) {
|
|
|
|
|
var msg = document.getElementById('send_input').value;
|
2015-09-08 19:49:03 +08:00
|
|
|
|
document.getElementById('send_input').value = '';
|
|
|
|
|
ws.send(msg);
|
2014-06-10 00:25:08 +08:00
|
|
|
|
};
|
2015-09-08 19:49:03 +08:00
|
|
|
|
document.getElementById('send_input').onkeypress = function(ev) {
|
|
|
|
|
if (ev.keyCode == 13 || ev.which == 13) {
|
|
|
|
|
document.getElementById('send_button').click();
|
|
|
|
|
}
|
2014-06-10 00:25:08 +08:00
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
</script>
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<div class="content">
|
|
|
|
|
<h1>Websocket PubSub Demonstration</h1>
|
|
|
|
|
|
|
|
|
|
<p>
|
2015-09-08 19:49:03 +08:00
|
|
|
|
This page demonstrates how Mongoose could be used to implement
|
2014-06-10 00:25:08 +08:00
|
|
|
|
<a href="http://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern">
|
|
|
|
|
publish–subscribe pattern</a>. Open this page in several browser
|
|
|
|
|
windows. Each window initiates persistent
|
|
|
|
|
<a href="http://en.wikipedia.org/wiki/WebSocket">WebSocket</a>
|
2015-09-08 19:49:03 +08:00
|
|
|
|
connection with the server, making each browser window a websocket client.
|
|
|
|
|
Send messages, and see messages sent by other clients.
|
2014-06-10 00:25:08 +08:00
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<div id="messages">
|
|
|
|
|
</div>
|
2014-09-09 06:29:34 +08:00
|
|
|
|
|
2014-06-10 00:25:08 +08:00
|
|
|
|
<p>
|
|
|
|
|
<input type="text" id="send_input" />
|
|
|
|
|
<button id="send_button">Send Message</button>
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|