summaryrefslogtreecommitdiff
path: root/main.js
blob: a849dd469002b9e3efecdbd51a729418e7c882f6 (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const http = require("http");
const fs = require("fs");
const path = require("path");
const url = require("url");
const websocket = require("ws");

const static_content = __dirname + "/dist/";

// Create an array to store messages in
var log = []

// Initialise a simple http server to serve any static files
http.createServer((req, res) => {

	// normalise path name to stay within a normal directory
	var requestUrl = url.parse(req.url);
	var fsPath = static_content + path.normalize(requestUrl.pathname);
	
	// automatically return the index of a directory, if its been selected
	if (fs.statSync(fsPath).isDirectory()) 
		fsPath += fsPath.endsWith("/") ? "index.html" : "/index.html";
	
	// read and send the file
	fs.readFile(fsPath, (err, data) => {
		if (err) {
			res.writeHead(404);
			res.end(JSON.stringify(err))
		} else {
			res.writeHead(200);
			res.end(data);
		}
	});
}).listen(8080);

// create websocket: client tracking allows us to get a set of clients so that we can send messages to each client once they appear
const wss = new websocket.Server({port : 8081, clientTracking: true});

wss.on("connection", ws => {
	// predefine a user nickname
	var nick = "anon" + wss.clients.size

	ws.on("message", msg => {
		var data = JSON.parse(msg);

		// Ensure that nickname requirements are met before setting client's nick
		if (data.nickname && data.nickname.length < 24 && data.nickname.length > 0) nick = data.nickname;

		switch data.event {
			case "join":
				// send the client the requested number of messages before joining
				if (data.lines) log.slice(0-data.lines).forEach(l => ws.send(l))
				break;
			case "message":
				// ensure that the message length requirements are met correctly
				if (data.content.length < 128 && data.content.length > 0) {
					
					// recreate the message object to be sent
					message = JSON.stringify({
						event: "message",
						content: data.content,
						nickname: nick
					});

					// send the message to the log array first
					log.push(message);

					// propogate the message to all connected clients
					wss.clients.forEach(c => {
						c.send(message);
					})
				}
				break;
		}

	});
})