-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchild2.js
More file actions
executable file
·40 lines (40 loc) · 1.37 KB
/
Copy pathchild2.js
File metadata and controls
executable file
·40 lines (40 loc) · 1.37 KB
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
// Change from CommonJS require to ES module import *******************************
import io from 'socket.io-client';
const resetColor = '\x1b[0m';
const redColor = '\x1b[31m';
const greenColor = '\x1b[32m';
const yellowColor = '\x1b[33m';
const blueColor = '\x1b[34m';
// Connect to the parent server ***************************************************
const socket = io('http://localhost:3000');
// Listen for messages from the parent ********************************************
socket.on('messageFromParent', (message) => {
if (message.message === 'Hello from Child 1!') {
socket.emit('messageFromChild', {
from: 'Child 2',
message: 'Hello from Child 2!',
type: 'hello'
});
}
if (message.message === 'Exit request to Child 2') {
socket.emit('messageFromChild', {
from: 'Child 2',
message: 'Exit request to Child 1',
type: 'request'
});
socket.emit('exit');
//console.log('Child 2 : exit event emitted')
setTimeout(() => {
socket.disconnect();
process.exit(0)
return;
}, 1000);
}
});
socket.on('exit', () => {
socket.emit('messageFromChild', {
type: 'exit',
from: 'Child 2',
message: 'Exit event received from child 2'
});
});