-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
37 lines (32 loc) · 1002 Bytes
/
index.js
File metadata and controls
37 lines (32 loc) · 1002 Bytes
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
const createServer = require('./server');
const createClient = require('./client');
const SERVER_PORT = 3000;
const initSocketHandlers = (socket, isServer) => {
socket.on('hello', () => {
console.log(`${isServer ? 'client' : 'server'} said hi`);
});
socket.on('message', data => {
console.log(
`${isServer ? 'client' : 'server'} sent message: ${JSON.stringify(data)}`,
);
});
};
(async () => {
const server = createServer(SERVER_PORT, initSocketHandlers);
const client = createClient(SERVER_PORT, initSocketHandlers);
await waitPromise(1000);
client.fire('hello');
await waitPromise(1000);
server.fire('hello');
await waitPromise(1000);
server.fire('message', {id: 0, content: 'you may update your ui'});
await waitPromise(1000);
client.fire('message', {id: 1, content: 'please update your db'});
await waitPromise(1000);
process.exit(0);
})();
function waitPromise(time) {
return new Promise(resolve => {
setTimeout(resolve, time);
});
}