Skip to content

Instantly share code, notes, and snippets.

@uorat
Created September 24, 2016 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uorat/35023ff98c7e1e42973c804df9301a46 to your computer and use it in GitHub Desktop.
Save uorat/35023ff98c7e1e42973c804df9301a46 to your computer and use it in GitHub Desktop.
Sample client code for Socket.IO w/ ALB
<html>
<head>
<title>hello node.js!</title>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://your.domain.or.ip/socket.io/socket.io.js"></script>
<script>
$(document).ready(function(){
var socket = io.connect('https://your.domain.or.ip');
socket.on('connect', function() {
console.log("connect [sessid = " + socket.id + "]");
});
socket.on('message', function(data) {
console.log('data = ' + data.value);
$('div#comment').append('<div>' + data.value + ' [sessid = ' + data.sessid + ']</div>');
});
socket.on('disconnect', function() { console.log('exit !!'); });
$(':button[name=buttonMsg]').click(function() {
var msg = $(':text[name=message]').val();
socket.emit('message', { value: msg, sessid: socket.id });
});
$(':button[name=buttonDiscon]').click(function() {
var msg = "Disconnected [sessid = " + socket.id + "]";
socket.emit('message', { value: msg, sessid: socket.id });
socket.disconnect();
});
});
</script>
</head>
<body>
<input type="text" name="message" value="">
<input type="button" name="buttonMsg" value="send message">
<input type="button" name="buttonDiscon" value="disconnect">
<h1>comment</h1>
<div id="comment">
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment