-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (63 loc) · 2.79 KB
/
index.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const http = require("http");
const express = require("express");
const socketio = require("socket.io");
const path = require("path");
const app = express();
const httpserver = http.Server(app);
const io = socketio(httpserver);
const gamedirectory = path.join(__dirname, "html");
app.use(express.static(gamedirectory));
httpserver.listen(3000);
var rooms = [];
var usernames = [];
var online = [];
var pfps = [];
io.on('connection', function(socket) {
socket.on("join", function(room, username, pfp) {
if(online.includes(username) && (usernames[socket.id] != username || typeof(usernames[socket.id]) === "undefined" || usernames[socket.id] === null)){
socket.emit("dialog", "Username already taken.");
}
else if(usernames[socket.id] === username && rooms[socket.id] === room){
socket.emit("dialog", "You are already connected.");
}
else {
socket.leaveAll();
socket.join(room);
socket.emit("join", room);
if(typeof(usernames[socket.id]) === "undefined" || usernames[socket.id] === null){
online.push(username);
io.in(room).emit("recieve", "connect", username + " has entered the chat.", "Server", "favicon.png");
}
else if(username != usernames[socket.id]) {
online.splice(online.indexOf(usernames[socket.id]), 1);
online.push(username);
socket.broadcast.to(rooms[socket.id]).emit("recieve", "r", usernames[socket.id] + " has changed ther username to " + username, "Server", "favicon.png");
socket.emit("recieve", "s", usernames[socket.id] + " has changed ther username to " + username, "Server", "favicon.png");
}
if(room != rooms[socket.id] && (typeof(rooms[socket.id]) != 'undefined' || rooms[socket.id] != null)){
io.in(rooms[socket.id]).emit("recieve", "disconnect", usernames[socket.id] + " has moved to " + room, "Server", "favicon.png");
io.in(room).emit("recieve", "connect", username + " has entered the chat.", "Server", "favicon.png");
}
rooms[socket.id] = room;
usernames[socket.id] = username;
if(pfp === ""){pfp = "images/blank.png"}
pfps[socket.id] = pfp;
io.emit("usernames", online);
}
console.log(online);
})
socket.on("send", function(message) {
io.in(rooms[socket.id]).emit("recieve", "normal", message, usernames[socket.id], pfps[socket.id]);
})
socket.on("disconnect", ()=>{
if(usernames[socket.id] != null || typeof(usernames[socket.id]) != "undefined"){
io.in(rooms[socket.id]).emit("recieve", "disconnect", usernames[socket.id] + " has left the chat.", "Server", "favicon.png");
online.splice(online.indexOf(usernames[socket.id]), 1);
delete rooms[socket.id];
delete usernames[socket.id];
delete pfps[socket.id];
io.emit("usernames", online);
}
console.log(online);
})
})