Skip to content

Instantly share code, notes, and snippets.

@fortizc
Created November 16, 2015 15:22
Show Gist options
  • Save fortizc/a1a6c742fb41ea6a2b8a to your computer and use it in GitHub Desktop.
Save fortizc/a1a6c742fb41ea6a2b8a to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
window.WebSocket = window.WebSocket || window.MozWebSocket;
var websocket = new WebSocket('ws://127.0.0.1:9000', 'chat-protocol');
websocket.onopen = function () {
$('h1').css('color', 'green');
};
websocket.onerror = function () {
$('h1').css('color', 'red');
};
websocket.onmessage = function (message) {
console.log(message.data);
$('div').append(message.data + '<br/>');
};
$('#set').click(function(e) {
e.preventDefault();
websocket.send($('#nick').val()+'@nick');
});
$('#send').click(function(e) {
e.preventDefault();
if ($('#txt').val().length > 0)
{
websocket.send($('#txt').val());
$('#txt').val('');
}
});
});
</script>
</head>
<body>
<h1>WebSockets test</h1>
<form>
Msg:&nbsp;
<input type="text" id="txt" />
<button id="send">Send</button>
<br/>
<br/>
Nick:
<input type="text" id="nick" />
<button id="set">Set</button>
</form>
<br/>
<div></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment