Total Complexity | 9 |
Complexity/F | 1.5 |
Lines of Code | 52 |
Function Count | 6 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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 |