gpt4 book ai didi

javascript - 从前端javascript中加载的DLL调用函数(在客户端javascript中加载dll)

转载 作者:太空狗 更新时间:2023-10-29 20:37:26 24 4
gpt4 key购买 nike

我有一个简单的客户端 javascript 应用程序。我希望它加载一个 DLL 文件(用于 SwipeReader CR100)并从 javascript 代码中调用 DLL 库中的函数。第二个是为 SwipeReader 触发的事件添加监听器,例如 DocumentRead 或 DocumentReadError 并在 javascript 中处理它们。

所以,我有 4 个小问题需要解决:

  1. 在 javascript(主要是 Chrome V8 引擎)中加载 DLL。
  2. 在 DLL 中调用函数。
  3. 为 DLL 中触发的事件添加监听器。
  4. 在带有响应对象的回调上在 JS 中做一些事情(警报、console.log 数据)

以前有没有人这样做过,或者这有可能吗?

先谢谢你,丹尼尔。

最佳答案

我已经做到了。解决方案是使用 EdgeJS作为 NodeJS V8 和 C# CLR 之间的桥梁。 Edge 加载 DLL 并在 V8 和 CLR 之间创建通信 channel ,消息是 Func<object, Task<object>> 形式的函数或 function(payload, callback)在每种语言 VM 之间编码。

我将在下面发布代码示例:
C#

public abstract class NetWebSocket
{
private Func<object, Task<object>> SendImpl { get; set; }

protected NetWebSocket(Func<object, Task<object>> sendImpl)
{
this.SendImpl = sendImpl;
}

protected abstract Task ReceiveAsync(string message);

public Func<object, Task<object>> ReceiveImpl
{
get
{
return async (input) =>
{
Console.Out.WriteLine(input);
await this.ReceiveAsync((string) input);
return Task.FromResult<object>(null);
};
}
}

protected async Task SendAsync(string message)
{
await this.SendImpl(message);
return;
}
}

public class MyNetWebSocketImpl : NetWebSocket
{
public CHello module;
private string JSONCodelineDataRepr = "not set";

public MyNetWebSocketImpl(Func<object, Task<object>> sendImpl) : base(sendImpl)
{
// do other stuff after calling the super class constructor
module = new CHello();
module.DocumentReadEvent += this.DocumentReadEventHandler;
module.DocumentReadErrorEvent += this.DocumentReadErrorEventHandler;
// uncomment after the websocket communication works
module.Start();
}

protected override async Task ReceiveAsync(string message)
{
// not really needed because only the NodeJS Server listens to C# .NET Server messages
Console.WriteLine(message);
if (message.Equals("shutdown"))
{
module.Close();
}
// not necessary (can comment the send function call)
// if I eventually receive a message, respond with the JSON representation of the Patient ID Card
await this.SendAsync("I received a message from you, but I'll ignore it and send you the Patient" +
" ID Card Data instead.. I'm a fish, so start phishing! PersonData = " +
JSONCodelineDataRepr);
return;
}

private async void DocumentReadEventHandler(string args)
{
this.JSONCodelineDataRepr = args;
await this.SendAsync(args);
}

private async void DocumentReadErrorEventHandler(string args)
{
await this.SendAsync(args);
}
}

public class Startup
{

public static MyNetWebSocketImpl ws;

public async Task<object> Invoke(Func<object, Task<object>> sendImpl)
{
ws = new MyNetWebSocketImpl(sendImpl);

return ws.ReceiveImpl;
}
}

Javascript/节点

(function(e,d,g,e){

var edge = require('edge'),
http = require('http'),
WebSocketServer = require('ws').Server,
swipe = edge.func('./dlls/ActiveXCOM.dll');

var server = http.createServer(function(req,res){
res.writeHead(200, {'Content-Type' : 'text/html'});
res.end((
function () { /*
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id='jsonOutput'>
</div>
<script>
var ws = new WebSocket('ws://' + window.document.location.host);

ws.onmessage = function (event) {
console.log(event.data);
var div = document.getElementById('jsonOutput');
div.innerHTML = event.data;
}

ws.onopen = function (event) {
// send something to the server
ws.send('I am the client from the browser calling you, the NodeJS WebSocketServer!');
}

ws.onclose = function (event) {
alert('websocket closing');
}

window.onbeforeunload = function myonbeforeUnload() {
return "Are you sure?";
}

window.onunload = function myonunload() {
confirm('Are you really sure?');
ws.close();
return "Are you really sure?";
}
</script>
</body>
</html>
*/}).toString().match(/[^]*\/\*([^]*)\*\/\}$/)[1]);
});

var wss = new WebSocketServer({server: server});

wss.on('connection', function(ws) {
var sendImpl = function (message, callback) {
console.log(message);
ws.send(message);
callback();
};

var receiveHandler = swipe(sendImpl, true);

ws.on('message', function (message) {
receiveHandler(message);
});

ws.on('close', function close(){
console.log('****************************The client disconnected!');
receiveHandler('shutdown');
delete receiveHandler;
});
});

server.listen(process.env.PORT || 8080);
module.exports = this;
})();

我希望实现是明确的。如果您在理解它时遇到任何困难,请随时给我写信。

编码愉快!

[不要听信反对者的话!]

关于javascript - 从前端javascript中加载的DLL调用函数(在客户端javascript中加载dll),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34791334/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com