I am reading a book "Node.js in Action" and trying out examples from it. One of the example which creates a channel from "net" and then creates a channel for all connections and broadcasts messages to other connections/channels.
var events = require('events');
var net = require('net');
var channel = new events.EventEmitter();
channel.clients = {};
channel.subscriptions = {};
channel.on('join', function (id, client) {
this.clients[id] = client;
this.subscriptions[id] = function (senderId, message) {
if (id !== senderId) {
this.clients[id].write(message);
}
};
this.on('broadcast', this.subscriptions[id]);
});
channel.on('leave', function(id) {
channel.removeListener('broadcast', this.subscriptions[id]);
channel.emit('broadcast', id, id + " has left the chat. \n");
});
channel.on('shutdown', function() {
channel.emit('broadcast', '', "Chat has shut down.!\n");
channel.removeAllListeners('broadcast');
});
var server = net.createServer(function (client) {
var id = client.remoteAddress + ':' + client.remotePort;
//client.on('connect', function () {
channel.emit('join', id, client);
//});
client.on('data', function (data) {
data = data.toString();
if(data == "shutdown\r\n") {
channel.emit('shutdown');
}
channel.emit('broadcast', id, data);
});
client.on('close', function() {
channel.emit('leave', id);
});
});
server.listen(8888);
Now if you look at the following code:
client.on('data', function (data) {
data = data.toString();
if(data == "shutdown\r\n") {
channel.emit('shutdown');
}
channel.emit('broadcast', id, data);
});
it reads data from command line when you connect to this server by using telnet using following command:
telnet 127.0.0.1 8888
and you can connect from multiple terminals/shells to this server running on localhost port 8888 but when it comes to reading input stream from telnet - it reads data as soon as user enters it. So if I want to detect a string "shutdown" with carriage return and line feed (\r\n) I am not able to do it because callback function gets called with each keystroke, so what you get is "s" "h" "u" "t" ... "n" as individual characters and not as a string buffer.
I read in APIs that there is flowing mode and pause mode but not sure how to implement it here.
There was another example in book where I encountered and I fixed it by comparing it with character-code and manually buffering till I encounter carriage-return key but I am looking forward to a better solution and preferably if node.js has something to offer.
var net = require('net');
var line = '';
var server = net.createServer(function(socket) {
socket.on('data', function(data) {
if(data.toString().charCodeAt(0) != 13){
line += data.toString();
} else {
//console.log('OO');
socket.write(">>");
socket.write(line);
socket.write("\r\n");
line = '';
}
});
socket.once('data', function(data) {
socket.write("Hey first time I always stay little reluctant..! Lolz!");
})
});
server.listen(88);
Above code is the fix which I used for another example where I ran into similar problem but I want to know standard approach to fix this.