Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 7b8e1e6

Browse files
committed
fix(WebSocket): patch WebSocket instance
1 parent 4a48f96 commit 7b8e1e6

File tree

3 files changed

+96
-2
lines changed

3 files changed

+96
-2
lines changed

Diff for: test/patch/WebSocket.spec.js

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
'use strict';
2+
3+
describe('WebSocket', function () {
4+
var socket,
5+
TEST_SERVER_URL = 'ws://localhost:8001',
6+
flag;
7+
8+
beforeEach(function (done) {
9+
socket = new WebSocket(TEST_SERVER_URL);
10+
socket.addEventListener('open', done);
11+
});
12+
13+
afterEach(function () {
14+
socket.close();
15+
});
16+
17+
it('should work with addEventListener', function () {
18+
var parent = window.zone;
19+
20+
socket.addEventListener('message', function (contents) {
21+
expect(window.zone.parent).toBe(parent);
22+
expect(contents).toBe('HI');
23+
done();
24+
});
25+
socket.send('hi');
26+
});
27+
28+
it('should respect removeEventListener', function (done) {
29+
var log = '';
30+
function logOnMessage () {
31+
log += 'a';
32+
33+
expect(log).toEqual('a');
34+
35+
socket.removeEventListener('message', logOnMessage);
36+
socket.send('hi');
37+
38+
setTimeout(function () {
39+
expect(log).toEqual('a');
40+
done();
41+
}, 10);
42+
};
43+
44+
socket.addEventListener('message', logOnMessage);
45+
socket.send('hi');
46+
});
47+
48+
it('should work with onmessage', function (done) {
49+
var parent = window.zone;
50+
socket.onmessage = function (contents) {
51+
expect(window.zone.parent).toBe(parent);
52+
expect(contents.data).toBe('hi');
53+
done();
54+
};
55+
socket.send('hi');
56+
});
57+
58+
59+
it('should only allow one onmessage handler', function (done) {
60+
var log = '';
61+
socket.onmessage = function () {
62+
log += 'a';
63+
expect(log).toEqual('b');
64+
done();
65+
};
66+
socket.onmessage = function () {
67+
log += 'b';
68+
expect(log).toEqual('b');
69+
done();
70+
};
71+
72+
socket.send('hi');
73+
});
74+
75+
it('should handle removing onmessage', function () {
76+
var log = '';
77+
socket.onmessage = function () {
78+
log += 'a';
79+
};
80+
socket.onmessage = null;
81+
82+
socket.send('hi');
83+
expect(log).toEqual('');
84+
});
85+
86+
});

Diff for: test/ws-server.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var ws = require('nodejs-websocket');
2+
3+
// simple echo server
4+
ws.createServer(function (conn) {
5+
conn.on('text', function (str) {
6+
conn.sendText(str.toString());
7+
});
8+
}).listen(8001);

Diff for: zone.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -414,12 +414,12 @@ Zone.patchViaCapturingAllTheEvents = function () {
414414
});
415415
};
416416

417-
417+
// we have to patch the instance since the proto is non-configurable
418418
Zone.patchWebSocket = function() {
419419
var WS = window.WebSocket;
420420
window.WebSocket = function(a, b) {
421421
var socket = arguments.length > 1 ? new WS(a, b) : new WS(a);
422-
Zone.patchProperties(socket, ['onmessage']);
422+
Zone.patchProperties(socket, ['onclose', 'onerror', 'onmessage', 'onopen']);
423423
return socket;
424424
};
425425
}

0 commit comments

Comments
 (0)