add tests for namespace configuration
This commit is contained in:
@ -9,6 +9,7 @@ var jwt = require('jsonwebtoken');
|
||||
var xtend = require('xtend');
|
||||
|
||||
var server, sio;
|
||||
var enableDestroy = require('server-destroy');
|
||||
|
||||
exports.start = function (options, callback) {
|
||||
|
||||
@ -49,11 +50,8 @@ exports.start = function (options, callback) {
|
||||
sio = socketIo.listen(server);
|
||||
|
||||
if (options.handshake) {
|
||||
// this.set('authorization', socketio_jwt.authorize(options));
|
||||
sio.use(socketio_jwt.authorize(options));
|
||||
}
|
||||
|
||||
if (options.handshake) {
|
||||
sio.sockets.on('echo', function (m) {
|
||||
sio.sockets.emit('echo-response', m);
|
||||
});
|
||||
@ -72,9 +70,11 @@ exports.start = function (options, callback) {
|
||||
server.__sockets.push(c);
|
||||
});
|
||||
server.listen(9000, callback);
|
||||
enableDestroy(server);
|
||||
};
|
||||
|
||||
exports.stop = function (callback) {
|
||||
sio.close();
|
||||
server.destroy();
|
||||
callback();
|
||||
};
|
72
test/fixture/namespace.js
Normal file
72
test/fixture/namespace.js
Normal file
@ -0,0 +1,72 @@
|
||||
var express = require('express');
|
||||
var http = require('http');
|
||||
|
||||
var socketIo = require('socket.io');
|
||||
var socketio_jwt = require('../../lib');
|
||||
|
||||
var jwt = require('jsonwebtoken');
|
||||
|
||||
var xtend = require('xtend');
|
||||
|
||||
var server, sio;
|
||||
var enableDestroy = require('server-destroy');
|
||||
|
||||
/**
|
||||
* This is an example server that shows how to do namespace authentication.
|
||||
*
|
||||
* The /admin namespace is protected by JWTs while the global namespace is public.
|
||||
*/
|
||||
exports.start = function (callback) {
|
||||
|
||||
options = {
|
||||
secret: 'aaafoo super sercret',
|
||||
timeout: 1000,
|
||||
handshake: false
|
||||
};
|
||||
|
||||
var app = express();
|
||||
|
||||
app.configure(function(){
|
||||
this.use(express.json());
|
||||
this.use(express.urlencoded());
|
||||
});
|
||||
|
||||
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, options.secret, { expiresInMinutes: 60*5 });
|
||||
|
||||
res.json({token: token});
|
||||
});
|
||||
|
||||
server = http.createServer(app);
|
||||
|
||||
sio = socketIo.listen(server);
|
||||
|
||||
sio.on('connection', function (socket) {
|
||||
socket.emit('hi');
|
||||
});
|
||||
|
||||
var admin_nsp = sio.of('/admin');
|
||||
|
||||
admin_nsp.on('connection', socketio_jwt.authorize(options))
|
||||
.on('authenticated', function (socket) {
|
||||
socket.emit('hi admin');
|
||||
});
|
||||
|
||||
|
||||
server.listen(9000, callback);
|
||||
enableDestroy(server);
|
||||
};
|
||||
|
||||
exports.stop = function (callback) {
|
||||
sio.close();
|
||||
server.destroy();
|
||||
callback();
|
||||
};
|
Reference in New Issue
Block a user