]> git.proxmox.com Git - ceph.git/blame - ceph/src/civetweb/src/third_party/duktape-1.8.0/examples/eventloop/server-socket-test.js
import 12.2.13 release
[ceph.git] / ceph / src / civetweb / src / third_party / duktape-1.8.0 / examples / eventloop / server-socket-test.js
CommitLineData
7c673cae
FG
1
2var HOST = 'localhost'
3var PORT = 12345;
4var EXIT_TIMEOUT = 300e3;
5
6print('automatic exit after ' + (EXIT_TIMEOUT / 1e3) + ' seconds');
7setTimeout(function () {
8 print('exit timer');
9 EventLoop.requestExit();
10}, EXIT_TIMEOUT);
11
12print('listen on ' + HOST + ':' + PORT);
13EventLoop.server(HOST, PORT, function (fd, addr, port) {
14 print('new connection on fd ' + fd + ' from ' + addr + ':' + port);
15 EventLoop.setReader(fd, function (fd, data) {
16 var b, i, n, x;
17
18 // Handle socket data carefully: if you convert it to a string,
19 // it may not be valid UTF-8 etc. Here we operate on the data
20 // directly in the buffer.
21
22 b = data.valueOf(); // ensure we get a plain buffer
23 n = b.length;
24 for (i = 0; i < n; i++) {
25 x = b[i];
26 if (x >= 0x61 && x <= 0x7a) {
27 b[i] = x - 0x20; // uppercase
28 }
29 }
30
31 print('read data on fd ' + fd + ', length ' + data.length);
32 EventLoop.write(fd, data);
33 });
34});