@JSer/

Simple Live Chat

Nodejs

No description

fork
loading
Files
  • index.js
  • client
  • chats.txt
  • package-lock.json

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.3411058982810429","path":"index.js","file":{"path":"index.js","content":{"asEncoding":{"base64":"dmFyIGV4cHJlc3MgPSByZXF1aXJlKCdleHByZXNzJyk7CnZhciBhcHAgPSBleHByZXNzKCk7CnZhciBzZXJ2ID0gcmVxdWlyZSgnaHR0cCcpLlNlcnZlcihhcHApOwoKYXBwLnVzZSgnLycsIGV4cHJlc3Muc3RhdGljKF9fZGlybmFtZSArICcvY2xpZW50LycpKTsKCnNlcnYubGlzdGVuKDIwMDApOwpjb25zb2xlLmxvZygnU2VydmVyIHN0YXJ0ZWQuJyk7Cgp2YXIgY2hhdHMgPSBbXTsKdmFyIHNvY2tldExpc3QgPSB7fTsKdmFyIGd1ZXN0cyA9IDA7CnZhciBpbyA9IHJlcXVpcmUoJ3NvY2tldC5pbycpKHNlcnYsIHt9KTsKdmFyIGZzID0gcmVxdWlyZSgnZnMnKTsKdmFyIGQgPSBuZXcgRGF0ZSgpOwoKaW8uc29ja2V0cy5vbignY29ubmVjdGlvbicsIHNvY2tldCA9PiB7CiAgaWYoIWNoYXRzLmxlbmd0aCkKCSAgZ2V0Q2hhdHMoKTsKCWVtaXRDaGF0cygpOwoJc29ja2V0LmNvbG9yID0gZ2V0UmFuZG9tQ29sb3IoKTsKCglzb2NrZXQub24oJ25hbWUnLCBuYW1lID0+IHsKCSAgcmVmcmVzaFRpbWUoKTsKCSAgbmFtZSA9IGVuY29kZShuYW1lKTsKCQlzb2NrZXRMaXN0W3NvY2tldC5pZF0gPSBzb2NrZXQ7CgkJaWYgKG5hbWUgPT0gJ0d1ZXN0JykgewoJCQlzb2NrZXQubmFtZSA9IG5hbWUgKyAnICcgKyBndWVzdHM7CgkJCWd1ZXN0cysrOwoJCX0gZWxzZSBzb2NrZXQubmFtZSA9IG5hbWU7CgkJY2hhdHMucHVz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mVwbGFjZSgvJi9nLCAnJmFtcDsnKS5yZXBsYWNlKC88L2csICcmbHQ7JykucmVwbGFjZSgvPi9nLCAnJmd0OycpOwp9CgpmdW5jdGlvbiBnZXRSYW5kb21Db2xvcigpIHsKCXZhciBsZXR0ZXJzID0gJzAxMjM0NTY3ODlBQkNERUYnOwoJdmFyIGNvbG9yID0gJyMnOwoJZm9yICh2YXIgaSA9IDA7IGkgPCA2OyBpKyspIHsKCQljb2xvciArPSBsZXR0ZXJzW01hdGguZmxvb3IoTWF0aC5yYW5kb20oKSAqIDE0KV07Cgl9CglyZXR1cm4gY29sb3I7Cn0K"},"asBuffer":null},"loaded":true}}
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
var express = require('express');
var app = express();
var serv = require('http').Server(app);

app.use('/', express.static(__dirname + '/client/'));

serv.listen(2000);
console.log('Server started.');

var chats = [];
var socketList = {};
var guests = 0;
var io = require('socket.io')(serv, {});
var fs = require('fs');
var d = new Date();

io.sockets.on('connection', socket => {
  if(!chats.length)
	  getChats();
	emitChats();
	socket.color = getRandomColor();

	socket.on('name', name => {
	  refreshTime();
	  name = encode(name);
		socketList[socket.id] = socket;
		if (name == 'Guest') {
			socket.name = name + ' ' + guests;
			guests++;
		} else socket.name = name;
		chats.push({
		  message:
			'<span class="name" style="color:' +
				socket.color +
				'">' +
				socket.name +
				'</span><span class="green"> joined.</span>',
				time: d.getTime()
		});
		emitChats();
		emitWho();
		saveChats();
	});

	socket.on('message', message => {
	  refreshTime();
	  if(message.length > 100) return;
	  message = encode(message);
		chats.push({
		  message:
			'<span class="name" style="color:' +
				socket.color +
				'">' +
				socket.name +
				'</span>: <span class="message-content">' +
				message + '</span>',
				time: d.getTime(),
				socketId: socket.id,
				id: Math.random()
		});
		emitChats();
		saveChats();
	});
  socket.on('delete', id => {
    for(var i in chats){
      if(chats[i].id == id){
        chats.splice(i, 1);
        break;
      }
    }
    emitChats();
  });
	socket.on('disconnect', () => {
	  refreshTime();
		if (socketList[socket.id]) {
			chats.push({
			  message:
				'<span class="name" style="color:' +
					socket.color +
					'">' +
					socket.name +
					'</span> <span class="red"> left.</span>',
				time: d.getTime()
			});
			emitChats();
			delete socketList[socket.id];
		}
		saveChats();
		emitWho();
	});
});
setInterval(() => {
  refreshTime();
  emitChats();
}, 10000);
function refreshTime(){
  d = new Date();
  io.sockets.emit('time', d.getTime());
}
function emitWho() {
	for (var j in socketList) {
		var pack = [];
		for (var i in socketList) {
			if (j == i) {
				pack.push({
					name: socketList[i].name + ' (You)',
					color: socketList[i].color
				});
			} else {
				pack.push({ name: socketList[i].name, color: socketList[i].color });
			}
		}
		socketList[j].emit('who', pack);
	}
}
function emitChats(){
 for(var i in socketList){
    var pack = chats.map(c => {
      if(c.socketId != i) {
        return {message: c.message, time: c.time};
      }
      else{
        return c;
      }
    });
    socketList[i].emit('chats', pack);
  }
}
function saveChats() {
  var chatsStr = chats.map(e => e.message + "\n" + e.time).join("\n\n");
	fs.writeFile('chats.txt', chatsStr, function(err) {
		if (err) {
			return console.log(err);
		}
	});
}

function getChats() {
	fs.readFile('chats.txt', 'utf8', function(err, data) {
		if (err) {
			return console.log(err);
		}
		if (data.length) 
		  chats = data.split('\n\n').map(e => 
		    ({message: e.split("\n")[0], time: e.split("\n")[1]})
		  );
	});
}
function encode(txt) {
	return txt.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}

function getRandomColor() {
	var letters = '0123456789ABCDEF';
	var color = '#';
	for (var i = 0; i < 6; i++) {
		color += letters[Math.floor(Math.random() * 14)];
	}
	return color;
}
node v10.16.0