Skip to content

Commit c6ee4d5

Browse files
knagaitsevevilebottnawi
authored andcommitted
feat(experimentally): websocket client implementation (#2058)
1 parent 17b8819 commit c6ee4d5

File tree

5 files changed

+104
-3
lines changed

5 files changed

+104
-3
lines changed

client-src/clients/WebsocketClient.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
'use strict';
22

3+
/* global WebSocket */
4+
5+
/* eslint-disable
6+
no-unused-vars
7+
*/
38
const BaseClient = require('./BaseClient');
49

5-
module.exports = class WebsocketClient extends BaseClient {};
10+
module.exports = class WebsocketClient extends BaseClient {
11+
constructor(url) {
12+
super();
13+
this.client = new WebSocket(url.replace(/^http/, 'ws'));
14+
}
15+
16+
static getClientPath(options) {
17+
return require.resolve('./WebsocketClient');
18+
}
19+
20+
onOpen(f) {
21+
this.client.onopen = f;
22+
}
23+
24+
onClose(f) {
25+
this.client.onclose = f;
26+
}
27+
28+
// call f with the message string as the first argument
29+
onMessage(f) {
30+
this.client.onmessage = (e) => {
31+
f(e.data);
32+
};
33+
}
34+
};

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"url": "^0.11.0",
6767
"webpack-dev-middleware": "^3.7.0",
6868
"webpack-log": "^2.0.0",
69+
"ws": "^6.2.1",
6970
"yargs": "12.0.5"
7071
},
7172
"devDependencies": {
@@ -107,8 +108,7 @@
107108
"tcp-port-used": "^1.0.1",
108109
"url-loader": "^1.1.2",
109110
"webpack": "^4.35.0",
110-
"webpack-cli": "^3.3.5",
111-
"ws": "^6.2.1"
111+
"webpack-cli": "^3.3.5"
112112
},
113113
"peerDependencies": {
114114
"webpack": "^4.0.0"
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict';
2+
3+
const http = require('http');
4+
const express = require('express');
5+
const ws = require('ws');
6+
const WebsocketClient = require('../../../client-src/clients/WebsocketClient');
7+
const port = require('../../ports-map').WebsocketClient;
8+
9+
describe('WebsocketClient', () => {
10+
let socketServer;
11+
let listeningApp;
12+
13+
beforeAll((done) => {
14+
// eslint-disable-next-line new-cap
15+
const app = new express();
16+
17+
listeningApp = http.createServer(app);
18+
listeningApp.listen(port, 'localhost', () => {
19+
socketServer = new ws.Server({
20+
server: listeningApp,
21+
path: '/ws-server',
22+
});
23+
done();
24+
});
25+
});
26+
27+
describe('client', () => {
28+
it('should open, receive message, and close', (done) => {
29+
socketServer.on('connection', (connection) => {
30+
connection.send('hello world');
31+
32+
setTimeout(() => {
33+
connection.close();
34+
}, 1000);
35+
});
36+
37+
const client = new WebsocketClient(`http://localhost:${port}/ws-server`);
38+
const data = [];
39+
40+
client.onOpen(() => {
41+
data.push('open');
42+
});
43+
client.onClose(() => {
44+
data.push('close');
45+
});
46+
client.onMessage((msg) => {
47+
data.push(msg);
48+
});
49+
50+
setTimeout(() => {
51+
expect(data).toMatchSnapshot();
52+
done();
53+
}, 3000);
54+
});
55+
});
56+
57+
afterAll((done) => {
58+
listeningApp.close(() => {
59+
done();
60+
});
61+
});
62+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`WebsocketClient client should open, receive message, and close 1`] = `
4+
Array [
5+
"open",
6+
"hello world",
7+
"close",
8+
]
9+
`;

test/ports-map.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const portsList = {
3535
'sockPath-option': 1,
3636
'stats-option': 1,
3737
ProvidePlugin: 1,
38+
WebsocketClient: 1,
3839
};
3940

4041
let startPort = 8079;

0 commit comments

Comments
 (0)