2009-06-08 15:30:10 +02:00
|
|
|
include("mjsunit.js");
|
2009-05-19 21:53:26 +02:00
|
|
|
var port = 8921;
|
|
|
|
|
|
|
|
function onLoad () {
|
|
|
|
|
2009-06-04 12:31:45 +02:00
|
|
|
var server = new node.tcp.Server(function (socket) {
|
2009-05-19 21:53:26 +02:00
|
|
|
puts("new connection");
|
|
|
|
socket.onConnect = function () {
|
|
|
|
socket.send("hello\r\n");
|
|
|
|
};
|
|
|
|
|
|
|
|
socket.onEOF = function () {
|
|
|
|
socket.close();
|
|
|
|
};
|
|
|
|
|
2009-06-04 12:31:45 +02:00
|
|
|
socket.onDisconnect = function (had_error) {
|
|
|
|
//puts("server had_error: " + JSON.stringify(had_error));
|
|
|
|
assertFalse(had_error);
|
2009-05-19 21:53:26 +02:00
|
|
|
};
|
2009-06-04 12:31:45 +02:00
|
|
|
});
|
|
|
|
server.listen(port);
|
2009-05-19 21:53:26 +02:00
|
|
|
|
|
|
|
var count = 0;
|
|
|
|
var client = new node.tcp.Connection();
|
|
|
|
|
2009-05-20 10:02:02 +02:00
|
|
|
client.setEncoding("UTF8");
|
2009-05-19 21:53:26 +02:00
|
|
|
client.onConnect = function () {
|
|
|
|
puts("client connected");
|
|
|
|
};
|
|
|
|
|
|
|
|
client.onReceive = function (chunk) {
|
|
|
|
puts("got msg");
|
|
|
|
assertEquals("hello\r\n", chunk);
|
|
|
|
client.fullClose();
|
|
|
|
};
|
|
|
|
|
2009-06-04 12:31:45 +02:00
|
|
|
client.onDisconnect = function (had_error) {
|
|
|
|
assertFalse(had_error);
|
2009-05-19 21:53:26 +02:00
|
|
|
puts("client disconnected");
|
2009-06-04 12:31:45 +02:00
|
|
|
if (count++ < 5)
|
|
|
|
client.connect(port); // reconnect
|
|
|
|
else
|
|
|
|
server.close();
|
2009-05-19 21:53:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
client.connect(port);
|
|
|
|
}
|