0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/test-pingpong.js

67 lines
1.1 KiB
JavaScript

include("mjsunit");
var port = 12123;
var N = 1000;
var count = 0;
function Ponger (socket, server) {
this.encoding = "UTF8";
this.timeout = 0;
this.onConnect = function () {
puts("got socket.");
};
this.onReceive = function (data) {
assertTrue(count <= N);
stdout.print ("-");
if (/PING/.exec(data)) {
socket.send("PONG");
}
};
this.onEOF = function () {
puts("ponger: onEOF");
socket.send("QUIT");
socket.close();
};
this.onDisconnect = function () {
puts("ponger: onDisconnect");
server.close();
};
}
function Pinger (socket) {
this.encoding = "UTF8";
this.onConnect = function () {
socket.send("PING");
};
this.onReceive = function (data) {
stdout.print(".");
assertEquals("PONG", data);
count += 1;
if (count < N) {
socket.send("PING");
} else {
puts("sending FIN");
socket.sendEOF();
}
};
this.onEOF = function () {
puts("pinger: onEOF");
assertEquals(N, count);
};
}
function onLoad() {
var server = new TCPServer(Ponger);
server.listen(port);
var client = new TCPConnection(Pinger);
client.connect(port);
}