C#编程之使用Edge.js,在JavaScript中调用C# .Net
白羽 2018-07-10 来源 :网络 阅读 2299 评论 0

摘要:本文将带你了解C#编程之使用Edge.js,在JavaScript中调用C# .Net,希望本文对大家学C#/.Net有所帮助。




Edge.js能够让开发者在JavaScript中调用C#的接口,提高应用的扩展能力。这里介绍如何调用C#接口获取图片数据,并通过Node.js搭建的WebSocket server发送到Web客户端。

参考:How to Use Edge.js to Empower WebSocket Solutions in JavaScript

通过.Net接口获取图片返回给JavaScript

先看下单纯使用JavaScript来load本地图片可以这样:

var fs = require('fs');

fs.readFile('Capture.jpg', function(err, data) {

  console.log(data.length); // image data

});

 

要使用Edge.js,使用下面的命令来安装:

npm install edge

创建C#文件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;

    }

}

 

在默认情况下,edge加载的系统dll只有mscorlib.dll and System.dll,因此需要通过#r “System.Drawing.dll”手动添加。

现在在JavaScript层就可以获取图像了:

var nativeImageLoader = edge.func(require('path').join(__dirname, 'nativeImageLoader.cs'));

nativeImageLoader('load', function(error, result) {

            if (error) throw error;

            // result is the loaded image

 });

 

使用Node.js创建WebSocket解决方案

首先安装WebSocket包:

npm install ws

几行代码搞定server的图像数据发送:

var WebSocketServer = require('ws').Server,

    wss = new WebSocketServer({

        port: 8080

});

wss.on('connection', function(ws) {

    ws.on('message', function(message) {

        console.log('Received: %s', message);

        nativeImageLoader('load', function(error, result) {

            if (error) throw error;

            ws.send(result); // send the captured image

        });

    });

});

 

运行server:

node server.js

在客户端中接收数据:

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);    

};

 

打开client.htm可以看到收到的数据:

 C#编程之使用Edge.js,在JavaScript中调用C# .Net

源码

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

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



以上就介绍了C#.NET的相关知识,希望对C#.NET有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言C#.NET频道!


本文由 @白羽 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程