Last active
September 19, 2016 10:46
-
-
Save uorat/538ebe191993665767f96f2b2b8251c4 to your computer and use it in GitHub Desktop.
Sample client code for Socket.IO (/usr/share/nginx/html/sample.html)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>hello node.js!</title> | |
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script> | |
<script src="/socket.io/socket.io.js"></script> | |
<script> | |
$(document).ready(function(){ | |
var socket = io.connect(location.host); | |
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