1
|
|
|
/** |
2
|
|
|
* Client manager module for managing connections and broadcasting messages. |
3
|
|
|
* @module clientManager |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
import express from "express"; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Manages client and bike connections. |
10
|
|
|
*/ |
11
|
|
|
const clientManager = { |
12
|
|
|
/** @type {Array<express.Response>} */ |
13
|
|
|
clients: [], |
14
|
|
|
|
15
|
|
|
/** @type {Array<express.Response>} */ |
16
|
|
|
bikes: [], |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Adds a new client to the clients array. |
20
|
|
|
* @param {express.Response} client - The client to add. |
21
|
|
|
*/ |
22
|
|
|
addClient(client) { |
23
|
|
|
this.clients.push(client); |
24
|
|
|
}, |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Removes a client from the clients array. |
28
|
|
|
* @param {express.Response} client - The client to remove. |
29
|
|
|
*/ |
30
|
|
|
removeClient(client) { |
31
|
|
|
this.clients = this.clients.filter(c => c !== client); |
32
|
|
|
}, |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Adds a new bike to the bikes array. |
36
|
|
|
* @param {express.Response} bike - The bike connection to add. |
37
|
|
|
*/ |
38
|
|
|
addBike(bike) { |
39
|
|
|
this.bikes.push(bike); |
40
|
|
|
}, |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Removes a bike from the bikes array. |
44
|
|
|
* @param {express.Response} bike - The bike to remove. |
45
|
|
|
*/ |
46
|
|
|
removeBike(bike) { |
47
|
|
|
this.bikes = this.bikes.filter(b => b !== bike); |
48
|
|
|
}, |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Retrieves the current list of bikes. |
52
|
|
|
* @returns {Array<express.Response>} The current list of bikes. |
53
|
|
|
*/ |
54
|
|
|
getBikes() { |
55
|
|
|
return this.bikes; |
56
|
|
|
}, |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Retrieves the current list of clients. |
60
|
|
|
* @returns {Array<express.Response>} The current list of clients. |
61
|
|
|
*/ |
62
|
|
|
getClients() { |
63
|
|
|
return this.clients; |
64
|
|
|
}, |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Broadcasts a message to all clients. |
68
|
|
|
* @param {Object} message - The message to broadcast. |
69
|
|
|
*/ |
70
|
|
|
broadcastToClients(message) { |
71
|
|
|
this.clients.forEach(client => client.write(`data: ${JSON.stringify(message)}\n\n`)); |
72
|
|
|
}, |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Broadcasts a message to all bikes. |
76
|
|
|
* @param {Object} message - The message to broadcast. |
77
|
|
|
*/ |
78
|
|
|
broadcastToBikes(message) { |
79
|
|
|
this.bikes.forEach(bike => bike.write(`data: ${JSON.stringify(message)}\n\n`)); |
80
|
|
|
} |
81
|
|
|
}; |
82
|
|
|
|
83
|
|
|
export default clientManager; |
84
|
|
|
|