Call Function from DLL loaded in front-end javascript (load dll in clientside javascript)

前端 未结 2 1992
南笙
南笙 2020-12-05 22:25

I have a simple clientside javascript application. I want it to load a DLL file (for a SwipeReader CR100) and call functions from the DLL library from within the javascript

2条回答
  •  被撕碎了的回忆
    2020-12-05 23:02

    I've done it. The solution is to use EdgeJS as a bridge between NodeJS V8 and C# CLR. Edge loads the DLL and creates a communication channel between V8 and CLR, the messages are functions of the form Func> or function(payload, callback) that are marshalled between each language VM.

    I'll post the code sample below:
    C#

    public abstract class NetWebSocket
    {
        private Func>  SendImpl { get; set; }
    
        protected NetWebSocket(Func> sendImpl)
        {
            this.SendImpl = sendImpl;
        }
    
        protected abstract Task ReceiveAsync(string message);
    
        public Func> ReceiveImpl
        {
            get
            {
                return async (input) =>
                {
                    Console.Out.WriteLine(input);
                    await this.ReceiveAsync((string) input);
                    return Task.FromResult(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> 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 Invoke(Func> sendImpl)
        {
            ws = new MyNetWebSocketImpl(sendImpl);
           
            return ws.ReceiveImpl;
        }
    }
    
    

    Javascript/NodeJS

    (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 () { /*
                
                
                
                
                
                    
    */}).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; })();

    I hope the implementation is clear. If you have any trouble understanding it, please don't hesitate to write to me.

    Happy Coding!

    [Don't listen to the naysayers!]

    提交回复
    热议问题