UNPKG

768 BJavaScriptView Raw
1const SockJS = require('sockjs-client/dist/sockjs');
2const safeThis = require('./utils/safeThis');
3
4/**
5 * A SockJS client adapted for use with webpack-dev-server.
6 * @constructor
7 * @param {string} url The socket URL.
8 */
9function SockJSClient(url) {
10 this.socket = new SockJS(url);
11}
12
13/**
14 * Creates a handler to handle socket close events.
15 * @param {function(): void} fn
16 */
17SockJSClient.prototype.onClose = function onClose(fn) {
18 this.socket.onclose = fn;
19};
20
21/**
22 * Creates a handler to handle socket message events.
23 * @param {function(*): void} fn
24 */
25SockJSClient.prototype.onMessage = function onMessage(fn) {
26 this.socket.onmessage = function onMessageHandler(event) {
27 fn(event.data);
28 };
29};
30
31safeThis.__webpack_dev_server_client__ = SockJSClient;