I am trying to use Narf module on node.js to establish a real-time connection. Everything is set up using the below server side code.
var narf = require( './narf' );
var connectedClients = []; //keep track of which clients are connected
var APIFunctions = { //forward facing functions
GET : { //headers object and parsed url are passed as a parameter for get functions
sendToClients : function ( header, url ){
connectedClients.forEach( function( connection ){
connection.send( JSON.stringify( { message : url.message } ) );
});
}
},
POST : {}
};
narf.startHTTPServer( APIFunctions, function( httpServer ){
narf.narfSocketServer( httpServer, function( request ){
var connection = request.accept( null, request.origin ); //accept the connection request
connectedClients.push( connection );
console.log( connectedClients.length + ' connections open' );
connection.on( 'message', function( message ){ //the user has sent a message
if ( message.type === 'utf8' ){
console.log( message ); //process
if( typeof message === 'string' ) message = JSON.parse( message );
connection.send( JSON.stringify({ message : 'hello client' }) );
}
} );
connection.on( 'close', function( ){ //The user has closed the connection
for (var i in connectedClients){
/* remove the client connection from the array and free some memory*/
if( connectedClients[i] == connection ){
connectedClients.splice(i,1);
console.log('removing from disconnected client list');
}
}
} );
} );
} );
However, the default hostname of narf is localhost . I need to change the hostname to an ip address for narf.
I'm not able to find an documentation for changing the hostname.
I haven't used NARF before, but it looks like it is setting up an HTTP server on the machine it is running on.
Therefore, the hostname is not determined by NARF or by your code, but rather by your machine and network IP and DNS configuration. If you want your server to run with a specific IP address, you'll have to make sure that the machine has been assigned that IP address.
Now if you have multiple IPs assigned to your machine and you just want NARF to listen on one of them, that's a different story. I'm not exactly sure if NARF or Node.js provide a mechanism for binding to a specific IP on a multihomed machine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.