現在、サーバー上で実行され、ブラウザーにデータを表示するシミュレーターを開発しています。
ファイルの提供、通信などには Node.js を使用したいと考えています。しかし、計算部門で思うように動作するかどうかわからないので、シミュレーション部分を C++ で開発したいと考えています。
シミュレーションは個別の「世界」に分割され、すべていくつかの初期パラメーターで始まります。
これを行う最善の方法は何ですか?
現在、サーバー上で実行され、ブラウザーにデータを表示するシミュレーターを開発しています。
ファイルの提供、通信などには Node.js を使用したいと考えています。しかし、計算部門で思うように動作するかどうかわからないので、シミュレーション部分を C++ で開発したいと考えています。
シミュレーションは個別の「世界」に分割され、すべていくつかの初期パラメーターで始まります。
これを行う最善の方法は何ですか?
V8 では、JavaScript から C++ コードを呼び出すことができます。
したがって、コードの 3 つの部分を使用できます。
World
。World
クラスの一部を「見る」ことができるようにします。まず、V8 と C++ の通信方法を理解します。Google はこれに関するガイドを提供しています: https://developers.google.com/v8/embed
次に、node.js 固有の接着剤が必要です。http://www.slideshare.net/nsm.nikhil/writing-native-bindings-to-nodejs-in-cおよびhttp://syskall.com/how-to-write-your-own-native-nodejsを参照してください。 -拡大
上記のスライド共有リンクから:
#include <v8.h>
#include <node.h>
using namespace v8;
extern "C" {
static void init(Handle<Object> target) {}
NODE_MODULE(module_name, init)
}
それをあなたが望むものにより近いものに拡張することができます:
src/world.h
#ifndef WORLD_H_
#define WORLD_H_
class World {
public:
void update();
};
extern World MyWorld;
#endif
src/world.cpp
#include "world.h"
#include <iostream>
using std::cout;
using std::endl;
World MyWorld;
void World::update() {
cout << "Updating World" << endl;
}
src/bind.cpp
#include <v8.h>
#include <node.h>
#include "world.h"
using namespace v8;
static Handle<Value> UpdateBinding(const Arguments& args) {
HandleScope scope;
MyWorld.update();
return Undefined();
}
static Persistent<FunctionTemplate> updateFunction;
extern "C" {
static void init(Handle<Object> obj) {
v8::HandleScope scope;
Local<FunctionTemplate> updateTemplate = FunctionTemplate::New(UpdateBinding);
updateFunction = v8::Persistent<FunctionTemplate>::New(updateTemplate);
obj->Set(String::NewSymbol("update"), updateFunction->GetFunction());
}
NODE_MODULE(world, init)
}
デモ/demo.js
var world = require('../build/Release/world.node');
world.update();
wscript
def set_options(opt):
opt.tool_options("compiler_cxx")
def configure(conf):
conf.check_tool("compiler_cxx")
conf.check_tool("node_addon")
def build(bld):
obj = bld.new_task_gen("cxx", "shlib", "node_addon")
obj.cxxflags = ["-g", "-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE", "-Wall"]
# This is the name of our extension.
obj.target = "world"
obj.source = "src/world.cpp src/bind.cpp"
obj.uselib = []
Linux シェルでは、いくつかのセットアップ:
node-waf configure
ビルドするには、次を実行します。
node-waf
テストする:
node demo/demo.js
出力:
Updating World