forked from magician03/P2P-Chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdummy.js
40 lines (31 loc) · 1.01 KB
/
dummy.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
const spawn = require('child_process').spawn;
args = ['peer1.js', 'hello', 'bar'];
function initThing () {
const ls = spawn('node', args);
ls.stdout.pipe(process.stdout);
ls.stderr.pipe(process.stdout);
ls.stdout.on('data', (data) => {
var x = `${data}`;
x = x.split('>');
var username = x[0];
var message = x.slice(1, x.length).join('>');
if (username == 'new_peer') {
var peerElem = document.createElement('div');
peerElem.innerHTML = message.split('.')[0];
document.getElementById('peer-box').appendChild(peerElem);
} else {
var msgElem = document.createElement('div');
msgElem.setAttribute('class', 'message');
msgElem.innerHTML = "<span class='sender-name'>"+username+"</span><br> "+message;
document.getElementById('chat-window').appendChild(msgElem);
// document.write(`${data}`);
}
});
ls.stderr.on('data', (data) => {
document.write(`stderr: ${data}`);
});
ls.on('close', (code) => {
document.write(`child process exited with code ${code}`);
});
}
initThing();