Completed
Push — master ( 1abe1d...4b8289 )
by Thomas
46s
created

Socket.start   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 9.4285
1
'use strict'
2
3
const io = require('socket.io-client')
4
const pkg = require('../../package.json')
5
const helpers = require('../run-helpers')
6
7
var Socket = {
8
  _instance: null
9
}
10
11
Socket.start = function (containerToken) {
12
  var socket = io('http://ws.sadev.io/')
13
14
  socket.on('connect', function () {
15
    socket.emit('room', 'SADEV:' + containerToken)
16
    socket.emit('room', 'BROADCAST')
17
    socket.emit('room', pkg.name + '/' + pkg.version)
18
    socket.emit('clientInfo', pkg)
19
    Socket._instance = socket
20
  })
21
22
  socket.on('log', function (data) {
23
    if (!('color' in data)) {
24
      if (data.component === 'Logger') {
25
        data.color = 'yellow'
26
      } else {
27
        data.color = 'blue'
28
      }
29
    }
30
    helpers.logMessage(data)
31
  })
32
33
  socket.on('disconnect', function () {
34
    Socket._instance = null
35
    socket.close()
36
    setTimeout(function () {
37
      Socket.start(containerToken)
38
    }, 200)
39
  })
40
41
  socket.open()
42
}
43
44
Socket.emit = function () {
45
  if (!Socket._instance) {
46
    return false
47
  }
48
49
  return Socket._instance.emit.apply(Socket._instance, arguments)
50
}
51
52
module.exports = Socket
53