0

I want to run a javascript program without terminating inside node.js :---

Is it the right way to do it by using while(1); ?

Inside this javascript program i have created a websocket & listing to it.
Whenever data comes on websocket it throws on console.log.

test.js :--

var tt = new websocket_fun();

function websocket_fun()
{

   var temp = new websocket_create();

   while(1);
}
function websocket_create()  
{

     // Open the socket
    this.socket = new WebSocket( "192.168.0.11:8080"); 
    // Bind events
    this.socket.onmessage = this.onMessagesocket.bind(this);
    this.socket.onopen = this.onOpensocket.bind(this);
    this.socket.onclose = this.onClosesocket.bind(this);


}

websocket_create.prototype.onMessagesocket = function(msg)
{
  console.log(msg);
}

websocket_create.prototype.onOpensocket = function(msg)
{
  console.log('Open websocket');
}

websocket_create.prototype.onClosesocket = function(msg)
{
  console.log('Close websocket');
}

run :---
node test.js

Katoch
  • 2,709
  • 9
  • 51
  • 84

2 Answers2

0

while(1) is not a good idea since it will block your program and eat a lot of processor power.

There are probably other ways to do this but the easiest I can think of is to use setInterval
You can use an empty function if you don't have anything to execute.

setInterval(function(){}, 10000);
mihai
  • 37,072
  • 9
  • 60
  • 86
  • can i call a function periodically ... ? gave me an error "period is not defined " if i use .. setInterval(function(){period();}, 10000); fuction period() { console.log('1 + '); } – Katoch Nov 18 '14 at 11:35
  • just define `period` before calling `setInterval`. You can also do `setInterval(period, 10000)` – mihai Nov 18 '14 at 12:48
0

It is a little more complex than that, you need an http server for listening requests, just use this code to understand it:

var WebSocketServer = require('websocket').server;
var http = require('http');

var server = http.createServer(function(request, response) {
    console.log((new Date()) + ' Received request for ' + request.url);
    response.writeHead(404);
    response.end();
});
server.listen(8080, function() {
    console.log((new Date()) + ' Server is listening on port 8080');
});

wsServer = new WebSocketServer({
    httpServer: server,
    // You should not use autoAcceptConnections for production
    // applications, as it defeats all standard cross-origin protection
    // facilities built into the protocol and the browser.  You should
    // *always* verify the connection's origin and decide whether or not
    // to accept it.
    autoAcceptConnections: false
});

function originIsAllowed(origin) {
  // put logic here to detect whether the specified origin is allowed.
  return true;
}

wsServer.on('request', function(request) {
    if (!originIsAllowed(request.origin)) {
      // Make sure we only accept requests from an allowed origin
      request.reject();
      console.log((new Date()) + ' Connection from origin ' + request.origin + ' rejected.');
      return;
    }

    var connection = request.accept('echo-protocol', request.origin);
    console.log((new Date()) + ' Connection accepted.');
    connection.on('message', function(message) {
        if (message.type === 'utf8') {
            console.log('Received Message: ' + message.utf8Data);
            connection.sendUTF(message.utf8Data);
        }
        else if (message.type === 'binary') {
            console.log('Received Binary Message of ' + message.binaryData.length + ' bytes');
            connection.sendBytes(message.binaryData);
        }
    });
    connection.on('close', function(reasonCode, description) {
        console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
    });
Jairo
  • 350
  • 4
  • 12