-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
130 lines (107 loc) · 2.3 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
*
*
*
*
*
*
* @author XU Kai([email protected])
* @date 2016-12-04 星期日
*
*
*/
'use strict';
let socketCar, socketController;
const DIRECTIVE_MAP = {
// Car direction controller.
'lti': 'car_forward',
'lli': 'car_left',
'lri': 'car_right',
'lbi': 'car_back',
// Camera direction controller.
'rti': 'camera_up',
'rli': 'camera_left',
'rri': 'camera_right',
'rbi': 'camera_down'
};
let listen = (socket) => {
// Tell the controller if CAR is online or not.
socket.emit('car status', !!socketCar)
// Recive the DRIECTIVES from the controller side.
socket.on('directives', (direction) => {
if (!socketCar) {
return;
}
let act;
if (direction.charAt(2) === 'o') {
if (direction.charAt(0) === 'l') {
act = 'car_pause';
} else {
act = 'camera_pause'
}
} else {
act = DIRECTIVE_MAP[direction]
}
console.log(act + ' sent to the CAR .');
socketCar.emit('directives', {
'D': act,
'S': ''
})
});
};
// DEMO Test.
/*
let timer;
let carRunDemo = () => {
socketCar.emit('directives', {
'D': 'car_forward'
});
timer = setTimeout(() => {
socketCar.emit('directives', {
'D': 'car_left',
'S': '0.5'
});
timer = setTimeout(() => {
socketCar.emit('directives', {
'D': 'car_back',
'S': 1000
});
timer = setTimeout(() => {
socketCar.emit('directives', {
'D': 'car_pause'
});
}, 1000);
}, 1000);
}, 1000);
}
*/
let hanler = (socket) => {
let {
specify
} = socket.handshake.query;
console.log(specify + ' is online.');
switch (specify) {
case 'CAR':
socketCar = socket
socketController && socketController.emit('car status', true);
// setTimeout(carRunDemo, 5000);
break;
case 'SIGNAL':
listen(socketController = socket);
break;
default:
break;
}
socket.on('disconnect', () => {
if (specify === 'CAR') {
socketCar = null
socketController && socketController.emit('car status', false);
}
(specify === 'SIGNAL') && (socketController = null);
// clearTimeout(timer);
console.log(specify + ' is offline.');
});
};
const io = require('socket.io')();
io.on('connection', hanler);
io.listen(8080);