]>
git.proxmox.com Git - mirror_novnc.git/blob - tests/fake.websocket.js
1 import Base64
from '../core/base64.js';
3 // PhantomJS can't create Event objects directly, so we need to use this
4 function make_event(name
, props
) {
5 const evt
= document
.createEvent('Event');
6 evt
.initEvent(name
, true, true);
8 for (let prop
in props
) {
9 evt
[prop
] = props
[prop
];
15 export default class FakeWebSocket
{
16 constructor(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;
36 this.readyState
= FakeWebSocket
.CLOSED
;
38 this.onclose(make_event("close", { 'code': code
, 'reason': reason
, 'wasClean': true }));
43 if (this.protocol
== 'base64') {
44 data
= Base64
.decode(data
);
46 data
= new Uint8Array(data
);
48 this._send_queue
.set(data
, this.bufferedAmount
);
49 this.bufferedAmount
+= data
.length
;
53 const res
= new Uint8Array(this._send_queue
.buffer
, 0, this.bufferedAmount
);
54 this.bufferedAmount
= 0;
59 this.readyState
= FakeWebSocket
.OPEN
;
61 this.onopen(make_event('open'));
66 this.onmessage(make_event("message", { 'data': data
}));
70 FakeWebSocket
.OPEN
= WebSocket
.OPEN
;
71 FakeWebSocket
.CONNECTING
= WebSocket
.CONNECTING
;
72 FakeWebSocket
.CLOSING
= WebSocket
.CLOSING
;
73 FakeWebSocket
.CLOSED
= WebSocket
.CLOSED
;
75 FakeWebSocket
.__is_fake
= true;
77 FakeWebSocket
.replace
= () => {
78 if (!WebSocket
.__is_fake
) {
79 const real_version
= WebSocket
;
80 // eslint-disable-next-line no-global-assign
81 WebSocket
= FakeWebSocket
;
82 FakeWebSocket
.__real_version
= real_version
;
86 FakeWebSocket
.restore
= () => {
87 if (WebSocket
.__is_fake
) {
88 // eslint-disable-next-line no-global-assign
89 WebSocket
= WebSocket
.__real_version
;