Javascript 与 C++ 通信
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14973224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript communicate with C++
提问by Rob
I have a desktop application that has a C++ backend and a HTML5/JS front end. We are currently having to use Google V8 or Mac Webview and Objective-C to allow Javascript and C++ to communicate. Is there any way to have them directly talk to each other without the middleware?
我有一个桌面应用程序,它有一个 C++ 后端和一个 HTML5/JS 前端。我们目前不得不使用 Google V8 或 Mac Webview 和 Objective-C 来允许 Javascript 和 C++ 进行通信。有没有办法让他们在没有中间件的情况下直接相互交谈?
Trying to accomplish:
试图实现:
- Share variables.
- Call Functions from C++ to JS.
- Call Functions from JS to C++.
- 共享变量。
- 从 C++ 调用函数到 JS。
- 将函数从 JS 调用到 C++。
I have tried googling this and everything points to the above solutions.
我试过谷歌搜索这个,一切都指向上述解决方案。
采纳答案by Talon876
You could try using Google's Protocol Bufferswhich allows you to create data objects that get compiled to C++ objects. You could then use one of the following projects from their wikito use protobuffers with javascript:
您可以尝试使用 Google 的Protocol Buffers,它允许您创建编译为 C++ 对象的数据对象。然后,您可以使用他们wiki中的以下项目之一来将 protobuffers 与 javascript 一起使用:
回答by Dan Ross
Your software sounds like a lot like a web app, without the internet in the middle. NodeJSmight make good middleware in this case, you can write modulesfor it in C++, and use them in javascript. I haven't done this yet myself. A short example:)
你的软件听起来很像一个网络应用程序,中间没有互联网。在这种情况下,NodeJS可能是很好的中间件,你可以用C++ 为它编写模块,然后在 javascript 中使用它们。我自己还没有这样做。一个简短的例子:)
And now a few years layerwe have the Electron project, which is basically the above but with a Chrome based web view.
现在几年我们有了Electron 项目,它基本上是上面的,但是有一个基于 Chrome 的 web 视图。
回答by DeveloperPrime
I wrote a library for that
我为此写了一个库
Take a look to https://skywarpcpp.wordpress.com
看看https://skywarpcpp.wordpress.com
SKYWARP++ Data streaming and rpc enabler server library for c++ Applications
SKYWARP++ 数据流和 c++ 应用程序的 rpc 启用程序服务器库
You can download it at github
你可以在github下载
回答by jjrv
You can now use Electronto get basically a Chrome web browser with direct access to the Node.js API. Then you can use nbindto easily call C++ code from JavaScript. This way both share the same thread and heap.
你现在可以使用Electron来获得基本的 Chrome 网络浏览器,它可以直接访问 Node.js API。然后您可以使用nbind从 JavaScript 轻松调用 C++ 代码。这样两者共享相同的线程和堆。
You declare the C++ classes by adding something like this to a C++11 source file:
您可以通过向 C++11 源文件中添加以下内容来声明 C++ 类:
NBIND_CLASS(X) {
construct<int, int>();
method(Y);
method(Z);
}
That creates bindings for a class X with a constructor taking 2 ints and methods Y and Z with pretty much any kind of arguments and optional return values.
这为类 X 创建了绑定,其构造函数采用 2 个整数,方法 Y 和 Z 具有几乎任何类型的参数和可选的返回值。
nbindcomes with an example for how to package your C++ code into a Node.js addon, for use in Electron.
nbind附带一个示例,说明如何将您的 C++ 代码打包到 Node.js 插件中,以便在 Electron 中使用。
回答by Anand Rathi
Please have a look at this , I am the owner
请看这个,我是业主
http://code.google.com/p/libjspp/
http://code.google.com/p/libjspp/
libjspp allows easy interfacing & interacting of C++ with javascript spidermonkey 1.8.5? Non intrusive to C++ & javascript world at same time. Useful for applications which want to Embed & Extend Javascript in object oriented manner. Very thin interface virtually no overhead by using C++ templates.
libjspp 允许 C++ 与 javascript spidermonkey 1.8.5 轻松接口和交互?同时不侵入 C++ 和 javascript 世界。对于想要以面向对象的方式嵌入和扩展 Javascript 的应用程序很有用。通过使用 C++ 模板,非常薄的界面几乎没有开销。
I am
我是

