Passed
Push — master ( 1d44d5...cbfa7c )
by Eric
01:10
created

index.js (3 issues)

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
The parameter protocol is not used and could be removed.

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.

Loading history...
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
The parameter data is not used and could be removed.

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.

Loading history...
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
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
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