在 Node.js / 服务器端 javascript 中使用 .NET DLL

发布于 2024-10-11 05:14:29 字数 873 浏览 4 评论 0原文

我有一个在线游戏的宠物项目,整个游戏引擎是用 C# 编写的,我想知道是否可以从使用 Node.JS 构建的解决方案中调用这个现有程序集 (.dll) 的函数、Socket.IO、Express 等?

游戏引擎本身相当完整;经过测试且坚固耐用。我希望有一些巧妙的方法来公开其功能,而不需要太多的开销。

更新

稍微回答一下我自己的问题.. 我已经结束了构建自己的 Web 套接字服务器(基于最新的 Web 套接字协议文档)。它是用 C# 编写的,并使用 Mono 进行编译,以便它可以托管在运行 mono 的 Linux 机器上,因此(经过一些调整)我可以使用我现有的游戏引擎。

更新2 现在存在一个完全符合我最初寻找的项目 - http://tjanczuk.github.io /edge/#/

更新 3 Edge.js 通过新的 edge-js 包支持 Node 的最新版本和 .net core。

支持 Node.Js 6.x、7.x、8.x、9.x、10.x、11.x 支持 .NET Windows/Linux/macOS 上的 Core 1.0.1 - 2.x。支持 Mono 运行时 4.8.x - 5.x。

可以从 https://www.npmjs.com/package/edge-js

I have a pet project that is an online game, the entire game engine is written in C# and I would like to know if there is anyway I can call the functions of this existing assembly (.dll) from a solution built using Node.JS, Socket.IO, Express etc?

The game engine itself is pretty complete; tested and robust. I am hoping there is some neat way of exposing its functionality without too much overhead.

UPDATE:

To answer my own question a little..
I have ended building my own web socket server (based on the most current web socket protocol document). It is written in C# and compiled using Mono so that it can be hosted on a Linux box running mono and therefore (with a few tweaks) I can use my existing game engine.

UPDATE 2
A project that does exactly what I was originally looking for now exists - http://tjanczuk.github.io/edge/#/

UPDATE 3
Edge.js supporting node's last versions and .net core with a new edge-js package.

Support for Node.Js 6.x, 7.x, 8.x, 9.x, 10.x, 11.x Support for .NET
Core 1.0.1 - 2.x on Windows/Linux/macOS. Support for Mono runtime
4.8.x - 5.x.

Can be installed from https://www.npmjs.com/package/edge-js

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

寄意 2024-10-18 05:14:29

查看我启动的 edge.js 项目 (http://tjanczuk.github.com/edge)。它提供了一种在进程中运行 .NET 和 Node.js 代码的机制。 Edge.js 允许您从 Node.js 调用 .NET 代码,并从 .NET 调用 Node.js 代码。它在 .NET 和 Node.js 之间编组数据,并协调多线程 CLR 和单线程 V8 之间的线程模型。

使用edge.js,您可以从node.js 访问预先存在的.NET 代码岛,这似乎符合您的场景。

Check out the edge.js project I started (http://tjanczuk.github.com/edge). It provides a mechanism for running .NET and node.js code in-process. Edge.js allows you to call .NET code from node.js and node.js code from .NET. It marshals data between .NET and node.js as well as reconciles the threading models between multi-threaded CLR and single threaded V8.

Using edge.js you can access islands of pre-existing .NET code from node.js, which seems to match your scenario.

日暮斜阳 2024-10-18 05:14:29

我最近面临着同样的挑战(要求从node.js javascript 调用C# 代码)。我有 1000 行复杂的 C# 代码,我真的不喜欢将它们移植到 JavaScript。

我解决了,如果如下。

  • 相关的 C# 代码基本上是 DLL 程序集中的 1-2 个类。
  • 定义了一个 COM 接口,它是 C# 类接口的子集,并在 C# 类中实现了该接口。因此,DLL 成为进程内 COM 服务器。
  • 实现了一个node.js扩展DLL,它使用标准Win32 COM API实例化我的C# COM类,并使用COM接口将方法调用从node.js javascript路由到C#代码。

如果只想朝一个方向拨打电话,这可以解决问题。我还需要从 C# 调用 javascript。这要困难得多。必须:

  • 在 node.js 扩展 DLL 中实现一个 COM 对象(ATL 在这里提供帮助)
  • 将此 COM 对象的接口引用传递给 C# 代码(COM Interop)
  • 通过 COM 对象将调用路由到 node.js 中的 V8 对象

也许如果我有一些额外的时间,我可能会用这个来做一个示例项目。

I've been recently faced with the same challenge (requirement to call C# code from node.js javascript). I had 1000s of lines of complex C# code that I really didn't like to port to javascript.

I solved if as follows.

  • The relevant C# code is basically 1-2 classes in a DLL assembly
  • Defined a COM interface which is a subset of the C# class's interface and implemented that interface in the C# class. Thus, the DLL became an in-process COM server.
  • Implemented a node.js extension DLL that instantiates my C# COM class using standard Win32 COM API and routes method calls from node.js javascript to C# code using the COM interface.

This solves the problem if one only wants to make calls in one direction. I also had the requirement to make calls from C# to javascript. This is a lot harder. One has to:

  • Implement a COM object in the node.js extension DLL (ATL helps here)
  • Pass an interface reference of this COM object to C# code (COM Interop)
  • Route calls via the COM object to V8 objects in node.js

Maybe if I have some extra time, I might make an example project out of this.

谈场末日恋爱 2024-10-18 05:14:29

如果您只想启动一个轻量级 HTTP 服务器,同时仍然使用 C# 和 .Net 进行编程,那么您应该给 Kayak 一个机会。它是一个用于 C# 的轻量级 HTTP 服务器,从这个意义上来说,它的行为有点像 Node.js。

kayakhttp

更新:

如果您正在寻找一个轻量级 HTTP 服务器来处理 Web 请求今天你有几个选择:

  • ServiceStack(推荐)
  • Microsoft WebAPI
  • NancyFx

据我所知,上述所有内容都适用于某些版本的 Mono,因此您仍然可以在基于 Windows 和 Unix 的系统上托管它们。

If all you want to do is spin up a lightweight HTTP server while still programming with C# and .Net you should give Kayak a chance. It is a lightweight HTTP Server for C# and behaves kind of like node.js in that sense.

kayakhttp

Update:

If you are looking for a lightweight HTTP Server to handle web requests you have a couple alternatives today:

  • ServiceStack (recommended)
  • Microsoft WebAPI
  • NancyFx

To my knowledge all the above work on some version of Mono, so you can still host them across both Windows and Unix based systems.

叹倦 2024-10-18 05:14:29

可以编写 .Net 插件,简而言之,您可以编写常规本机插件并通过 CLI/C++ 调用添加 .Net 调用到 .Net dll。

在实践中,您通常会创建一个 C# dll 库,然后从 CLI/C++ 节点插件项目中调用该库。有一些微妙之处,例如确保实际的节点添加定义文件是在没有 CLR 支持的情况下编译的,以便节点可以正确加载它。

您可以查看:https://github.com/saary/node.net
有关如何实现这一目标的示例。

.Net addons can be written, in short you write a regular native addon and add .Net calls via CLI/C++ calls to .Net dlls.

In practice you usually create a C# dll library which you then call from a CLI/C++ node addon project. There is a bit of delicacies such as making sure that the actual node add on definition file is compiled without CLR support so node can load it correctly.

You can check out: https://github.com/saary/node.net
for an example of how this can be achieved.

疯到世界奔溃 2024-10-18 05:14:29

以下答案已过时,但仍然有助于理解第一个版本的 Node.js
Node.js 现在也可在 nodejs.org 上直接用于 Windows。没有 cygwin 要求或其他要求。

首先,目前 Node.js 没有原生 Windows 端口,只有 cygwin 版本(但我怀疑你已经知道了)。

GitHub 上的某个地方有一个节点模块,它提供了用于调用本机库的包装器,但是 iirc,它仅适用于 .so 库。

因此,如果你想使用 C# DLL,你首先必须编写一个原生 Node.js 扩展作为接口:
https://www.cloudkick.com/blog /2010/aug/23/writing-nodejs-native-extensions/

从该扩展中,您必须加载 DLL 并将 Node.js 的调用包装到 C# 代码,这意味着您必须编写一些低级别的代码C/C++ 代码并将 C# 值转换为 V8 内容。

我只有 C++ 和 V8 的经验,上手有点困难,因为代码示例有点稀疏,而且包装 C++ 类也不是那么简单。但我确实写了一些小的 JS 游戏引擎之类的东西,它使用 C++ OpenGL 后端,它是未完成(几乎没有任何评论),但它可能会给你一些想法。

注意: 有一些项目在某种程度上为 V8 提供了自动生成包装器,但这些项目仅限于 C++。

总而言之,我认为让 C# 包装器正常工作将是相当冒险的,但它应该是可能的。

The following answer is out of date, but still helpful for understanding of Node.js from first release
Node.js is now also available natively for Windows at nodejs.org. No cygwin requirement or otherwise.

First of all, at the moment there's no native Windows port of Node.js, there's only a cygwin version (but I suspect you already knew that).

There was a node module floating around somewhere at the GitHubs that provided wrappers for calling into native libraries, but iirc, that only worked with .so libs.

Therefore, if you want to use a C# DLL, you will first have to write a native Node.js extension as the interface:
https://www.cloudkick.com/blog/2010/aug/23/writing-nodejs-native-extensions/

From that extension you have to load the DLL and wrap the calls from Node.js to the C# code, that means you have to write some low level C/C++ code and convert C# values to V8 stuff.

I only have experience with C++ and V8, it's a bit hard to get started since the code examples are a bit sparse, also wrapping C++ classes is not that trivial. But I did wrote small JS game engine kind of thing, that uses a C++ OpenGL backend, it's unfinished (and there are hardly any comments) but it might give you some ideas.

Note: There are some projects in the wild that provide somewhat automatic generation of wrappers to V8, but those are C++ only.

So to conclude, I think it will be quite adventurous getting the C# wrappers to work, but it should be possible.

吹梦到西洲 2024-10-18 05:14:29

Edge.js 通过新的 edge-js 包支持 Node 的最新版本和 .net core。

支持 Node.Js 6.x、7.x、8.x、9.x、10.x、11.x 支持 .NET

Windows/Linux/macOS 上的 Core 1.0.1 - 2.x。支持 Mono 运行时

4.8.x - 5.x。

可以从 https:// 安装 (npm i edge-js) www.npmjs.com/package/edge-js

Edge.js supporting node's last versions and .net core with a new edge-js package.

Support for Node.Js 6.x, 7.x, 8.x, 9.x, 10.x, 11.x Support for .NET

Core 1.0.1 - 2.x on Windows/Linux/macOS. Support for Mono runtime

4.8.x - 5.x.

Can be installed (npm i edge-js) from https://www.npmjs.com/package/edge-js

入画浅相思 2024-10-18 05:14:29

您可能会这个项目有一些运气,它是 Node.js 到 .NET 的端口。我自己没有使用过它,但是通过本机 .NET 实现,理论上您应该能够执行您需要的操作。

您可能还想转向另一个方向,尝试将 C# 游戏引擎移植(又名:重新编译,除非您深入使用 Windows)到 Mono,然后看看是否可以从中构建包装器。

You might have some luck with this project, which is a port of Node.js to .NET. I haven't used it myself, but with a native .NET implementation you theoretically should be able to do what you need to.

You might also want to go the other direction and try to port (aka: recompile unless you're hooked deep into Windows) your C# game engine to Mono and see if you can then build wrappers off of that.

暮光沉寂 2024-10-18 05:14:29

我知道这是一个老问题,但想给出一个当前的答案。 IIS 7.5 和 .Net 4.x 支持 Websocket,但使用 SignalR 库可能是阻力最小的途径。它类似于 NodeJS 的 socket.io 库。

至于通过 NodeJS 访问 .Net 代码,您最好的选择是 Edge.js,使用 C/C++ 构建混合本机程序集,通过命令行应用程序(最好使用管道进行输入/输出)或通过服务(TCP 或其他)。

我发现 Edge.js 非常有限,并且没有通过管道控制台接口提供太多功能……并且认为服务可能最适合更复杂的接口。此时,您可能最好在 .Net 中完成项目的其余部分,除非您对 NodeJS 进行了投资以克服上述困难。

I know it's an old question, but wanted to throw in a current answer. With IIS 7.5 and .Net 4.x Websockets are supported, though use of the SignalR library will likely be the path of least resistance. It's similar to the socket.io library for NodeJS.

As to accessing .Net code via NodeJS, your best options are Edge.js, building a mixed native assembly with C/C++, exposing your .Net code either via a command line application (best to use pipes for input/output) or via a service (TCP or other).

I find Edge.js to be very limited, and not offer much over a piped console interface.. and feel that a service may be best for a more complex interface. At which point you may be best doing the rest of the project in .Net, unless you have an investment in NodeJS that supersedes said difficulties.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文