-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
146 lines (113 loc) · 4.48 KB
/
app.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
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
const express = require('express');
const app = express();
var path = require('path');
const http = require('http').Server(app);
const io = require('socket.io')(http);
const port = process.env.PORT || 3000;
let users = [];
let usersid = [];
let result;
var tempvalue;
var fpsroom;
var index;
var indexsec;
app.get(('/'), (req, res) => {
res.sendFile(path.join(__dirname + '/index.html'));
});
app.get(('/login'), (req, res) => {
res.sendFile(path.join(__dirname + '/index.html'));
});
app.get('/room', (req, res) => {
res.sendFile(path.join(__dirname + '/room.html'));
});
app.get('/nasil-kullanilir', (req, res) => {
res.sendFile(path.join(__dirname + '/nasil-kullanilir.html'));
});
io.on('connection', (socket) => {
socket.on('new user',(user)=>{
var check = users.findIndex((element) => element.username == user.username);
result = users.filter(word => word.roomid == user.roomid);
if( check == -1 ){
socket.join(user.roomid);
if(result.length==0){
user.mod="moderator";
io.to(socket.id).emit('moderator check',"moderator");
}
else{
user.mod="izleyici";
io.to(socket.id).emit('moderator check',"izleyici");
}
users.push(user);
usersid.push(user.id);
console.log(user.username+" "+user.roomid+" odasına katıldı.");
io.to(user.roomid).emit("message new user", users.length);
io.to(user.roomid).emit('room users list', result.length);
}
else{
if(result.length==1){
user.mod="moderator";
io.to(socket.id).emit('moderator check',"moderator");
}
else{
user.mod="izleyici";
io.to(socket.id).emit('moderator check',"izleyici");
}
socket.join(user.roomid);
console.log(check+" index kullanıcısı ile aynı username'e sahipsin o yüzden üst üste giriş yapamazsın, son kaldığın noktaya yönlendiriyorum. Eğer çıkmak istiyorsan çıkış yap.");
var oldid = users.findIndex((word) => word.id == user.storageid);
io.to(user.id).emit("existing user","existing user");
users[oldid].storageid = users[oldid].id;
users[oldid].id = user.id;
usersid[oldid] = user.id;
}
io.to(user.roomid).emit("message new user", users);
io.to(user.roomid).emit('room users list', result.length);
index = users.findIndex((element) => element.roomid == user.roomid);
console.table(users);
console.table(usersid);
io.to(user.roomid).emit("message new user", users);
io.to(user.roomid).emit('room users list', result);
//fpsroom = users.findIndex((word) => word.roomid == user.roomid);
//console.log(users[fpsroom]);
});
console.table(users);
socket.on('leave room',(data)=>{
console.log("ÇIKMADAN ÖNCE");
result = users.filter(word => word.roomid == data.roomid);
console.log(result);
socket.leave(data.roomid);
console.log("Bu odadan ayrıldı");
index = users.findIndex((element) => element.username == data.username);
users.splice(index,1);
usersid.splice(index,1);
indexsec = users.findIndex((element) => element.roomid == data.roomid);
result = users.filter(word => word.roomid == data.roomId);
console.log("ÇIKTIKTAN SONRA");
console.log(result);
});
socket.on('event message', (data) => {
index = users.findIndex((element) => element.roomid == data.roomid);
if(socket.id == usersid[index]){
socket.broadcast.to(data.roomid).emit('event message',data);
}
});
socket.on('videoid',(YTvideodata) => {
index = users.findIndex((element) => element.roomid == data.roomid);
if(socket.id == usersid[index]){
socket.broadcast.to(YTvideodata.roomid).emit('videoid',YTvideodata);
io.to(socket.id).emit('moderator',"moderator");
}
});
/*
socket.on('twiframe video url',(twdata) => {
index = users.findIndex((element) => element.roomid == twdata.roomid);
if(socket.id == usersid[index]){
io.to(twdata.roomid).emit('twiframe video url',twdata);
io.to(socket.id).emit('moderator',"moderator");
}
});
*/
});
http.listen(port, () => {
console.log(`Socket.IO server running at http://localhost:${port}/`);
});