-
Notifications
You must be signed in to change notification settings - Fork 0
/
SocketConnection.js
141 lines (119 loc) · 6 KB
/
SocketConnection.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
'use strict';
module.exports = class SocketConnection{
constructor(io){
io.sockets.on('connection', function(socket) {
console.log("Socket enabled")
var roomCreatorSocketId;
var roomJoinerSocketId;
var roomCreator;
var roomJoin;
// convenience function to log server messages on the client
function log() {
var array = ['Logs From Server:'];
array.push.apply(array, arguments);
socket.emit('log', array);
}
socket.on('webrtc-message', function(message) {
log('Client said: ', message);
socket.to(roomCreator).emit('webrtc-message', message);
});
socket.on('serverBaseDataShare',function(data){
//console.log(data.Data);
//console.log("data size is ",data.Data.byteLength);
// console.log(currentObjSocketId)
socket.to(roomCreator).emit('serverBaseDataReceive',{ArrayData:data.Data});
});
socket.on('FileMetaData', function(data) {
log('Client said filename is : ', data.sendFileName , "And file size is ",data.sendFileSize );
// for a real app, would be room-only (not broadcast)
socket.broadcast.to(roomCreator).emit('FileMetaData', data);
});
socket.on('fileReceived', function(data) {
log('Client said : ', data);
// for a real app, would be room-only (not broadcast)
socket.broadcast.to(roomCreator).emit('fileReceived', data);
});
/////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
socket.on('create', function(data) {
roomCreator = data.email;
roomCreatorSocketId = socket.id;
console.log("DEBUG--- Room Creator Socket ID "+roomCreatorSocketId)
console.log(roomCreator)
log('Received request to create or join room ' + roomCreator);
var clientsInRoom = io.sockets.adapter.rooms[roomCreator];
var numClients = clientsInRoom ? Object.keys(clientsInRoom.sockets).length : 0;
if(numClients >= 1){
console.log("room already created");
socket.emit('roomFull',roomCreator);
}
else{
socket.join(data.email);
socket.emit('roomCreated',roomCreator);
}
log('Client ID ' + socket.id + ' created room ' + roomCreator);
});
socket.on('join',function(data){
roomJoinerSocketId = socket.id;
console.log("DEBUG--- Room Joiner Socket ID "+roomJoinerSocketId)
roomCreator = data.roomCreator;
roomJoin = data.newRoomJoiner;
console.log(data.newRoomJoiner+" asas")
let clientsInRoom = io.sockets.adapter.rooms[roomCreator];
let numClients = clientsInRoom ? Object.keys(clientsInRoom.sockets).length : 0;
console.log("DEBUG--Number of clients in room- "+numClients)
if(numClients > 1){
socket.emit('roomFull',roomCreator);
}
else if(numClients < 1){
socket.emit('peerOffline',roomCreator);
}
else{
socket.emit('connectionRequest-pending');
io.sockets.in(roomCreator).emit('connectionRequest',{fromMail : roomJoin,socketid:roomJoinerSocketId});
}
});
socket.on("connectionAccepted",function(data){
roomJoinerSocketId = data.roomJoinerSocketId
io.sockets.in(data.roomJoinerSocketId).emit('invokeRoomJoiner',{"socketid":roomCreatorSocketId});
});
socket.on('joinConfirm',function(data){
socket.join(roomCreator);
roomCreatorSocketId = data.roomCreatorSocketId;
let clientsInRoom = io.sockets.adapter.rooms[roomCreator];
let numClients = clientsInRoom ? Object.keys(clientsInRoom.sockets).length : 0;
log('Room ' + roomCreator + ' now has ' + numClients + ' client(s)');
io.sockets.in(roomCreator).emit('roomJoined',{creator:roomCreator,joiner:roomJoin});
});
socket.on('disconnectFromRoom',function(data){
if(data.disconnectedUser === roomCreator){
console.log("Debug -- Room Leave from room creator")
io.sockets.in(roomJoinerSocketId).emit('changeStatus');
io.of('/').connected[roomJoinerSocketId].leave(roomCreator,function(){
io.sockets.in(roomCreatorSocketId).emit('userDisconnect',{'disconnectFromRoomCreator':true});
});
} else{
console.log("Debug -- Room Leave from room joiner")
socket.leave(roomCreator,function(){
console.log(roomCreatorSocketId)
io.sockets.in(roomCreatorSocketId).emit('userDisconnect',{'disconnectFromRoomCreator':true})
io.sockets.in(roomJoinerSocketId).emit('userDisconnect',{'disconnectFromRoomCreator':false});
//console.log("Clients In room "+Object.keys(clientsInRoom.sockets))
});
}
});
socket.on('disconnect',function(){
console.log("DEBUG = socket disconnects")
socket.broadcast.to(roomCreator).emit('userDisconnect', 'disconnected');
});
socket.on('end', function (){
socket.leave(roomCreator);
log('disconnect to server');
});
socket.on('bye', function(){
console.log('received bye');
});
});
}
}