-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
111 lines (95 loc) · 3.12 KB
/
index.js
File metadata and controls
111 lines (95 loc) · 3.12 KB
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
const express = require("express");
const app = express();
const socket = require("socket.io");
let namespaces = require("./data/Namespaces");
const Namespace = require("./classes/Namespace");
app.use(express.static("public"));
const expressServer = app.listen(8000, () => {
console.log("server is listening on 8000");
});
const io = socket(expressServer, {
cors: {
origin: "*",
methods: ["GET", "POST"],
Credential: true,
},
});
io.on("connection", (socket) => {
const nsData = namespaces.map((ns) => {
return {
img: ns.img,
endpoint: ns.endpoint,
};
});
socket.emit("nslist", nsData);
});
namespaces.forEach((namespace) => {
let currentRoomName;
io.of(namespace.endpoint).on("connection", (nsSocket, req) => {
console.log(`${nsSocket.id}: is Connected to ${namespace.endpoint}`);
nsSocket.emit("roomData", namespace.rooms);
nsSocket.on("joinedRoom", (roomName) => {
nsSocket.join(roomName);
currentRoomName = roomName;
io.of(namespace.endpoint)
.in(roomName)
.fetchSockets()
.then((clients) => clients.map((client) => client.id))
.then((clientsId) => {
io.of(namespace.endpoint)
.in(roomName)
.emit("ClientsIdArray", clientsId.length);
});
let nsRoom = namespace.rooms.find((room) => {
return room.roomTitle === currentRoomName;
});
nsSocket.emit("historyMessage", nsRoom.history);
nsSocket.on("disconnect", () => {
console.log(`Client disconnected: ${nsSocket.id}`);
io.of(namespace.endpoint)
.in(roomName)
.fetchSockets()
.then((clients) => clients.map((client) => client.id))
.then((clientsId) => {
io.of("/wiki")
.in(roomName)
.emit("ClientsIdArray", clientsId.length);
});
});
//send back number of user to all sockets connected to this room
});
nsSocket.on("newMessageToserver", (msg) => {
console.log("MESSAGE", msg);
const convertedDate = new Date().toLocaleString();
const fullmsg = {
fullmsg: msg,
date: convertedDate,
userName: "onkar",
avatar:
"https://dl.memuplay.com/new_market/img/com.vicman.newprofilepic.icon.2022-06-07-21-33-07.png",
};
let nsRoom = namespace.rooms.find((room) => {
return room.roomTitle === currentRoomName;
});
console.log(nsRoom, "nsroom");
nsRoom.addMessage(fullmsg);
io.of(namespace.endpoint)
.to(currentRoomName)
.emit("messageToClients", fullmsg);
});
});
});
// io.on("connection", (socket, req) => {
// socket.emit("messageFromServer", { data: "Hello, it's from the server" });
// console.log("a Client is Connected");
// socket.on("chatmessage", (data) => {
// console.log(socket.id);
// io.emit("chatmessageFromServer", { data });
// });
// socket.on("disconnect", () => {
// console.log("A client disconnected");
// });
// });
// io.of("/privateRoom").on("connection", (socket2) => {
// socket2.emit("Prvatemessage", { data: "hello my friend from socket2" });
// });