@@ -139,6 +139,17 @@ internal static void OnDestroyObject(uint networkId)
139139 singleton . releasedNetworkObjectIds . Push ( networkId ) ;
140140 }
141141
142+ internal void InvokeMessageHandlers ( string messageType , byte [ ] data , int clientId )
143+ {
144+ if ( ! messageTypes . ContainsKey ( messageType ) || ! messageCallbacks . ContainsKey ( messageTypes [ messageType ] ) )
145+ return ;
146+
147+ foreach ( KeyValuePair < int , Action < int , byte [ ] > > pair in messageCallbacks [ messageTypes [ messageType ] ] )
148+ {
149+ pair . Value ( clientId , data ) ;
150+ }
151+ }
152+
142153 internal int AddIncomingMessageHandler ( string name , Action < int , byte [ ] > action )
143154 {
144155 if ( messageTypes . ContainsKey ( name ) )
@@ -566,6 +577,17 @@ private void HandleIncomingData(int connectonId, byte[] data)
566577
567578 internal void Send ( int clientId , string messageType , string channelName , byte [ ] data )
568579 {
580+ if ( isHost && clientId == - 1 )
581+ {
582+ //Host trying to send data to it's own client
583+ InvokeMessageHandlers ( messageType , data , clientId ) ;
584+ return ;
585+ }
586+ else if ( clientId == - 1 )
587+ {
588+ //Client trying to send data to host
589+ clientId = serverClientId ;
590+ }
569591 using ( MemoryStream stream = new MemoryStream ( ) )
570592 {
571593 using ( BinaryWriter writer = new BinaryWriter ( stream ) )
@@ -597,7 +619,18 @@ internal void Send(int[] clientIds, string messageType, string channelName, byte
597619 int channel = channels [ channelName ] ;
598620 for ( int i = 0 ; i < clientIds . Length ; i ++ )
599621 {
600- NetworkTransport . Send ( hostId , clientIds [ i ] , channel , dataToSend , size , out error ) ;
622+ int clientId = clientIds [ i ] ;
623+ if ( isHost && clientId == - 1 )
624+ {
625+ InvokeMessageHandlers ( messageType , data , clientId ) ;
626+ continue ;
627+ }
628+ else if ( clientId == - 1 )
629+ {
630+ //Client trying to send data to host
631+ clientId = serverClientId ;
632+ }
633+ NetworkTransport . Send ( hostId , clientId , channel , dataToSend , size , out error ) ;
601634 }
602635 }
603636 }
@@ -618,7 +651,18 @@ internal void Send(List<int> clientIds, string messageType, string channelName,
618651 int channel = channels [ channelName ] ;
619652 for ( int i = 0 ; i < clientIds . Count ; i ++ )
620653 {
621- NetworkTransport . Send ( hostId , clientIds [ i ] , channel , dataToSend , size , out error ) ;
654+ int clientId = clientIds [ i ] ;
655+ if ( isHost && clientId == - 1 )
656+ {
657+ InvokeMessageHandlers ( messageType , data , clientId ) ;
658+ continue ;
659+ }
660+ else if ( clientId == - 1 )
661+ {
662+ //Client trying to send data to host
663+ clientId = serverClientId ;
664+ }
665+ NetworkTransport . Send ( hostId , clientId , channel , dataToSend , size , out error ) ;
622666 }
623667 }
624668 }
@@ -639,7 +683,19 @@ internal void Send(string messageType, string channelName, byte[] data)
639683 int channel = channels [ channelName ] ;
640684 foreach ( KeyValuePair < int , NetworkedClient > pair in connectedClients )
641685 {
642- NetworkTransport . Send ( hostId , pair . Key , channel , dataToSend , size , out error ) ;
686+ int clientId = pair . Key ;
687+ if ( isHost && pair . Key == - 1 )
688+ {
689+ InvokeMessageHandlers ( messageType , data , pair . Key ) ;
690+ continue ;
691+ }
692+ else if ( clientId == - 1 )
693+ {
694+ //Client trying to send data to host
695+ clientId = serverClientId ;
696+ }
697+ NetworkTransport . Send ( hostId , clientId , channel , dataToSend , size , out error ) ;
698+
643699 }
644700 }
645701 }
@@ -662,7 +718,18 @@ internal void Send(string messageType, string channelName, byte[] data, int clie
662718 {
663719 if ( pair . Key == clientIdToIgnore )
664720 continue ;
665- NetworkTransport . Send ( hostId , pair . Key , channel , dataToSend , size , out error ) ;
721+ int clientId = pair . Key ;
722+ if ( isHost && pair . Key == - 1 )
723+ {
724+ InvokeMessageHandlers ( messageType , data , clientId ) ;
725+ continue ;
726+ }
727+ else if ( clientId == - 1 )
728+ {
729+ //Client trying to send data to host
730+ clientId = serverClientId ;
731+ }
732+ NetworkTransport . Send ( hostId , clientId , channel , dataToSend , size , out error ) ;
666733 }
667734 }
668735 }
0 commit comments