I’m familiarizing myself with node.js and after seeing Ryan Dahl’s example where he builds a super-simple chat server, i decided to try to create a setup where one port listens for inputted text and sends it to an webpage that’s listening on another port. I’ve tried looking through the documentation here but it’s pretty confusing to me. I’ve pasted the code i’ve written so far, but i bet it’s off-base.
var net = require('net');
var http = require('http');
var sockets = [];
var firstCall = true;
var webpage = http.Server(function(req, res) {
if (firstCall) {
res.writeHead(200);
firstCall = false;
}
req.on('data', function(d) {
res.write(d);
}
});
var inputs = net.Server(function(socket) {
sockets.push(socket);
socket.on('data', function(d) {
});
socket.on('cl', function() {
var i = sockets.indexOf(socket);
sockets.splice(i,1);
});
});
inputs.listen(8080);
webpage.listen(8000);
I would use socket.io for something like this; makes life easier, here is a good assist:
https://github.com/learnboost/socket.io