diff --git a/example/public/index.html b/example/public/index.html new file mode 100644 index 0000000..8050bb0 --- /dev/null +++ b/example/public/index.html @@ -0,0 +1,65 @@ + + + + Socket.io with JWTs + + +
+ + + +
+ +
+ +
+ + + + + + + \ No newline at end of file diff --git a/example/server.js b/example/server.js new file mode 100644 index 0000000..9a694d0 --- /dev/null +++ b/example/server.js @@ -0,0 +1,52 @@ +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(); + +app.configure(function(){ + this.use(express.json()); + this.use(express.static(__dirname + '/public')); +}); + +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.set('authorization', socketio_jwt.authorize({ + secret: jwt_secret, + handshake: true +})); + +sio.sockets + .on('connection', function (socket) { + 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'); +}); \ No newline at end of file