forked from codedinc/eventloopclass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
32 lines (23 loc) · 815 Bytes
/
server.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
var syscalls = require('syscalls');
var loop = require('./loop');
var fd = syscalls.socket(syscalls.AF_INET, syscalls.SOCK_STREAM, 0);
syscalls.fcntl(fd, syscalls.F_SETFL, syscalls.O_NONBLOCK);
syscalls.bind(fd, 3000, "0.0.0.0");
syscalls.listen(fd, 100);
console.log("Listening on port 3000");
loop.on(fd, 'read', function() {
var connFd = syscalls.accept(fd);
syscalls.fcntl(connFd, syscalls.F_SETFL, syscalls.O_NONBLOCK);
console.log("Accepted new connection");
loop.on(connFd, 'read', function() {
var data = syscalls.read(connFd, 1024);
console.log("Received: " + data);
loop.remove(connFd, 'read');
loop.on(connFd, 'write', function() {
syscalls.write(connFd, "bye!\n");
syscalls.close(connFd);
loop.remove(connFd, 'write');
});
});
});
loop.run();