2
1
mirror of https://github.com/Thream/socketio-jwt.git synced 2024-07-21 09:38:31 +02:00
socketio-jwt/example/server.js
wonderdogone ddbee9fc9d updated express version for example
updated the example to work with express version 4.10.6.
Removed connect middleware. Added serve-static and body-parser
middleware.
Updated package.json to reflect changes
2014-12-17 20:10:56 -08:00

54 lines
1.2 KiB
JavaScript

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');
});