This repository has been archived on 2024-11-11. You can view files and clone it, but cannot push or open issues or pull requests.
socketio-jwt/example/public/index.html

65 lines
1.7 KiB
HTML
Raw Normal View History

2014-01-14 15:04:41 +01:00
<!DOCTYPE html>
<html>
<head>
<title>Socket.io with JWTs</title>
</head>
<body>
<form id="login">
<input type="text" id="username" />
<input type="password" id="password" />
<input type="submit">
</form>
<div>
<button id="ping">Send Socket.io Ping</button>
</div>
<script src="//code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
var token, socket;
function connect () {
socket = io.connect('', {
'force new connection': true,
query: token ? 'token=' + token : undefined
});
socket.on('pong', function () {
console.log('- pong');
}).on('time', function (data) {
console.log('- broadcast: ' + data);
}).on('authenticated', function () {
console.log('- authenticated');
}).on('disconnect', function () {
console.log('- disconnected');
});
}
connect(); //connect now, it will drop
$('#ping').on('click', function () {
console.log('- ping');
socket.emit('ping');
});
$('#login').submit(function (e) {
e.preventDefault();
var username = $('username').val();
var password = $('password').val();
$.ajax({
type: 'POST',
data: {
username: username,
password: password
},
url: '/login'
}).done(function (result) {
token = result.token;
connect();
});
});
</script>
</body>
</html>