Introduced a new example
This introduces a new example project that build on top of socket.io’s own tutorial. Fixes https://github.com/auth0/socketio-jwt/issues/59
This commit is contained in:
parent
5532ff03fd
commit
31b4c450e0
2
example/.gitignore
vendored
Normal file
2
example/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
node_modules
|
||||
|
88
example/index.html
Normal file
88
example/index.html
Normal file
@ -0,0 +1,88 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Socket.IO chat</title>
|
||||
<style>
|
||||
* { margin: 0; padding: 0; box-sizing: border-box; }
|
||||
body { font: 13px Helvetica, Arial; }
|
||||
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
|
||||
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
|
||||
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
|
||||
#messages { list-style-type: none; margin: 0; padding: 0; }
|
||||
#messages li { padding: 5px 10px; }
|
||||
#messages li:nth-child(odd) { background: #eee; }
|
||||
#login { width: 100%; padding: 10px }
|
||||
#login button { width: 10%; padding: 8px; border: none; background: rgb(130, 255, 130);}
|
||||
</style>
|
||||
<!-- Auth0 lock script -->
|
||||
<script src="//cdn.auth0.com/js/lock-7.5.min.js"></script>
|
||||
|
||||
<!-- Setting the right viewport -->
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
|
||||
</head>
|
||||
<body>
|
||||
<div id="login">
|
||||
<button>Login</button>
|
||||
</div>
|
||||
<div id="chat">
|
||||
<ul id="messages"></ul>
|
||||
<form action="">
|
||||
<input id="m" autocomplete="off" /><button>Send</button>
|
||||
</form>
|
||||
</div>
|
||||
<script src="/socket.io/socket.io.js"></script>
|
||||
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
|
||||
<script>
|
||||
$('#chat').hide();
|
||||
var userProfile;
|
||||
var userToken;
|
||||
$('#login button').click(function(e){
|
||||
e.preventDefault();
|
||||
lock.show(function(err, profile, token) {
|
||||
if (err) {
|
||||
//Error callback
|
||||
alert('There was an error');
|
||||
alert(err);
|
||||
} else {
|
||||
//Success callback
|
||||
userToken = token;
|
||||
|
||||
//Save the JWT token
|
||||
localStorage.setItem('userToken', token);
|
||||
|
||||
//Save the profile
|
||||
userProfile = profile;
|
||||
|
||||
openChat();
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
function openChat(){
|
||||
var socket = io();
|
||||
socket.on('connect', function () {
|
||||
socket.on('authenticated', function () {
|
||||
//Do
|
||||
$('#login').hide();
|
||||
$('#chat').show();
|
||||
$('form').submit(function(event){
|
||||
socket.emit('chat message', $('#m').val());
|
||||
$('#m').val('');
|
||||
return false;
|
||||
});
|
||||
socket.on('chat message', function(msg){
|
||||
console.log("msg");
|
||||
$('#messages').append($('<li>').text(msg));
|
||||
});
|
||||
})
|
||||
.emit('authenticate', {token: userToken}); // send the jwt
|
||||
})
|
||||
};
|
||||
|
||||
var lock = null;
|
||||
$(document).ready(function() {
|
||||
lock = new Auth0Lock('YOUR_CLIENT_ID', 'YOUR_NAMESPACE');
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
25
example/index.js
Normal file
25
example/index.js
Normal file
@ -0,0 +1,25 @@
|
||||
var app = require('express')();
|
||||
var http = require('http').Server(app);
|
||||
var io = require('socket.io')(http);
|
||||
var socketioJwt = require('socketio-jwt');
|
||||
|
||||
app.get('/', function(req, res){
|
||||
res.sendFile(__dirname + '/index.html');
|
||||
});
|
||||
|
||||
io
|
||||
.on('connection', socketioJwt.authorize({
|
||||
secret: Buffer('YOUR_CLIENT_SECRET', 'base64'),
|
||||
timeout: 15000 // 15 seconds to send the authentication message
|
||||
}))
|
||||
.on('authenticated', function(socket){
|
||||
console.log('connected & authenticated: ' + socket.decoded_token.toString());
|
||||
socket.on('chat message', function(msg){
|
||||
debugger;
|
||||
io.emit('chat message', msg);
|
||||
});
|
||||
});
|
||||
|
||||
http.listen(3001, function(){
|
||||
console.log('listening on *:3001');
|
||||
});
|
10
example/package.json
Normal file
10
example/package.json
Normal file
@ -0,0 +1,10 @@
|
||||
{
|
||||
"name": "socket-chat-example",
|
||||
"version": "0.0.1",
|
||||
"description": "my first socket.io app",
|
||||
"dependencies": {
|
||||
"express": "^4.10.2",
|
||||
"socket.io": "^1.3.6",
|
||||
"socketio-jwt": "^4.3.1"
|
||||
}
|
||||
}
|
@ -1,64 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Socket.io with JWTs</title>
|
||||
</head>
|
||||
<body>
|
||||
<form id="login">
|
||||
<input type="text" id="username" value="john" />
|
||||
<input type="password" id="password" value="aaa" />
|
||||
<input type="submit" value="login">
|
||||
</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(token ? ('?token=' + token) : '', {
|
||||
'forceNew': true
|
||||
});
|
||||
|
||||
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>
|
@ -1,54 +0,0 @@
|
||||
var express = require('express');
|
||||
var http = require('http');
|
||||
|
||||
var socketIo = require('socket.io');
|
||||
var socketio_jwt = require('../'); //require('socketio-jwt');
|
||||
|
||||
var jwt = require('jsonwebtoken');
|
||||
var jwt_secret = 'foo bar big secret';
|
||||
|
||||
var app = express();
|
||||
|
||||
var serveStatic = require('serve-static');
|
||||
var bodyParser = require('body-parser');
|
||||
|
||||
app.use(serveStatic('public', {'index': ['index.html']}));
|
||||
app.use(bodyParser.json());
|
||||
|
||||
app.post('/login', function (req, res) {
|
||||
var profile = {
|
||||
first_name: 'John',
|
||||
last_name: 'Doe',
|
||||
email: 'john@doe.com',
|
||||
id: 123
|
||||
};
|
||||
|
||||
// We are sending the profile inside the token
|
||||
var token = jwt.sign(profile, jwt_secret, { expiresInMinutes: 60*5 });
|
||||
|
||||
res.json({token: token});
|
||||
});
|
||||
|
||||
var server = http.createServer(app);
|
||||
var sio = socketIo.listen(server);
|
||||
|
||||
sio.use(socketio_jwt.authorize({
|
||||
secret: jwt_secret,
|
||||
handshake: true
|
||||
}));
|
||||
|
||||
sio.sockets
|
||||
.on('connection', function (socket) {
|
||||
console.log(socket.decoded_token.email, 'connected');
|
||||
socket.on('ping', function (m) {
|
||||
socket.emit('pong', m);
|
||||
});
|
||||
});
|
||||
|
||||
setInterval(function () {
|
||||
sio.sockets.emit('time', Date());
|
||||
}, 5000);
|
||||
|
||||
server.listen(9000, function () {
|
||||
console.log('listening on http://localhost:9000');
|
||||
});
|
Reference in New Issue
Block a user