Skip to content

Commit b756a22

Browse files
committed
Connection handshake
1 parent e84f03c commit b756a22

File tree

6 files changed

+67
-31
lines changed

6 files changed

+67
-31
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
vendor
22
config.php
3-
composer.lock
3+
composer.lock
4+
logs

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ PHP Socket.io server for WebRTC signaling
55

66
The following is the list of events you can send to the server. For example `socket.emit('join', 'a68ca609389b6ba7f0766b9ed1bfd8ca')`
77

8-
> `create()` creates a room.
8+
> `create(offer)` creates a room
99
10-
> `join(roomId)` joins a room.
10+
> `get(roomId)` asks for the room offer
11+
12+
> `join(roomId, answer)` joins a room with an answer.
1113
1214
> `leave()` leaves the current room.
1315
@@ -27,6 +29,8 @@ The following are the events the server will trigger.
2729

2830
> `created(roomId)` confirms you created a room.
2931
32+
> `gotten(roomId, offer)` answers the get requests with the offer.
33+
3034
> `joined(roomId)` confirms you joined the room.
3135
3236
> `left(roomId)` confirms you left the room.

server.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,24 @@
6464
}
6565
});
6666

67-
$socket->on("create", function() use ($socket, $controller) {
67+
$socket->on("create", function($offer) use ($socket, $controller) {
6868
$client = $controller->getClient($socket);
6969
if($client !== false){
70-
$controller->createRoom($client);
70+
$controller->createRoom($client, $offer);
7171
}
7272
});
7373

74-
$socket->on("join", function($roomId) use ($socket, $controller) {
74+
$socket->on("get", function($roomId) use ($socket, $controller) {
7575
$client = $controller->getClient($socket);
7676
if($client !== false){
77-
$controller->joinRoom($client, $roomId);
77+
$controller->getRoom($client, $roomId);
78+
}
79+
});
80+
81+
$socket->on("join", function($roomId, $answer) use ($socket, $controller) {
82+
$client = $controller->getClient($socket);
83+
if($client !== false){
84+
$controller->joinRoom($client, $roomId, $answer);
7885
}
7986
});
8087

src/Client.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ public function getRoom(){
5151

5252
public function setRoom($room){
5353
if($this->room !== false){
54-
$client->getSocket()->leave($this->room->getId());
54+
$this->socket->leave($this->room->getId());
5555
}
5656
if($room !== false){
57-
$client->getSocket()->join($room->getId());
57+
$this->socket->join($room->getId());
5858
}
5959
$this->room = $room;
6060
}

src/Controller.php

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function __construct($io, $logger){
2424
}
2525

2626
public function getClient($socket){
27-
$client = $socket->client;
27+
$client = $socket->ppsClient;
2828
if($this->clients->hasKey($client->getId())){
2929
return $client;
3030
}
@@ -33,16 +33,16 @@ public function getClient($socket){
3333

3434
public function connect($socket){
3535
$client = new Client($socket->id, $socket);
36-
$socket->client = $client;
36+
$socket->ppsClient = $client;
3737
$this->clients->add($client);
3838

39-
$this->logger->info(__FUNCTION__.":".__LINE__ .":". $socket->conn->remoteAddress .": connected with ID: "+ $socket->id);
39+
$this->logger->info(__FUNCTION__.":".__LINE__ .":". $socket->conn->remoteAddress .": connected with ID: ". $socket->id . " (ONLINE: " . sizeof($this->clients) . ")");
4040
}
4141

4242
public function disconnect($client){
4343
$this->clients->remove($client);
4444

45-
$this->logger->info(__FUNCTION__.":".__LINE__ .":" + $client->getId() + ": disconnected");
45+
$this->logger->info(__FUNCTION__.":".__LINE__ .":" . $client->getId() . ": disconnected (ONLINE: " . sizeof($this->clients) . ")");
4646
}
4747

4848
public function message($client, $message){
@@ -57,7 +57,7 @@ public function message($client, $message){
5757
"message" => $message
5858
]);
5959

60-
$this->logger->info(__FUNCTION__.":".__LINE__ .":" + $client->getId() + " message in " + $room->getId() + ": "+ $message);
60+
$this->logger->info(__FUNCTION__.":".__LINE__ .":" . $client->getId() . " message in " . $room->getId() . ": " . $message);
6161
}
6262
}
6363

@@ -74,18 +74,28 @@ public function toggleResource($client, $resource){
7474
/**
7575
* Room management
7676
*/
77-
public function createRoom($client){
77+
public function createRoom($client, $offer){
7878
do{
7979
$roomId = bin2hex(random_bytes(ROOM_HASH_LENGTH));
8080
}while($this->rooms->hasKey($roomId));
8181

82-
$this->rooms->add(new Room($id, $client));
82+
$room = new Room($roomId, $client);
83+
$room->setData("offer", $offer);
84+
$this->rooms->add($room);
8385
$client->getSocket()->emit("created", $roomId);
8486

85-
$this->logger->info(__FUNCTION__.":".__LINE__ .":" + $client->getId() + " created the room " + $roomId);
87+
$this->logger->info(__FUNCTION__.":".__LINE__ .":" . $client->getId() . " created the room " . $roomId);
8688
}
8789

88-
public function joinRoom($client, $roomId){
90+
public function getRoom($client, $roomId){
91+
$room = $this->rooms->get($roomId);
92+
if($room !== false){
93+
// Getting
94+
$client->getSocket()->emit("gotten", $room->getData("offer"));
95+
}
96+
}
97+
98+
public function joinRoom($client, $roomId, $answer){
8999
$room = $this->rooms->get($roomId);
90100

91101
$this->leaveRoom($client);
@@ -94,20 +104,21 @@ public function joinRoom($client, $roomId){
94104
try{
95105
$room->join($client);
96106
// Joined
107+
$room->setData("answer", $answer);
97108
$client->getSocket()->emit("joined", $room->getId());
98109
$room->getSocket($this->io)->emit("r_joined", $client->getId());
99110

100-
$this->logger->info(__FUNCTION__.":".__LINE__ .":" + $client->getId() + " joined " + $roomId);
111+
$this->logger->info(__FUNCTION__.":".__LINE__ .":" . $client->getId() . " joined " . $roomId);
101112
}catch(RoomIsFullException $e){
102113
// Room is full
103114
$client->getSocket()->emit("join_full");
104115

105-
$this->logger->info(__FUNCTION__.":".__LINE__ .":" + $client->getId() + " join failed (full) " + $roomId);
116+
$this->logger->info(__FUNCTION__.":".__LINE__ .":" . $client->getId() . " join failed (full) " . $roomId);
106117
}catch(ClientIsBannedException $e){
107118
// Client is banned
108119
$client->getSocket()->emit("join_banned");
109120

110-
$this->logger->info(__FUNCTION__.":".__LINE__ .":" + $client->getId() + " join failed (banned) " + $roomId);
121+
$this->logger->info(__FUNCTION__.":".__LINE__ .":" . $client->getId() . " join failed (banned) " . $roomId);
111122
}
112123
}
113124

@@ -120,7 +131,7 @@ public function leaveRoom($client){
120131
$client->getSocket()->emit("left", $room->getId());
121132
$room->getSocket($this->io)->emit("r_left", $client->getId());
122133

123-
$this->logger->info(__FUNCTION__.":".__LINE__ .":" + $client->getId() + " left " + $room->getId());
134+
$this->logger->info(__FUNCTION__.":".__LINE__ .":" . $client->getId() . " left " . $room->getId());
124135
}
125136
}
126137

@@ -134,17 +145,17 @@ public function kickFromRoom($client, $userId){
134145
$clientToKick->getSocket()->emit("kicked", $room->getId());
135146
$room->getSocket($this->io)->emit("r_kicked", $clientToKick->getId());
136147

137-
$this->logger->info(__FUNCTION__.":".__LINE__ .":" + $client->getId() + " kicked " + $clientToKick->getId() + " from " + $room->getId());
148+
$this->logger->info(__FUNCTION__.":".__LINE__ .":" . $client->getId() . " kicked " . $clientToKick->getId() . " from " . $room->getId());
138149
}catch(ClientIsNotOwnerException $e){
139150
// Client is not the owner
140151
$client->getSocket()->emit("kick_noprivileges");
141152

142-
$this->logger->info(__FUNCTION__.":".__LINE__ .":" + $client->getId() + " failed kicking (privileges) " + $clientToKick->getId() + " from " + $room->getId());
153+
$this->logger->info(__FUNCTION__.":".__LINE__ .":" . $client->getId() . " failed kicking (privileges) " . $clientToKick->getId() . " from " . $room->getId());
143154
}catch(ClientIsNotInTheRoomException $e){
144155
// ClientToKick is no longuer in the room
145156
$client->getSocket()->emit("kick_notin");
146157

147-
$this->logger->info(__FUNCTION__.":".__LINE__ .":" + $client->getId() + " failed kicking (not in) " + $clientToKick->getId() + " from " + $room->getId());
158+
$this->logger->info(__FUNCTION__.":".__LINE__ .":" . $client->getId() . " failed kicking (not in) " . $clientToKick->getId() . " from " . $room->getId());
148159
}
149160
}
150161
}
@@ -159,17 +170,17 @@ public function banFromRoom($client, $userId){
159170
$clientToBan->getSocket()->emit("banned", $room->getId());
160171
$room->getSocket($this->io)->emit("r_banned", $clientToBan->getId());
161172

162-
$this->logger->info(__FUNCTION__.":".__LINE__ .":" + $client->getId() + " banned " + $clientToBan->getId() + " from " + $room->getId());
173+
$this->logger->info(__FUNCTION__.":".__LINE__ .":" . $client->getId() . " banned " . $clientToBan->getId() . " from " . $room->getId());
163174
}catch(ClientIsNotOwnerException $e){
164175
// Client is not the owner
165176
$client->getSocket()->emit("ban_noprivileges");
166177

167-
$this->logger->info(__FUNCTION__.":".__LINE__ .":" + $client->getId() + " failed banning (privileges) " + $clientToBan->getId() + " from " + $room->getId());
178+
$this->logger->info(__FUNCTION__.":".__LINE__ .":" . $client->getId() . " failed banning (privileges) " . $clientToBan->getId() . " from " . $room->getId());
168179
}catch(ClientIsBannedException $e){
169180
// ClientToBan is already banned
170181
$client->getSocket()->emit("ban_already");
171182

172-
$this->logger->info(__FUNCTION__.":".__LINE__ .":" + $client->getId() + " failed banning (already) " + $clientToBan->getId() + " from " + $room->getId());
183+
$this->logger->info(__FUNCTION__.":".__LINE__ .":" . $client->getId() . " failed banning (already) " . $clientToBan->getId() . " from " . $room->getId());
173184
}
174185
}
175186
}
@@ -184,15 +195,15 @@ public function unbanFromRoom($client, $userId){
184195
$clientToUnban->getSocket()->emit("unbanned", $room->getId());
185196
$room->getSocket($this->io)->emit("r_unbanned", $clientToUnban->getId());
186197

187-
$this->logger->info(__FUNCTION__.":".__LINE__ .":" + $client->getId() + " unbanned " + $clientToUnban->getId() + " from " + $room->getId());
198+
$this->logger->info(__FUNCTION__.":".__LINE__ .":" . $client->getId() . " unbanned " . $clientToUnban->getId() . " from " . $room->getId());
188199
}catch(ClientIsNotOwnerException $e){
189200
// Client is not the owner
190201
$client->getSocket()->emit("unban_noprivileges");
191-
$this->logger->info(__FUNCTION__.":".__LINE__ .":" + $client->getId() + " failed unbanning (privileges) " + $clientToUnban->getId() + " from " + $room->getId());
202+
$this->logger->info(__FUNCTION__.":".__LINE__ .":" . $client->getId() . " failed unbanning (privileges) " . $clientToUnban->getId() . " from " . $room->getId());
192203
}catch(ClientIsNotBannedException $e){
193204
// ClientToUnban is not banned
194205
$client->getSocket()->emit("unban_notbanned");
195-
$this->logger->info(__FUNCTION__.":".__LINE__ .":" + $client->getId() + " failed unbanning (not banned) " + $clientToUnban->getId() + " from " + $room->getId());
206+
$this->logger->info(__FUNCTION__.":".__LINE__ .":" . $client->getId() . " failed unbanning (not banned) " . $clientToUnban->getId() . " from " . $room->getId());
196207
}
197208
}
198209
}

src/Room.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,29 @@ class Room{
1313
protected $clients;
1414
protected $owner;
1515
protected $banned;
16+
protected $data;
1617

1718
public function __construct($id, $owner){
1819
$this->id = $id;
1920
$this->clients = new Mapping();
2021
$this->banned = new Mapping();
2122
$this->owner = $owner;
23+
$this->data = [];
2224

2325
$this->join($owner);
2426
}
2527

28+
public function getData($type = null){
29+
if($type !== null && isset($this->data[$type])){
30+
return $this->data[$type];
31+
}
32+
return $this->data;
33+
}
34+
35+
public function setData($type, $data){
36+
$this->data[$type] = $data;
37+
}
38+
2639
public function getId(){
2740
return $this->id;
2841
}

0 commit comments

Comments
 (0)