Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
55 lines (46 sloc) 1.62 KB
<html>
<head>
<title>Chat example</title>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js"></script>
</head>
<body>
<input id="text" type="text" />
<input id="send" type="submit" />
<div id="chat-log"></div>
</body>
<script>
$(function() {
var is_secure = (window.location.protocol === "https:");
var ws = new WebSocket((is_secure ? "wss://" : "ws://") + document.location.host + "/ws/");
ws.onopen = function (event) {
console.log('READY');
$('div#chat-log').prepend('<p>Connected to chat server</p>');
};
ws.onmessage = function (event) {
var msg = JSON.parse(event.data);
console.log(msg);
if ('hello' in msg) {
$('div#chat-log').prepend('<p>' + msg['hello'] + ' has joined the chat</p>');
} else if ('goodbye' in msg) {
$('div#chat-log').prepend('<p>' + msg['goodbye'] + ' has left the chat</p>');
} else if ('name' in msg) {
$('div#chat-log').prepend('<p>' + msg['name']['old'] + ' is now known as ' + msg['name']['new'] + '</p>');
} else if ('say' in msg) {
$('div#chat-log').prepend('<p>' + msg['say']['name'] + ': ' + msg['say']['msg'] + '</p>');
}
};
$('input#send').click(function() {
var cmd = $('input#text').val();
var msg = {};
if (cmd.match('^/name')) {
msg['name'] = cmd.split(' ').slice(1).join(' ');
} else {
msg['say'] = cmd;
}
console.log('SEND: ' + msg);
ws.send(JSON.stringify(msg));
$('input#text').val('');
});
});
</script>
</html>
You can’t perform that action at this time.