@amasad/

90s chat

Nodejs

No description

fork
loading
Files
  • index.js
  • public
  • package-lock.json

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.09404240776110129","path":"index.js","file":{"path":"index.js","content":{"asEncoding":{"base64":"Ly8gU2V0dXAgYmFzaWMgZXhwcmVzcyBzZXJ2ZXIKdmFyIGV4cHJlc3MgPSByZXF1aXJlKCdleHByZXNzJyk7CnZhciBhcHAgPSBleHByZXNzKCk7CnZhciBwYXRoID0gcmVxdWlyZSgncGF0aCcpOwp2YXIgc2VydmVyID0gcmVxdWlyZSgnaHR0cCcpLmNyZWF0ZVNlcnZlcihhcHApOwp2YXIgaW8gPSByZXF1aXJlKCdzb2NrZXQuaW8nKShzZXJ2ZXIpOwp2YXIgcG9ydCA9IHByb2Nlc3MuZW52LlBPUlQgfHwgMzAwMDsKCnNlcnZlci5saXN0ZW4ocG9ydCwgZnVuY3Rpb24oKSB7Cgljb25zb2xlLmxvZygnU2VydmVyIGxpc3RlbmluZyBhdCBwb3J0ICVkJywgcG9ydCk7Cn0pOwoKY29uc29sZS5sb2coJ2hlcmUnKTsKLy8gUm91dGluZwphcHAudXNlKGV4cHJlc3Muc3RhdGljKHBhdGguam9pbihfX2Rpcm5hbWUsICdwdWJsaWMnKSkpOwoKdmFyIG51bVVzZXJzID0gMDsKCmlvLm9uKCdjb25uZWN0aW9uJywgZnVuY3Rpb24oc29ja2V0KSB7Cgl2YXIgYWRkZWRVc2VyID0gZmFsc2U7CgoJLy8gd2hlbiB0aGUgY2xpZW50IGVtaXRzICduZXcgbWVzc2FnZScsIHRoaXMgbGlzdGVucyBhbmQgZXhlY3V0ZXMKCXNvY2tldC5vbignbmV3IG1lc3NhZ2UnLCBmdW5jdGlvbihkYXRhKSB7CgkJLy8gd2UgdGVsbCB0aGUgY2xpZW50IHRvIGV4ZWN1dGUgJ25ldyBtZXNzYWdlJwoJCXNvY2tldC5icm9hZGNhc3QuZW1pdCgnbmV3IG1lc3NhZ2UnL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5cGluZycsIGZ1bmN0aW9uKCkgewoJCXNvY2tldC5icm9hZGNhc3QuZW1pdCgnc3RvcCB0eXBpbmcnLCB7CgkJCXVzZXJuYW1lOiBzb2NrZXQudXNlcm5hbWUKCQl9KTsKCX0pOwoKCS8vIHdoZW4gdGhlIHVzZXIgZGlzY29ubmVjdHMuLiBwZXJmb3JtIHRoaXMKCXNvY2tldC5vbignZGlzY29ubmVjdCcsIGZ1bmN0aW9uKCkgewoJCWlmIChhZGRlZFVzZXIpIHsKCQkJLS1udW1Vc2VyczsKCgkJCS8vIGVjaG8gZ2xvYmFsbHkgdGhhdCB0aGlzIGNsaWVudCBoYXMgbGVmdAoJCQlzb2NrZXQuYnJvYWRjYXN0LmVtaXQoJ3VzZXIgbGVmdCcsIHsKCQkJCXVzZXJuYW1lOiBzb2NrZXQudXNlcm5hbWUsCgkJCQludW1Vc2VyczogbnVtVXNlcnMKCQkJfSk7CgkJfQoJfSk7Cn0pOwoK"},"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
// Setup basic express server
var express = require('express');
var app = express();
var path = require('path');
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var port = process.env.PORT || 3000;

server.listen(port, function() {
	console.log('Server listening at port %d', port);
});

console.log('here');
// Routing
app.use(express.static(path.join(__dirname, 'public')));

var numUsers = 0;

io.on('connection', function(socket) {
	var addedUser = false;

	// when the client emits 'new message', this listens and executes
	socket.on('new message', function(data) {
		// we tell the client to execute 'new message'
		socket.broadcast.emit('new message', {
			username: socket.username,
			message: data
		});
	});

	// when the client emits 'add user', this listens and executes
	socket.on('add user', function(username) {
		if (addedUser) return;

		// we store the username in the socket session for this client
		socket.username = username;
		++numUsers;
		addedUser = true;
		socket.emit('login', {
			numUsers: numUsers
		});
		// echo globally (all clients) that a person has connected
		socket.broadcast.emit('user joined', {
			username: socket.username,
			numUsers: numUsers
		});
	});

	// when the client emits 'typing', we broadcast it to others
	socket.on('typing', function() {
		socket.broadcast.emit('typing', {
			username: socket.username
		});
	});

	// when the client emits 'stop typing', we broadcast it to others
	socket.on('stop typing', function() {
		socket.broadcast.emit('stop typing', {
			username: socket.username
		});
	});

	// when the user disconnects.. perform this
	socket.on('disconnect', function() {
		if (addedUser) {
			--numUsers;

			// echo globally that this client has left
			socket.broadcast.emit('user left', {
				username: socket.username,
				numUsers: numUsers
			});
		}
	});
});

node v10.16.0