2012-04-15 45 views
2

所以我对C相当陌生,并且在一个简单的节点本机扩展上工作。C节点本地扩展调用方法两次

这里是我的扩展名为helloworld.c

Handle<Value> Method(const Arguments& args) { 
    printf(":%s:\n", "Calling Method"); 
    //SendByte(bdrate,'1'); 
    HandleScope scope; 
    if(toggleLight()==0){ 
    printf(":%s:\n", "Turning On"); 
    return scope.Close(String::New("Turned On")); 
    } 
    else{ 
printf(":%s:\n", "Turning Off"); 
return scope.Close(String::New("Turned Off")); 
    } 
} 

void init(Handle<Object> target) { 
    printf(":%s:\n", "Init"); 
    target->Set(String::NewSymbol("hello"), 
     FunctionTemplate::New(Method)->GetFunction()); 
} 
NODE_MODULE(helloworld, init) 

我消耗的代码前面通过以下Node.js的类...

var addon = require('./build/Release/helloworld'); 

var http = require("http"); 

http.createServer(function(request, response) { 
    response.writeHead(200, {"Content-Type": "text/plain"}); 
    response.write(addon.hello()); 
    response.end(); 
}).listen(8888); 

当我打电话我看到的网站以下在我的终端

~/Desktop/hellonode$ node testnode 
:Init: 
:Calling Method: 
:Turning Off: 
:Calling Method: 
:Turning On: 

为什么它似乎在调用该方法两次?我相信答案很明显,但我看不到它。

+3

嗯,你可以决定是否使用C或C++?在这里,你在.c文件中混合了C和C++ ...。哇。 – Griwes 2012-04-15 17:18:57

回答