]>
git.proxmox.com Git - mirror_novnc.git/blob - tests/playback.js
2 * noVNC: HTML5 VNC client
3 * Copyright (C) 2012 Joel Martin
4 * Licensed under MPL 2.0 (see LICENSE.txt)
8 /*jslint browser: true, white: false */
9 /*global Util, VNC_frame_data, finish */
11 var rfb
, mode
, test_state
, frame_idx
, frame_length
,
12 iteration
, iterations
, istart_time
, encoding
,
14 // Pre-declarations for jslint
15 send_array
, next_iteration
, end_iteration
, queue_next_packet
,
16 do_packet
, enable_test_mode
;
18 // Override send_array
19 send_array = function (arr
) {
20 // Stub out send_array
24 if (window
.setImmediate
=== undefined) {
25 var _immediateIdCounter
= 1;
26 var _immediateFuncs
= {};
28 window
.setImmediate = function (func
) {
29 var index
= Util
._immediateIdCounter
++;
30 _immediateFuncs
[index
] = func
;
31 window
.postMessage("noVNC immediate trigger:" + index
, "*");
35 window
.clearImmediate = function (id
) {
39 var _onMessage = function (event
) {
40 if ((typeof event
.data
!== "string") ||
41 (event
.data
.indexOf("noVNC immediate trigger:") !== 0)) {
45 var index
= event
.data
.slice("noVNC immediate trigger:".length
);
47 var callback
= _immediateFuncs
[index
];
48 if (callback
=== undefined) {
52 delete _immediateFuncs
[index
];
56 window
.addEventListener("message", _onMessage
);
59 enable_test_mode = function () {
60 rfb
._sock
.send
= send_array
;
61 rfb
._sock
.close = function () {};
62 rfb
._sock
.flush = function () {};
63 rfb
._checkEvents = function () {};
64 rfb
.connect = function (host
, port
, password
, path
) {
65 this._rfb_host
= host
;
66 this._rfb_port
= port
;
67 this._rfb_password
= (password
!== undefined) ? password
: "";
68 this._rfb_path
= (path
!== undefined) ? path
: "";
69 this._sock
.init('binary', 'ws');
70 this._rfb_connection_state
= 'connecting';
71 this._rfb_init_state
= 'ProtocolVersion';
75 next_iteration = function () {
76 rfb
= new RFB({'target': document
.getElementById('VNC_canvas'),
78 'onDisconnected': disconnected
,
79 'onNotification': notification
});
82 // Missing in older recordings
83 if (typeof VNC_frame_encoding
=== 'undefined') {
84 var frame
= VNC_frame_data
[0];
85 var start
= frame
.indexOf('{', 1) + 1;
86 if (frame
.slice(start
).startsWith('UkZC')) {
92 encoding
= VNC_frame_encoding
;
95 if (iteration
=== 0) {
96 frame_length
= VNC_frame_data
.length
;
97 test_state
= 'running';
100 if (test_state
!== 'running') { return; }
103 if (iteration
> iterations
) {
109 istart_time
= (new Date()).getTime();
110 rfb
.connect('test', 0, "bogus");
116 end_iteration = function () {
117 if (rfb
._display
.pending()) {
118 rfb
._display
.set_onFlush(function () {
124 rfb
._display
.flush();
130 queue_next_packet = function () {
131 var frame
, foffset
, toffset
, delay
;
132 if (test_state
!== 'running') { return; }
134 frame
= VNC_frame_data
[frame_idx
];
135 while ((frame_idx
< frame_length
) && (frame
.charAt(0) === "}")) {
136 //Util.Debug("Send frame " + frame_idx);
138 frame
= VNC_frame_data
[frame_idx
];
141 if (frame
=== 'EOF') {
142 Util
.Debug("Finished, found EOF");
146 if (frame_idx
>= frame_length
) {
147 Util
.Debug("Finished, no more frames");
152 if (mode
=== 'realtime') {
153 foffset
= frame
.slice(1, frame
.indexOf('{', 1));
154 toffset
= (new Date()).getTime() - istart_time
;
155 delay
= foffset
- toffset
;
160 setTimeout(do_packet
, delay
);
162 window
.setImmediate(do_packet
);
166 var bytes_processed
= 0;
168 do_packet = function () {
169 // Avoid having an excessive queue buildup
170 if (rfb
._flushing
&& (mode
!== 'realtime')) {
171 rfb
._display
.set_onFlush(function () {
172 rfb
._display
.set_onFlush(rfb
._onFlush
.bind(rfb
));
179 //Util.Debug("Processing frame: " + frame_idx);
180 var frame
= VNC_frame_data
[frame_idx
],
181 start
= frame
.indexOf('{', 1) + 1;
183 if (encoding
=== 'base64') {
184 u8
= Base64
.decode(frame
.slice(start
));
187 u8
= new Uint8Array(frame
.length
- start
);
188 for (var i
= 0; i
< frame
.length
- start
; i
++) {
189 u8
[i
] = frame
.charCodeAt(start
+ i
);
192 bytes_processed
+= u8
.length
;
193 rfb
._sock
._recv_message({'data' : u8
});