source: frontend/node_modules/webpack-dev-server/lib/servers/WebsocketServer.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.4 KB
RevLine 
[9af201e]1"use strict";
2
3const WebSocket = require("ws");
4const BaseServer = require("./BaseServer");
5
6/** @typedef {import("../Server").WebSocketServerConfiguration} WebSocketServerConfiguration */
7/** @typedef {import("../Server").ClientConnection} ClientConnection */
8
9module.exports = class WebsocketServer extends BaseServer {
10 static heartbeatInterval = 1000;
11
12 /**
13 * @param {import("../Server")} server
14 */
15 constructor(server) {
16 super(server);
17
18 /** @type {import("ws").ServerOptions} */
19 const options = {
20 .../** @type {WebSocketServerConfiguration} */
21 (this.server.options.webSocketServer).options,
22 clientTracking: false,
23 };
24 const isNoServerMode =
25 typeof options.port === "undefined" &&
26 typeof options.server === "undefined";
27
28 if (isNoServerMode) {
29 options.noServer = true;
30 }
31
32 this.implementation = new WebSocket.Server(options);
33
34 /** @type {import("http").Server} */
35 (this.server.server).on(
36 "upgrade",
37 /**
38 * @param {import("http").IncomingMessage} req
39 * @param {import("stream").Duplex} sock
40 * @param {Buffer} head
41 */
42 (req, sock, head) => {
43 if (!this.implementation.shouldHandle(req)) {
44 return;
45 }
46
47 this.implementation.handleUpgrade(req, sock, head, (connection) => {
48 this.implementation.emit("connection", connection, req);
49 });
50 }
51 );
52
53 this.implementation.on(
54 "error",
55 /**
56 * @param {Error} err
57 */
58 (err) => {
59 this.server.logger.error(err.message);
60 }
61 );
62
63 const interval = setInterval(() => {
64 this.clients.forEach(
65 /**
66 * @param {ClientConnection} client
67 */
68 (client) => {
69 if (client.isAlive === false) {
70 client.terminate();
71
72 return;
73 }
74
75 client.isAlive = false;
76 client.ping(() => {});
77 }
78 );
79 }, WebsocketServer.heartbeatInterval);
80
81 this.implementation.on(
82 "connection",
83 /**
84 * @param {ClientConnection} client
85 */
86 (client) => {
87 this.clients.push(client);
88
89 client.isAlive = true;
90
91 client.on("pong", () => {
92 client.isAlive = true;
93 });
94
95 client.on("close", () => {
96 this.clients.splice(this.clients.indexOf(client), 1);
97 });
98 }
99 );
100
101 this.implementation.on("close", () => {
102 clearInterval(interval);
103 });
104 }
105};
Note: See TracBrowser for help on using the repository browser.