Using Edge.js, call C #. Net in JavaScript

Source: Internet
Author: User
Tags node server git clone

Edge.js enables developers to invoke C # interfaces in JavaScript to improve application extensibility. Here's how to call the C # interface to get the picture data and send it to the Web client through the WebSocket server built by node. js.

Reference: How to useedge.js to Empower WebSocket Solutions in JavaScript

Pass. NET interface gets the picture back to JavaScript

Let's take a look at the simple use of JavaScript to load local images:

var fs = require (' FS '); Fs.readfile (' Capture.jpg ', function (err, data) {Console.log (data.length);//image data});

To use Edge.js, use the following command to install:

NPM Install Edge

Create C # file NativeImageLoader.cs:

#r   "System.Drawing.dll" using system.threading.tasks;using system.drawing; public  Class startup{    public async task<object> invoke (object  Input)     {           byte[]  Imagebuffer;        image image = image.fromfile (" Capture.jpg ");        using  (System.io.memorystream stream  = new system.io.memorystream ())         {             image. Save (stream, system.drawing.imaging.imageformat.jpeg);             imagebuffer = stream. GetBuffer ();        }          return imagebuffer;    }} 

By default, the system DLLs loaded by the Edge are only mscorlib.dll and System.dll, so they need to be added manually by #r "System.Drawing.dll" .

Now you can get the image on the JavaScript layer:

var nativeimageloader = Edge.func (Require (' path '). Join (__dirname, ' NativeImageLoader.cs '); Nativeimageloader (' Load            ', function (error, result) {if (error) throw error; Result is the loaded image});
Create a WebSocket solution using node. js

First install the WebSocket package:

NPM Install WS

A few lines of code take care of the server's image data sending:

var websocketserver = require (' ws '). Server, WSS = new Websocketserver ({port:8080}), Wss.on (' Connection ', function (WS) {ws.on (' message ', functio        N (message) {Console.log (' Received:%s ', message);            Nativeimageloader (' Load ', function (error, result) {if (error) throw error; Ws.send (result);    Send the captured image}); });});

Run Server:

Node Server.js

To receive data in the client:

Var ws = new websocket ("ws://127.0.0.1:8080/");      ws.binarytype  =  "Arraybuffer";  ws.onopen = function ()  {        alert ("opened");        ws.send ("I ' M dynamsoft");     };     ws.onmessage = function  (EVT)  {          var bytes = new uint8array (Evt.data);     var data =  "";    var len =  bytes.bytelength;    for  (var i = 0; i < len; + +i)  {        data += string.fromcharcode (Bytes[i]);     }    var img = document.getelementbyid ("image");     img.src =  "Data:image/png;base64, "+window.btoa (data);    };     ws.onclose =  function ()  {       alert ("Closed");    };      ws.onerror = function (ERR)  {        alert ("error: "  + err);    };

Open client.htm to see the data received:

Source

Https://github.com/DynamsoftRD/WebSocket-in-JavaScript

git clone https://github.com/DynamsoftRD/WebSocket-in-JavaScript.git


Using Edge.js, call C #. Net in JavaScript

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.