]> git.proxmox.com Git - mirror_novnc.git/blob - tests/playback.js
Enable noVNC to become Browserifiable
[mirror_novnc.git] / tests / playback.js
1 /*
2 * noVNC: HTML5 VNC client
3 * Copyright (C) 2012 Joel Martin
4 * Licensed under MPL 2.0 (see LICENSE.txt)
5 */
6
7 "use strict";
8 /*jslint browser: true, white: false */
9 /*global Util, VNC_frame_data, finish */
10
11 var rfb, mode, test_state, frame_idx, frame_length,
12 iteration, iterations, istart_time,
13
14 // Pre-declarations for jslint
15 send_array, next_iteration, queue_next_packet, do_packet, enable_test_mode;
16
17 // Override send_array
18 send_array = function (arr) {
19 // Stub out send_array
20 };
21
22 enable_test_mode = function () {
23 rfb._sock._mode = VNC_frame_encoding;
24 rfb._sock.send = send_array;
25 rfb._sock.close = function () {};
26 rfb._sock.flush = function () {};
27 rfb._checkEvents = function () {};
28 rfb.connect = function (host, port, password, path) {
29 this._rfb_host = host;
30 this._rfb_port = port;
31 this._rfb_password = (password !== undefined) ? password : "";
32 this._rfb_path = (path !== undefined) ? path : "";
33 this._sock.init('binary', 'ws');
34 this._updateState('ProtocolVersion', "Starting VNC handshake");
35 };
36 };
37
38 next_iteration = function () {
39 rfb = new RFB({'target': document.getElementById('VNC_canvas'),
40 'onUpdateState': updateState});
41 enable_test_mode();
42
43 if (iteration === 0) {
44 frame_length = VNC_frame_data.length;
45 test_state = 'running';
46 }
47
48 if (test_state !== 'running') { return; }
49
50 iteration += 1;
51 if (iteration > iterations) {
52 finish();
53 return;
54 }
55
56 frame_idx = 0;
57 istart_time = (new Date()).getTime();
58 rfb.connect('test', 0, "bogus");
59
60 queue_next_packet();
61
62 };
63
64 queue_next_packet = function () {
65 var frame, foffset, toffset, delay;
66 if (test_state !== 'running') { return; }
67
68 frame = VNC_frame_data[frame_idx];
69 while ((frame_idx < frame_length) && (frame.charAt(0) === "}")) {
70 //Util.Debug("Send frame " + frame_idx);
71 frame_idx += 1;
72 frame = VNC_frame_data[frame_idx];
73 }
74
75 if (frame === 'EOF') {
76 Util.Debug("Finished, found EOF");
77 next_iteration();
78 return;
79 }
80 if (frame_idx >= frame_length) {
81 Util.Debug("Finished, no more frames");
82 next_iteration();
83 return;
84 }
85
86 if (mode === 'realtime') {
87 foffset = frame.slice(1, frame.indexOf('{', 1));
88 toffset = (new Date()).getTime() - istart_time;
89 delay = foffset - toffset;
90 if (delay < 1) {
91 delay = 1;
92 }
93
94 setTimeout(do_packet, delay);
95 } else {
96 setTimeout(do_packet, 1);
97 }
98 };
99
100 var bytes_processed = 0;
101
102 do_packet = function () {
103 //Util.Debug("Processing frame: " + frame_idx);
104 var frame = VNC_frame_data[frame_idx],
105 start = frame.indexOf('{', 1) + 1;
106 bytes_processed += frame.length - start;
107 if (VNC_frame_encoding === 'binary') {
108 var u8 = new Uint8Array(frame.length - start);
109 for (var i = 0; i < frame.length - start; i++) {
110 u8[i] = frame.charCodeAt(start + i);
111 }
112 rfb._sock._recv_message({'data' : u8});
113 } else {
114 rfb._sock._recv_message({'data' : frame.slice(start)});
115 }
116 frame_idx += 1;
117
118 queue_next_packet();
119 };
120