Skip to content

Commit 0caf4e5

Browse files
committed
Corrections & socket IO binding refactor
1 parent 7c6bd4e commit 0caf4e5

File tree

4 files changed

+131
-120
lines changed

4 files changed

+131
-120
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ The following is the list of events you can send to the server. For example `soc
6565
6666
> `message(msg)` sends a message to the current room.
6767
68-
> `toggle(resource)` toggles a resource. The resources are 'screen', 'video' and 'audio'.
68+
> `toggle(resource)` toggles a resource. The resources are 'video', 'microphone' and 'audio'.
6969
7070
> `candidate(callId, candidate)` sends a candidate.
7171

server.php

Lines changed: 6 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
require_once(__dir__ . "/vendor/autoload.php");
88

9+
ini_set("log_errors", 1);
10+
ini_set("error_log", LOG_PATH);
11+
912
use Workerman\Worker;
1013
use PHPSocketIO\SocketIO;
1114

@@ -26,119 +29,8 @@
2629
$handler->registerExceptionHandler();
2730
$handler->registerFatalHandler();
2831

29-
$io = new SocketIO(PORT, array(
30-
'ssl' => array(
31-
'local_cert' => CERT_CA,
32-
'local_pk' => CERT_KEY,
33-
'verify_peer' => false,
34-
'allow_self_signed' => true,
35-
'verify_peer_name' => false
36-
)
37-
));
38-
39-
40-
$controller = new Controller($io, $logger);
41-
42-
$io->on('connection', function ($socket) use ($controller) {
43-
44-
$controller->connect($socket);
45-
46-
$socket->on("error", function ($exception) use ($socket, $controller) {
47-
$client = $controller->getClient($socket);
48-
if($client !== false){
49-
$controller->handleException($client, $exception);
50-
}
51-
});
52-
53-
$socket->on("disconnect", function ($reason) use ($socket, $controller) {
54-
$client = $controller->getClient($socket);
55-
if($client !== false){
56-
if($reason == $client->getId()){
57-
$reason = "leaving";
58-
}
59-
$controller->disconnect($client, $reason);
60-
}
61-
});
62-
63-
$socket->on("message", function($message) use ($socket, $controller) {
64-
$client = $controller->getClient($socket);
65-
if($client !== false){
66-
$controller->message($client, $message);
67-
}
68-
});
69-
70-
$socket->on("toggle", function($resource) use ($socket, $controller) {
71-
$client = $controller->getClient($socket);
72-
if($client !== false){
73-
$controller->toggleResource($client, $resource);
74-
}
75-
});
76-
77-
$socket->on("create", function($name, $password) use ($socket, $controller) {
78-
$client = $controller->getClient($socket);
79-
if($client !== false){
80-
$controller->createRoom($client, $name, $password);
81-
}
82-
});
83-
84-
$socket->on("join", function($roomId, $password) use ($socket, $controller) {
85-
$client = $controller->getClient($socket);
86-
if($client !== false){
87-
$controller->joinRoom($client, $roomId, $password);
88-
}
89-
});
90-
91-
$socket->on("leave", function() use ($socket, $controller) {
92-
$client = $controller->getClient($socket);
93-
if($client !== false){
94-
$controller->leaveRoom($client);
95-
}
96-
});
97-
98-
$socket->on("kick", function($userId) use ($socket, $controller) {
99-
$client = $controller->getClient($socket);
100-
if($client !== false){
101-
$controller->kickFromRoom($client, $userId);
102-
}
103-
});
104-
105-
$socket->on("ban", function($userId) use ($socket, $controller) {
106-
$client = $controller->getClient($socket);
107-
if($client !== false){
108-
$controller->banFromRoom($client, $userId);
109-
}
110-
});
111-
112-
$socket->on("unban", function($userId) use ($socket, $controller) {
113-
$client = $controller->getClient($socket);
114-
if($client !== false){
115-
$controller->unbanFromRoom($client, $userId);
116-
}
117-
});
118-
119-
/**
120-
* Calls
121-
*/
122-
$socket->on("candidate", function($callId, $candidate) use ($socket, $controller) {
123-
$client = $controller->getClient($socket);
124-
if($client !== false){
125-
$controller->candidate($client, $callId, $candidate);
126-
}
127-
});
128-
129-
$socket->on("offer", function($callId, $offer) use ($socket, $controller) {
130-
$client = $controller->getClient($socket);
131-
if($client !== false){
132-
$controller->offer($client, $callId, $offer);
133-
}
134-
});
135-
$socket->on("answer", function($callId, $answer) use ($socket, $controller) {
136-
$client = $controller->getClient($socket);
137-
if($client !== false){
138-
$controller->answer($client, $callId, $answer);
139-
}
140-
});
32+
$controller = new Controller($logger);
33+
$controller->bind();
14134

142-
});
35+
Worker::runAll();
14336

144-
Worker::runAll();

src/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public function __construct($id, $socket){
1717
$this->room = false;
1818

1919
$this->resources = [
20-
"screen" => false,
2120
"video" => false,
21+
"microphone" => false,
2222
"audio" => false
2323
];
2424

src/Controller.php

Lines changed: 123 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Sowe\PHPPeerServer;
44

5+
use PHPSocketIO\SocketIO;
56
use Sowe\PHPPeerServer\Mapping;
6-
77
use Sowe\PHPPeerServer\Exceptions\RoomIsFullException;
88
use Sowe\PHPPeerServer\Exceptions\RoomWrongPasswordException;
99
use Sowe\PHPPeerServer\Exceptions\ClientIsAlreadyInException;
@@ -27,8 +27,7 @@ public static function getInstance(){
2727
return self::$instance;
2828
}
2929

30-
public function __construct($io, $logger){
31-
$this->io = $io;
30+
public function __construct($logger){
3231
$this->logger = $logger;
3332
$this->clients = new Mapping();
3433
$this->rooms = new Mapping();
@@ -89,7 +88,7 @@ public function message($client, $message){
8988

9089
public function toggleResource($client, $resource){
9190
if($client->toggleResource($resource)){
92-
$room->getSocket($this->io)->emit("r_resource", $client->getPublicInfo(), $client->getResources());
91+
$client->getRoom()->getSocket($this->io)->emit("r_resource", $client->getPublicInfo(), $client->getResources());
9392
}
9493
}
9594

@@ -282,4 +281,124 @@ public function unbanFromRoom($client, $userId){
282281
}
283282
}
284283

284+
/**
285+
* Binding
286+
*/
287+
public function bind(){
288+
$this->io = new SocketIO(PORT, array(
289+
'ssl' => array(
290+
'local_cert' => CERT_CA,
291+
'local_pk' => CERT_KEY,
292+
'verify_peer' => false,
293+
'allow_self_signed' => true,
294+
'verify_peer_name' => false
295+
)
296+
));
297+
$this->io->on('workerStart', function ($socket){
298+
$this->logger->info("Server starting...");
299+
});
300+
$this->io->on('workerStop', function ($socket){
301+
$this->logger->info("Server stopping...");
302+
});
303+
$this->io->on('connection', function ($socket) {
304+
$this->connect($socket);
305+
306+
$socket->on("error", function ($exception) use ($socket) {
307+
$client = $this->getClient($socket);
308+
if($client !== false){
309+
$this->handleException($client, $exception);
310+
}
311+
});
312+
313+
$socket->on("disconnect", function ($reason) use ($socket) {
314+
$client = $this->getClient($socket);
315+
if($client !== false){
316+
if($reason == $client->getId()){
317+
$reason = "leaving";
318+
}
319+
$this->disconnect($client, $reason);
320+
}
321+
});
322+
323+
$socket->on("message", function($message) use ($socket) {
324+
$client = $this->getClient($socket);
325+
if($client !== false){
326+
$this->message($client, $message);
327+
}
328+
});
329+
330+
$socket->on("toggle", function($resource) use ($socket) {
331+
$client = $this->getClient($socket);
332+
if($client !== false){
333+
$this->toggleResource($client, $resource);
334+
}
335+
});
336+
337+
$socket->on("create", function($name, $password) use ($socket) {
338+
$client = $this->getClient($socket);
339+
if($client !== false){
340+
$this->createRoom($client, $name, $password);
341+
}
342+
});
343+
344+
$socket->on("join", function($roomId, $password) use ($socket) {
345+
$client = $this->getClient($socket);
346+
if($client !== false){
347+
$this->joinRoom($client, $roomId, $password);
348+
}
349+
});
350+
351+
$socket->on("leave", function() use ($socket) {
352+
$client = $this->getClient($socket);
353+
if($client !== false){
354+
$this->leaveRoom($client);
355+
}
356+
});
357+
358+
$socket->on("kick", function($userId) use ($socket) {
359+
$client = $this->getClient($socket);
360+
if($client !== false){
361+
$this->kickFromRoom($client, $userId);
362+
}
363+
});
364+
365+
$socket->on("ban", function($userId) use ($socket) {
366+
$client = $this->getClient($socket);
367+
if($client !== false){
368+
$this->banFromRoom($client, $userId);
369+
}
370+
});
371+
372+
$socket->on("unban", function($userId) use ($socket) {
373+
$client = $this->getClient($socket);
374+
if($client !== false){
375+
$this->unbanFromRoom($client, $userId);
376+
}
377+
});
378+
379+
/**
380+
* Calls
381+
*/
382+
$socket->on("candidate", function($callId, $candidate) use ($socket) {
383+
$client = $this->getClient($socket);
384+
if($client !== false){
385+
$this->candidate($client, $callId, $candidate);
386+
}
387+
});
388+
389+
$socket->on("offer", function($callId, $offer) use ($socket) {
390+
$client = $this->getClient($socket);
391+
if($client !== false){
392+
$this->offer($client, $callId, $offer);
393+
}
394+
});
395+
396+
$socket->on("answer", function($callId, $answer) use ($socket) {
397+
$client = $this->getClient($socket);
398+
if($client !== false){
399+
$this->answer($client, $callId, $answer);
400+
}
401+
});
402+
});
403+
}
285404
}

0 commit comments

Comments
 (0)