This repository has been archived by the owner on Jul 15, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
chat.js
49 lines (42 loc) · 1.7 KB
/
chat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Basic chat app with Site.js (server).
//
// For a step-by-step tutorial of how to build this example, see:
// https://ar.al/2019/10/10/build-a-simple-chat-app-with-site.js
//
// Copyright ⓒ 2019 Aral Balkan.
// Released under GNU AGPL version 3.0 or later.
//
////////////////////////////////////////////////////////////////////////////////////////////////////
module.exports = function (client, request) {
// New client connection: persist client’s “room”
// based on request path.
client.room = this.setRoom(request)
// Log the connection.
console.log(`New client connected to ${client.room}`)
client.on('message', message => {
// New message received: broadcast it to all other clients
// in the same room after performing basic validation.
if (!isValidMessage(JSON.parse(message))) {
console.log(`Message is invalid; not broadcasting.`)
return
}
const numberOfRecipients = this.broadcast(client, message)
// Log the number of recipients message was sent to
// and make sure we pluralise the log message properly.
console.log(`${client.room} message broadcast to `
+ `${numberOfRecipients} recipient`
+ `${numberOfRecipients === 1 ? '' : 's'}`)
})
}
// Is the passed object a valid string?
function isValidString(s) {
return Boolean(s) // Isn’t null, undefined, '', or 0
&& typeof s === 'string' // and is the correct type
&& s.replace(/\s/g, '') !== '' // and is not just whitespace.
}
// Is the passed message object valid?
function isValidMessage(m) {
return isValidString(m.nickname) && isValidString(m.text)
}