1 | 'use strict'; |
||
2 | |||
3 | const webSocket = require('ws'); |
||
4 | |||
5 | module.exports = function(server) { |
||
6 | const wss = new webSocket.Server({ |
||
7 | server: server, |
||
8 | clientTracking: true, |
||
9 | handleProtocols: (protocol) => {return 'json'} |
||
0 ignored issues
–
show
|
|||
10 | }); |
||
11 | |||
12 | let sendToAll = (ws, data) => { |
||
13 | wss.clients.forEach((client) => { |
||
14 | if (data.type==='connection'){ |
||
15 | client.send(JSON.stringify(data)); |
||
16 | } else { |
||
17 | if(client !== ws && client.readyState === webSocket.OPEN){ |
||
18 | client.send(JSON.stringify(data)); |
||
19 | } |
||
20 | } |
||
21 | }); |
||
22 | } |
||
23 | |||
24 | wss.on('connection', (ws, data) => { |
||
0 ignored issues
–
show
|
|||
25 | let sendData = { |
||
26 | 'message': {author: 'Server', message:'test'}, |
||
27 | 'data': {clients: wss.clients.size}, |
||
28 | 'type': 'connection' |
||
29 | } |
||
30 | sendToAll(ws, sendData) |
||
31 | |||
32 | ws.on('message', (data) => { |
||
33 | |||
34 | let dataFromSend = JSON.parse(data) |
||
35 | let sendData = { |
||
36 | 'message': {author: dataFromSend.message.author, message: dataFromSend.message.message}, |
||
37 | 'data': {clients: wss.clients.size}, |
||
38 | 'type': 'message', |
||
39 | } |
||
40 | sendToAll(ws, sendData); |
||
41 | }); |
||
42 | |||
43 | ws.on('close', (code, reason) => { |
||
44 | console.log(`Closing connection: ${code} ${reason}`); |
||
0 ignored issues
–
show
|
|||
45 | let sendData = { |
||
46 | 'message': {author: 'Server', message:'A user has left the channel'}, |
||
47 | 'data': {clients: wss.clients.size}, |
||
48 | 'type': 'close' |
||
49 | } |
||
50 | sendToAll(ws, sendData) |
||
51 | }); |
||
52 | }); |
||
53 | |||
54 | return wss; |
||
55 | }; |
||
56 |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.