22
33namespace Sowe \PHPPeerServer ;
44
5+ use PHPSocketIO \SocketIO ;
56use Sowe \PHPPeerServer \Mapping ;
6-
77use Sowe \PHPPeerServer \Exceptions \RoomIsFullException ;
88use Sowe \PHPPeerServer \Exceptions \RoomWrongPasswordException ;
99use 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