2014-01-14 15:04:41 +01:00
|
|
|
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();
|
|
|
|
|
2014-12-18 05:10:56 +01:00
|
|
|
var serveStatic = require('serve-static')
|
|
|
|
var bodyParser = require('body-parser');
|
|
|
|
|
|
|
|
app.use(serveStatic('public', {'index': ['index.html']}))
|
|
|
|
app.use(bodyParser.json());
|
2014-01-14 15:04:41 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2014-06-15 00:01:34 +02:00
|
|
|
sio.use(socketio_jwt.authorize({
|
2014-01-14 15:04:41 +01:00
|
|
|
secret: jwt_secret,
|
|
|
|
handshake: true
|
|
|
|
}));
|
|
|
|
|
|
|
|
sio.sockets
|
|
|
|
.on('connection', function (socket) {
|
2014-06-15 00:01:34 +02:00
|
|
|
console.log(socket.decoded_token.email, 'connected');
|
2014-01-14 15:04:41 +01:00
|
|
|
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');
|
|
|
|
});
|