繁体   English   中英

在不终止node.js的情况下运行javascript程序

[英]run a javascript program without terminating inside node.js

我想运行一个JavaScript程序而不终止在node.js内部:---

using while(1);是否是正确的方法?

在这个javascript程序中,我创建了一个websocket并列出了它。
每当数据出现在websocket上时,它将抛出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');
}

跑 : - -
节点test.js

while(1)不是一个好主意,因为它会阻塞您的程序并消耗大量处理器能力。

可能还有其他方法可以做到这一点,但我能想到的最简单的方法是使用setInterval
如果没有什么要执行的,则可以使用空函数。

setInterval(function(){}, 10000);

比这稍微复杂一点,您需要一个http服务器来监听请求,只需使用以下代码即可理解它:

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.');
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM