If you want to communicate between server and client on port you need to create tcp connection on server.
In tcp connection way of communication is different rather than http request. If your device send to data on port it will receive on a dedicated server along with defined port.
In node.js for tcp connection we need to require 'net' module
var net = require("net");
To create server following line would be used
var server = net.createServer({allowHalfOpen: true});
we need to written following lines to receive client side request on dedicated port.
server.on('connection', function(stream) {
});
server.listen(6969);
Here 6969 is the port.
Full snippet given below to create server of tcp connection.
// server.js
var net = require("net");
global.mongo = require('mongoskin');
var serverOptions = {
'native_parser': true,
'auto_reconnect': true,
'poolSize': 5
};
var db = mongo.db("mongodb://127.0.0.1:27017/testdb", serverOptions);
var PORT = '6969';
var server = net.createServer({allowHalfOpen: true});
// listen connection request from client side
server.on('connection', function(stream) {
console.log("New Client is connected on " + stream.remoteAddress + ":" + stream.remotePort);
stream.setTimeout(000);
stream.setEncoding("utf8");
var data_stream = '';
stream.addListener("error", function(err) {
if (clients.indexOf(stream) !== -1) {
clients.splice(clients.indexOf(stream), 1);
}
console.log(err);
});
stream.addListener("data", function(data) {
var incomingStanza;
var isCorrectJson = false;
db.open(function(err, db) {
if (err) {
AppFun.errorOutput(stream, 'Error in connecting database..');
} else {
stream.name = stream.remoteAddress + ":" + stream.remotePort;
// Put this new client in the list
clients.push(stream);
console.log("CLIENTS LENGTH " + clients.length);
//handle json whether json is correct or not
try {
var incomingStanza = JSON.parse(data);
isCorrectJson = true;
} catch (e) {
isCorrectJson = false;
}
// Now you can process here for each request of client
});
stream.addListener("end", function() {
stream.name = stream.remoteAddress + ":" + stream.remotePort;
if (clients.indexOf(stream) !== -1) {
clients.splice(clients.indexOf(stream), 1);
}
console.log("end of listener");
stream.end();
stream.destroy();
});
});
server.listen(PORT);
console.log("Vent server listening on post number " + PORT);
// on error this msg will be shown
server.on('error', function(e) {
if (e.code == 'EADDRINUSE') {
console.log('Address in use, retrying...');
server.listen(5555);
setTimeout(function() {
server.close();
server.listen(PORT);
}, 1000);
}
if (e.code == 'ECONNRESET') {
console.log('Something Wrong. Connection is closed..');
setTimeout(function() {
server.close();
server.listen(PORT);
}, 1000);
}
});
Now its time to create a client for tcp server
From clint we will send all request to make a conncetion get possible results
//client.js
var net = require('net');
var client = net.connect({
//host:'localhost://',
port: 6969
},
function() { //'connect' listener
console.log('client connected');
var jsonData = '{"cmd":"test_command"}';
client.write(jsonData);
});
client.on('data', function(data) {
console.log(data.toString());
// client.end();
});
client.on('end', function() {
console.log('client disconnected');
});
Now we have both server and client in tcp communication.
to ready to listen server we need to hit command 'node server.js' in console.
after that server will be ready to listen request from client side
to make a call from client we need to hit command 'node client.js'
It will more meaningful and worthy if you can modify according your acctual requirement.
Thanks