Reputation: 99
Ive got a piece of code running in node js that reads incoming data over sockets. the information is displayed on the console which is useful but I want to also write it to a text file for further analysis. However as I had the code configured it worked but just kept overwriting the contents of the file. Ive changed it about to try and make sure the text file isnt constantly being overwritten. I now get the "write after end" error. Ive attached the code below. I would like it if the raw data in the variable "data" was saved in a text file. it would be great. Thanks guys
Load the TCP Library
var net = require('net');
var fs = require('fs');
// Keep track of the chat clients
var clients = [];
var tcpport = 13000;
var tcphost;
var wstream = fs.createWriteStream('myOutput.txt');
// Start a TCP Server
net.createServer(function (socket) {
// Identify this client
socket.name = socket.remoteAddress + ":" + socket.remotePort;
// Put this new client in the list
clients.push(socket);
// Send a nice welcome message and announce
console.log("Welcome " + socket.name);
console.log(socket.name + " joined");
// Handle incoming messages from clients.
socket.on('data', function (data) {
console.log('Incoming Data: ' + data.toString());
wstream.write(data.toString());
wstream.end();
var str = data.toString().split("@");
if (str[1] == 'Pong'){
broadcast('AnnouncerKit@AckPong@$');
broadcast('AnnouncerKit@Ping@$');
}
if (str[1]== 'AckPing'){
broadcast('AnnouncerKit@GetInfo@$');
}
if (str[1]== 'Store'){
broadcast('AnnouncerKit@AckStore@'+str[str.length-2]+'@$');
//console.log(bibs);
}
});
// Remove the client from the list when it leaves
socket.on('end', function () {
clients.splice(clients.indexOf(socket), 1);
console.log(socket.name + " left");
});
// Send a message to all clients
function broadcast(message) {
clients.forEach(function (client) {
client.write(message);
});
// Log it to the server output too
console.log('Outgoing Data: ' + message);
}
}).listen(tcpport);
console.log("TCP server running at port " + tcpport);
enter code here
Also just for reference, here is the working code but it overwrites.
// Load the TCP Library
var net = require('net');
// Keep track of the chat clients
var clients = [];
var tcpport = 13000;
var tcphost;
// Start a TCP Server
net.createServer(function (socket) {
var fs = require('fs');
// Identify this client
socket.name = socket.remoteAddress + ":" + socket.remotePort;
// Put this new client in the list
clients.push(socket);
// Send a nice welcome message and announce
console.log("Welcome " + socket.name);
console.log(socket.name + " joined");
// Handle incoming messages from clients.
socket.on('data', function (data) {
console.log('Incoming Data: ' + data.toString());
var wstream = fs.createWriteStream('myOutput.txt');
wstream.write(data.toString());
wstream.end();
var str = data.toString().split("@");
if (str[1] == 'Pong'){
broadcast('AnnouncerKit@AckPong@$');
broadcast('AnnouncerKit@Ping@$');
}
if (str[1]== 'AckPing'){
broadcast('AnnouncerKit@GetInfo@$');
}
if (str[1]== 'Store'){
broadcast('AnnouncerKit@AckStore@'+str[str.length-2]+'@$');
//console.log(bibs);
}
});
// Remove the client from the list when it leaves
socket.on('end', function () {
clients.splice(clients.indexOf(socket), 1);
console.log(socket.name + " left");
});
// Send a message to all clients
function broadcast(message) {
clients.forEach(function (client) {
client.write(message);
});
// Log it to the server output too
console.log('Outgoing Data: ' + message);
}
}).listen(tcpport);
console.log("TCP server running at port " + tcpport);
And the resulting output in the text file
Finish@Store@000064919:13:36.505 1 03 155641803211E@000089619:13:36.826 1 0F 155661803213D@683@$
Upvotes: 0
Views: 4082
Reputation: 820
In the second snippet, use:
var wstream = fs.createWriteStream('myOutput.txt', {flags: 'a'});
wstream.write(data.toString());
wstream.end();
setting the flag this way gives you a writer which writes to the end of the file. Refer the docs for other available options.
In the first code snippet, you are getting the "write after end" error because you are writing to a stream after it has been closed (ended). writer.end()
closes the stream. It will work the first time data is received, but on every subsequent chunk of data written on the socket, you are using the same writer which has already ended.
In the second snippet, you won't get this error because in the callback you are recreating the stream each time data is received.
Hope this helps.
Upvotes: 2