forked from kevin-roark/socket.io-computer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvnc.js
76 lines (60 loc) · 1.65 KB
/
vnc.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var Canvas = require('canvas');
var Emitter = require('events').EventEmitter;
var rfb = require('rfb2');
var exec = require('child_process').exec;
var Jpeg = require('jpeg').Jpeg;
var FixedJpegStack = require('jpeg').FixedJpegStack;
var fs = require('fs');
var SS_NAME = 'ss.jpg';
module.exports = VNC;
function VNC(host, port) {
this.host = host;
this.port = port;
this.displayNum = port - 5900; // vnc convention
this.width = 800;
this.height = 600;
this.r = rfb.createConnection({
host: host,
port: port
});
var self = this;
this.r.on('rect', this.drawRect.bind(this));
}
VNC.prototype.__proto__ = Emitter.prototype;
function putData(ctx, id, rect) {
ctx.putImageData(id, rect.x, rect.y);
}
VNC.prototype.drawRect = function(rect) {
if (rect.encoding != 0) {
this.emit('copy', rect);
return;
}
var date = new Date;
var rgb = new Buffer(rect.width * rect.height * 3);
for (var i = 0, o = 0; i < rect.data.length; i += 4) {
rgb[o++] = rect.data[i + 2];
rgb[o++] = rect.data[i + 1];
rgb[o++] = rect.data[i];
}
var self = this;
var image = new Jpeg(rgb, rect.width, rect.height, 'rgb');
image.encode(function(img, err){
if (img) self.emit('raw', {
x: rect.x,
y: rect.y,
width: rect.width,
height: rect.height,
image: img
});
});
if (!this.state) {
// first frame
this.state = new FixedJpegStack(this.width, this.height, 'rgb');
this.state.push(rgb, 0, 0, rect.width, rect.height);
} else {
this.state.push(rgb, rect.x, rect.y, rect.width, rect.height);
}
this.state.encode(function(img, err) {
if (img) self.emit('frame', img);
});
};