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

66 lines
1.2 KiB
JavaScript
Raw Normal View History

2009-04-22 15:52:23 +02:00
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");
2009-04-22 15:52:23 +02:00
}
};
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";
2009-04-22 15:52:23 +02:00
client.onConnect = function () {
puts("client is connected.");
client.send("PING");
};
client.onReceive = function (data) {
//puts("client recved data: " + JSON.stringify(data));
stdout.print(".");
2009-04-22 15:52:23 +02:00
assertEquals("PONG", data);
count += 1;
if (count < N) {
client.send("PING");
} else {
puts("sending FIN");
client.close();
}
2009-04-22 15:52:23 +02:00
};
2009-04-22 16:05:14 +02:00
client.onEOF = function () {
puts("pinger: onEOF");
2009-04-23 01:14:11 +02:00
assertEquals(N, count);
2009-04-22 16:04:05 +02:00
};
2009-04-22 15:52:23 +02:00
client.connect(port);
2009-04-22 15:52:23 +02:00
}