2020-12-27 17:25:44 +01:00
|
|
|
'use strict' // Node 4.x workaround
|
2019-10-14 04:13:25 +02:00
|
|
|
|
2020-12-27 17:25:44 +01:00
|
|
|
const express = require('express')
|
|
|
|
const http = require('http')
|
2015-08-31 16:04:04 +02:00
|
|
|
|
2020-12-27 17:25:44 +01:00
|
|
|
const socketIo = require('socket.io')
|
|
|
|
const socketio_jwt = require('../../lib')
|
2015-08-31 16:04:04 +02:00
|
|
|
|
2020-12-27 17:25:44 +01:00
|
|
|
const jwt = require('jsonwebtoken')
|
|
|
|
const xtend = require('xtend')
|
|
|
|
const enableDestroy = require('server-destroy')
|
2015-08-31 16:04:04 +02:00
|
|
|
|
2020-12-27 17:25:44 +01:00
|
|
|
let sio
|
2015-08-31 16:04:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2019-10-14 01:46:30 +02:00
|
|
|
exports.start = (callback) => {
|
2019-10-13 15:52:14 +02:00
|
|
|
const options = {
|
2015-08-31 16:04:04 +02:00
|
|
|
secret: 'aaafoo super sercret',
|
|
|
|
timeout: 1000,
|
|
|
|
handshake: false
|
2020-12-27 17:25:44 +01:00
|
|
|
}
|
2015-08-31 16:04:04 +02:00
|
|
|
|
2020-12-27 17:25:44 +01:00
|
|
|
const app = express()
|
|
|
|
const server = http.createServer(app)
|
|
|
|
sio = socketIo.listen(server)
|
2015-08-31 16:04:04 +02:00
|
|
|
|
2020-12-27 17:25:44 +01:00
|
|
|
app.use(express.json())
|
2019-10-14 01:46:30 +02:00
|
|
|
app.post('/login', (req, res) => {
|
2019-10-13 15:52:14 +02:00
|
|
|
const profile = {
|
2015-08-31 16:04:04 +02:00
|
|
|
first_name: 'John',
|
|
|
|
last_name: 'Doe',
|
|
|
|
email: 'john@doe.com',
|
|
|
|
id: 123
|
2020-12-27 17:25:44 +01:00
|
|
|
}
|
2015-08-31 16:04:04 +02:00
|
|
|
|
|
|
|
// We are sending the profile inside the token
|
2020-12-27 17:25:44 +01:00
|
|
|
const token = jwt.sign(profile, options.secret, { expiresIn: 60 * 60 * 5 })
|
|
|
|
res.json({ token: token })
|
|
|
|
})
|
2015-08-31 16:04:04 +02:00
|
|
|
|
2019-10-15 12:54:02 +02:00
|
|
|
// Global namespace (public)
|
2019-10-14 01:46:30 +02:00
|
|
|
sio.on('connection', (socket) => {
|
2020-12-27 17:25:44 +01:00
|
|
|
socket.emit('hi')
|
|
|
|
})
|
2015-08-31 16:04:04 +02:00
|
|
|
|
2019-10-15 12:54:02 +02:00
|
|
|
// Second roundtrip
|
2020-12-27 17:25:44 +01:00
|
|
|
const admin_nsp = sio.of('/admin')
|
2015-08-31 16:04:04 +02:00
|
|
|
|
2020-12-27 17:25:44 +01:00
|
|
|
admin_nsp
|
|
|
|
.on('connection', socketio_jwt.authorize(options))
|
2019-10-15 12:54:02 +02:00
|
|
|
.on('authenticated', (socket) => {
|
2020-12-27 17:25:44 +01:00
|
|
|
socket.emit('hi admin')
|
|
|
|
})
|
2019-10-15 12:54:02 +02:00
|
|
|
|
|
|
|
// One roundtrip
|
2020-12-27 17:25:44 +01:00
|
|
|
const admin_nsp_hs = sio.of('/admin_hs')
|
2019-10-15 12:54:02 +02:00
|
|
|
|
2020-12-27 17:25:44 +01:00
|
|
|
admin_nsp_hs.use(socketio_jwt.authorize(xtend(options, { handshake: true })))
|
2019-10-15 12:54:02 +02:00
|
|
|
admin_nsp_hs.on('connection', (socket) => {
|
2020-12-27 17:25:44 +01:00
|
|
|
socket.emit('hi admin')
|
|
|
|
})
|
2015-08-31 16:04:04 +02:00
|
|
|
|
2020-12-27 17:25:44 +01:00
|
|
|
server.listen(9000, callback)
|
|
|
|
enableDestroy(server)
|
|
|
|
}
|
2015-08-31 16:04:04 +02:00
|
|
|
|
2019-10-14 01:46:30 +02:00
|
|
|
exports.stop = (callback) => {
|
2020-12-27 17:25:44 +01:00
|
|
|
sio.close()
|
2015-08-31 16:14:48 +02:00
|
|
|
try {
|
2020-12-27 17:25:44 +01:00
|
|
|
server.destroy()
|
2015-08-31 16:14:48 +02:00
|
|
|
} catch (er) {}
|
2020-12-27 17:25:44 +01:00
|
|
|
callback()
|
|
|
|
}
|