Guest User

Untitled

a guest
Mar 24th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. const winston = require('winston')
  2. const feathers = require('feathers-client')
  3. const io = require('socket.io-client')
  4. const feathersHooks = require('feathers-hooks')
  5. const util = require('util')
  6. const level = process.env.LOG_LEVEL || 'info'
  7. const logger = new winston.Logger({ level, transports: [ new winston.transports.Console({ colorize: true }) ] })
  8.  
  9. async function connectClient(url, id) {
  10. const start = process.hrtime()
  11. // Configure our client (hooks, auth, connection)
  12. let client = feathers()
  13. client.data = { id, durations: { } }
  14. client.configure(feathersHooks())
  15. let socket = io(url, { transports: ['websocket'], path: '/apiws' })
  16. client.configure(feathers.socketio(socket, { timeout: 10000 }))
  17. client.configure(feathers.authentication({ path: '/api/authentication' }))
  18. // Helper to store the duration of a particular operation giving its start time
  19. client.setDuration = function (key, start) {
  20. const end = process.hrtime()
  21. client.data.durations[key] = (end[0] + end[1] / 1000000000) - (start[0] + start[1] / 1000000000)
  22. }
  23. // Helper to make the client wait simulating a "human"
  24. client.wait = async function (duration) {
  25. await util.promisify(setTimeout)(duration)
  26. }
  27. client.setDuration('connect', start)
  28. return client
  29. }
  30.  
  31. async function authenticateClient(client) {
  32. const start = process.hrtime()
  33. let response = await client.authenticate({
  34. strategy: 'local', email: 'xxx', password: 'xxx'
  35. })
  36. logger.verbose('Authenticated new client ' + client.data.id)
  37. // We always need to get the user after authenticating
  38. const payload = await client.passport.verifyJWT(response.accessToken)
  39. client.data.user = await client.service('/api/users').get(payload.userId)
  40. client.setDuration('authenticate', start)
  41. return client
  42. }
  43.  
  44. async function disconnectClient(client) {
  45. const start = process.hrtime()
  46. await client.logout()
  47. logger.verbose('Closed client ' + client.data.id)
  48. client.setDuration('disconnect', start)
  49. }
Add Comment
Please, Sign In to add comment