使用Edge.js,在JavaScript中调用C# .Net
来源:互联网 发布:云计算视频教程百度云 编辑:程序博客网 时间:2024/05/16 12:11
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本地图片可以这样:
1
2
3
4
var
fs = require(
'fs'
);
fs.readFile(
'Capture.jpg'
,
function
(err, data) {
console.log(data.length);
// image data
});
要使用Edge.js,使用下面的命令来安装:
1
npm install edge
创建C#文件nativeImageLoader.cs:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#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层就可以获取图像了:
1
2
3
4
5
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包:
1
npm install ws
几行代码搞定server的图像数据发送:
1
2
3
4
5
6
7
8
9
10
11
12
13
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:
1
node server.js
在客户端中接收数据:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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可以看到收到的数据:
源码
https://github.com/DynamsoftRD/WebSocket-in-JavaScript
1
git clone https://github.com/DynamsoftRD/WebSocket-in-JavaScript.git
0 0
- 使用Edge.js,在JavaScript中调用C# .Net
- 在js中调用C#中的变量
- paip..net c# 调用JS JAVASCRIPT变量与方法
- C#中使用MScript.ocx调用JS
- 在Java中使用ScriptEngine调用javascript
- C#中调用Javascript
- .net中前台javascript与后台c#函数相互调用
- .net中前台javascript与后台c#函数相互调用
- .net中前台javascript与后台c#函数相互调用
- .net中前台javascript与后台c#函数相互调用
- net中前台 javascript与后台c#函数相互调用
- .net中前台javascript与后台c#相互调用
- .net中前台javascript与后台c#函数相互调用
- net中前台javascript与后台c#相互调用::
- .net中前台javascript与后台c#相互调用
- .net中前台javascript与后台c#相互调用
- .net中前台javascript与后台c#相互调用
- .net中前台javascript与后台c#相互调用
- android实现通知栏下载更新app示例
- 【例 2.5】
- 导入excel,xls,xlsx
- Add Binary @ LeetCode java
- oracle Comment添加表备注和列备注
- 使用Edge.js,在JavaScript中调用C# .Net
- Create web project chart easily with HighCharts
- CFileDialog的信息
- ubuntu中写一个shell脚本的过程
- KKT条件
- 外网想上六唯空间
- 【伊利丹】Hadoop-2.5.0-CDH5.2.0 版本升级和数据均衡 实验记录
- Java中abstract和interface的区别
- 黄向赛的技术博客