Skip to content

Commit

Permalink
Updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zerodivisi0n committed Mar 21, 2012
1 parent 92e968e commit f4a6221
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
7 changes: 5 additions & 2 deletions test/echo-server.js
Expand Up @@ -62,14 +62,17 @@ wsServer = new WebSocketServer({
wsServer.on('connect', function(connection) {
if (debug) console.log((new Date()) + " Connection accepted" +
" - Protocol Version " + connection.webSocketVersion);
function sendCallback(err) {
if (err) console.error("send() error: " + err);
}
connection.on('message', function(message) {
if (message.type === 'utf8') {
if (debug) console.log("Received utf-8 message of " + message.utf8Data.length + " characters.");
connection.sendUTF(message.utf8Data);
connection.sendUTF(message.utf8Data, sendCallback);
}
else if (message.type === 'binary') {
if (debug) console.log("Received Binary Message of " + message.binaryData.length + " bytes");
connection.sendBytes(message.binaryData);
connection.sendBytes(message.binaryData, sendCallback);
}
});
connection.on('close', function(reasonCode, description) {
Expand Down
8 changes: 6 additions & 2 deletions test/fragmentation-test-client.js
Expand Up @@ -111,13 +111,17 @@ client.on('connect', function(connection) {
byteCounter = 0;
}
};

function sendUTFCallback(err) {
if (err) console.error("sendUTF() error: " + err);
}

function requestData() {
if (args.binary) {
connection.sendUTF('sendBinaryMessage|' + requestedLength);
connection.sendUTF('sendBinaryMessage|' + requestedLength, sendUTFCallback);
}
else {
connection.sendUTF('sendMessage|' + requestedLength);
connection.sendUTF('sendMessage|' + requestedLength, sendUTFCallback);
}
requestedLength += Math.ceil(Math.random() * 1024);
}
Expand Down
7 changes: 5 additions & 2 deletions test/fragmentation-test-server.js
Expand Up @@ -95,6 +95,9 @@ router.mount('*', 'fragmentation-test', function(request) {

connection.on('message', function(message) {
if (message.type === 'utf8') {
function sendCallback(err) {
if (err) console.error("send() error: " + err);
}
var length = 0;
var match = /sendMessage\|(\d+)/.exec(message.utf8Data);
if (match) {
Expand All @@ -107,7 +110,7 @@ router.mount('*', 'fragmentation-test', function(request) {
longLorem = longLorem.slice(0,requestedLength);
length = Buffer.byteLength(longLorem);
if (length > 0) {
connection.sendUTF(longLorem);
connection.sendUTF(longLorem, sendCallback);
console.log((new Date()) + " sent " + length + " byte utf-8 message to " + connection.remoteAddress);
}
return;
Expand All @@ -123,7 +126,7 @@ router.mount('*', 'fragmentation-test', function(request) {
buffer[i] = Math.ceil(Math.random()*255);
}

connection.sendBytes(buffer);
connection.sendBytes(buffer, sendCallback);
console.log((new Date()) + " sent " + buffer.length + " byte binary message to " + connection.remoteAddress);
return;
}
Expand Down
5 changes: 4 additions & 1 deletion test/libwebsockets-test-client.js
Expand Up @@ -53,14 +53,17 @@ mirrorClient.on('connect', function(connection) {
connection.on('close', function() {
console.log("lws-mirror-protocol Connection Closed");
});
function sendCallback(err) {
if (err) console.error("send() error: " + err);
}
function spamCircles() {
if (connection.connected) {
// c #7A9237 487 181 14;
var color = 0x800000 + Math.round(Math.random() * 0x7FFFFF);
var x = Math.round(Math.random() * 502);
var y = Math.round(Math.random() * 306);
var radius = Math.round(Math.random() * 30);
connection.send("c #" + color.toString(16) + " " + x + " " + y + " " + radius + ";");
connection.send("c #" + color.toString(16) + " " + x + " " + y + " " + radius + ";", sendCallback);
setTimeout(spamCircles, 10);
}
}
Expand Down
10 changes: 7 additions & 3 deletions test/libwebsockets-test-server.js
Expand Up @@ -91,6 +91,10 @@ var mirrorConnections = [];

var mirrorHistory = [];

function sendCallback(err) {
if (err) console.error("send() error: " + err);
}

router.mount('*', 'lws-mirror-protocol', function(request) {
var cookies = [
{
Expand All @@ -115,7 +119,7 @@ router.mount('*', 'lws-mirror-protocol', function(request) {
var historyString = mirrorHistory.join('');
console.log((new Date()) + " sending mirror protocol history to client; " + connection.remoteAddress + " : " + Buffer.byteLength(historyString) + " bytes");

connection.send(historyString);
connection.send(historyString, sendCallback);
}

mirrorConnections.push(connection);
Expand All @@ -134,7 +138,7 @@ router.mount('*', 'lws-mirror-protocol', function(request) {

// Re-broadcast the command to all connected clients
mirrorConnections.forEach(function (outputConnection) {
outputConnection.send(message.utf8Data);
outputConnection.send(message.utf8Data, sendCallback);
});
}
});
Expand All @@ -161,7 +165,7 @@ router.mount('*', 'dumb-increment-protocol', function(request) {

var number = 0;
connection.timerInterval = setInterval(function() {
connection.send((number++).toString(10));
connection.send((number++).toString(10), sendCallback);
}, 50);
connection.on('close', function() {
clearInterval(connection.timerInterval);
Expand Down

0 comments on commit f4a6221

Please sign in to comment.