forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-socket-test.js
More file actions
33 lines (28 loc) · 1 KB
/
server-socket-test.js
File metadata and controls
33 lines (28 loc) · 1 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
var HOST = 'localhost'
var PORT = 12345;
var EXIT_TIMEOUT = 300e3;
print('automatic exit after ' + (EXIT_TIMEOUT / 1e3) + ' seconds');
setTimeout(function () {
print('exit timer');
EventLoop.requestExit();
}, EXIT_TIMEOUT);
print('listen on ' + HOST + ':' + PORT);
EventLoop.server(HOST, PORT, function (fd, addr, port) {
print('new connection on fd ' + fd + ' from ' + addr + ':' + port);
EventLoop.setReader(fd, function (fd, data) {
var b, i, n, x;
// Handle socket data carefully: if you convert it to a string,
// it may not be valid UTF-8 etc. Here we operate on the data
// directly in the buffer.
b = data.valueOf(); // ensure we get a plain buffer
n = b.length;
for (i = 0; i < n; i++) {
x = b[i];
if (x >= 0x61 && x <= 0x7a) {
b[i] = x - 0x20; // uppercase
}
}
print('read data on fd ' + fd + ', length ' + data.length);
EventLoop.write(fd, data);
});
});