0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/test-pingpong.js
Ryan 09c2ae5c3e Slight change in tcp connection constructor
For server-side sockets, no longer pass the server object to the
js constructor. This is set later with SetAcceptor.

I think the change is a bit strage and convoluted but it allows one give
protocol /classes/ to the c++ constructors instead of protocol instances.
This is nice because derived classes (like HTTP) don't need to copy the
protocol instanciation code.
2009-05-04 12:08:13 +02:00

67 lines
1.1 KiB
JavaScript

include("mjsunit");
var port = 12123;
var N = 1000;
var count = 0;
function Ponger (socket) {
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");
socket.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);
}