mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
73fb24f48d
Instead servers are passed a function which gets called on connection (like in the original design) which has one argument, the connecting socket. The user sets up callbacks on that. It's pretty much how I had it originally. Encoding is now set via v8 getter/setter and can be changed dynamically. The timeout for all sockets is fixed at 60 seconds for now. Need to fix that.
66 lines
1.2 KiB
JavaScript
66 lines
1.2 KiB
JavaScript
include("mjsunit");
|
|
|
|
var port = 12123;
|
|
var N = 1000;
|
|
var count = 0;
|
|
|
|
function Ponger (socket) {
|
|
socket.encoding = "UTF8";
|
|
socket.timeout = 0;
|
|
|
|
puts("got socket.");
|
|
|
|
socket.onReceive = function (data) {
|
|
//puts("server recved data: " + JSON.stringify(data));
|
|
assertTrue(count <= N);
|
|
stdout.print("-");
|
|
if (/PING/.exec(data)) {
|
|
socket.send("PONG");
|
|
}
|
|
};
|
|
|
|
socket.onEOF = function () {
|
|
puts("ponger: onEOF");
|
|
socket.close();
|
|
};
|
|
|
|
socket.onDisconnect = function () {
|
|
puts("ponger: onDisconnect");
|
|
socket.server.close();
|
|
};
|
|
}
|
|
|
|
function onLoad() {
|
|
var server = new node.tcp.Server(Ponger);
|
|
server.listen(port);
|
|
|
|
var client = new node.tcp.Connection();
|
|
|
|
client.encoding = "UTF8";
|
|
|
|
client.onConnect = function () {
|
|
puts("client is connected.");
|
|
client.send("PING");
|
|
};
|
|
|
|
client.onReceive = function (data) {
|
|
//puts("client recved data: " + JSON.stringify(data));
|
|
stdout.print(".");
|
|
assertEquals("PONG", data);
|
|
count += 1;
|
|
if (count < N) {
|
|
client.send("PING");
|
|
} else {
|
|
puts("sending FIN");
|
|
client.close();
|
|
}
|
|
};
|
|
|
|
client.onEOF = function () {
|
|
puts("pinger: onEOF");
|
|
assertEquals(N, count);
|
|
};
|
|
|
|
client.connect(port);
|
|
}
|