Hi , im learning node , im quite new to command prompt and back ned ,and im trying to build TCP server/client chat
my code so far is
server.js
var net=require("net");
var sockets=[];
var server=net.createServer(function(socket){
console.log("new socket connected");
sockets.push(socket);
var sh="";
socket.on("data",function(data){
sh=sh+data;
});
socket.on("end",function(){
sockets.forEach(function(x){
if(x!==socket){
x.write("Dakto poveda :"+ sh);
sh="";
}
});
})
socket.on("close",function(){
console.log("Connection closed")
})
}).listen(8888)
and client.js
var net=require("net");
var client= new net.Socket();
client.connect(8888,function(){
console.log("Conncted");
client.write("Hello from client")
});
client.on("data",function(data){
console.log("data "+data)
});
client.on("close",function(){
console.log("Client closed");
client.write("client closed")
})
i start server.js then i open new cmds and start client.js in them . In the server file in writes that client has connected , and in the client cmd it writes "connected" and it doesnt write "hello from client" + the question is , how do i communicate between those clients / server? When i start those files , i cant write to cmd anymore