]>
git.proxmox.com Git - mirror_novnc.git/blob - tests/fake.websocket.js
4 // PhantomJS can't create Event objects directly, so we need to use this
5 function make_event(name
, props
) {
6 var evt
= document
.createEvent('Event');
7 evt
.initEvent(name
, true, true);
9 for (var prop
in props
) {
10 evt
[prop
] = props
[prop
];
16 FakeWebSocket = function (uri
, protocols
) {
18 this.binaryType
= "arraybuffer";
21 if (!protocols
|| typeof protocols
=== 'string') {
22 this.protocol
= protocols
;
24 this.protocol
= protocols
[0];
27 this._send_queue
= new Uint8Array(20000);
29 this.readyState
= FakeWebSocket
.CONNECTING
;
30 this.bufferedAmount
= 0;
32 this.__is_fake
= true;
35 FakeWebSocket
.prototype = {
36 close: function (code
, reason
) {
37 this.readyState
= FakeWebSocket
.CLOSED
;
39 this.onclose(make_event("close", { 'code': code
, 'reason': reason
, 'wasClean': true }));
43 send: function (data
) {
44 if (this.protocol
== 'base64') {
45 data
= Base64
.decode(data
);
47 data
= new Uint8Array(data
);
49 this._send_queue
.set(data
, this.bufferedAmount
);
50 this.bufferedAmount
+= data
.length
;
53 _get_sent_data: function () {
55 for (var i
= 0; i
< this.bufferedAmount
; i
++) {
56 arr
[i
] = this._send_queue
[i
];
59 this.bufferedAmount
= 0;
64 _open: function (data
) {
65 this.readyState
= FakeWebSocket
.OPEN
;
67 this.onopen(make_event('open'));
71 _receive_data: function (data
) {
72 this.onmessage(make_event("message", { 'data': data
}));
76 FakeWebSocket
.OPEN
= WebSocket
.OPEN
;
77 FakeWebSocket
.CONNECTING
= WebSocket
.CONNECTING
;
78 FakeWebSocket
.CLOSING
= WebSocket
.CLOSING
;
79 FakeWebSocket
.CLOSED
= WebSocket
.CLOSED
;
81 FakeWebSocket
.__is_fake
= true;
83 FakeWebSocket
.replace = function () {
84 if (!WebSocket
.__is_fake
) {
85 var real_version
= WebSocket
;
86 WebSocket
= FakeWebSocket
;
87 FakeWebSocket
.__real_version
= real_version
;
91 FakeWebSocket
.restore = function () {
92 if (WebSocket
.__is_fake
) {
93 WebSocket
= WebSocket
.__real_version
;