Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const winston = require('winston')
- const feathers = require('feathers-client')
- const io = require('socket.io-client')
- const feathersHooks = require('feathers-hooks')
- const util = require('util')
- const level = process.env.LOG_LEVEL || 'info'
- const logger = new winston.Logger({ level, transports: [ new winston.transports.Console({ colorize: true }) ] })
- async function connectClient(url, id) {
- const start = process.hrtime()
- // Configure our client (hooks, auth, connection)
- let client = feathers()
- client.data = { id, durations: { } }
- client.configure(feathersHooks())
- let socket = io(url, { transports: ['websocket'], path: '/apiws' })
- client.configure(feathers.socketio(socket, { timeout: 10000 }))
- client.configure(feathers.authentication({ path: '/api/authentication' }))
- // Helper to store the duration of a particular operation giving its start time
- client.setDuration = function (key, start) {
- const end = process.hrtime()
- client.data.durations[key] = (end[0] + end[1] / 1000000000) - (start[0] + start[1] / 1000000000)
- }
- // Helper to make the client wait simulating a "human"
- client.wait = async function (duration) {
- await util.promisify(setTimeout)(duration)
- }
- client.setDuration('connect', start)
- return client
- }
- async function authenticateClient(client) {
- const start = process.hrtime()
- let response = await client.authenticate({
- strategy: 'local', email: 'xxx', password: 'xxx'
- })
- logger.verbose('Authenticated new client ' + client.data.id)
- // We always need to get the user after authenticating
- const payload = await client.passport.verifyJWT(response.accessToken)
- client.data.user = await client.service('/api/users').get(payload.userId)
- client.setDuration('authenticate', start)
- return client
- }
- async function disconnectClient(client) {
- const start = process.hrtime()
- await client.logout()
- logger.verbose('Closed client ' + client.data.id)
- client.setDuration('disconnect', start)
- }
Add Comment
Please, Sign In to add comment