@@ -45,6 +45,10 @@ public class NetworkingManager : MonoBehaviour
45
45
/// </summary>
46
46
public static NetworkingManager singleton { get ; private set ; }
47
47
/// <summary>
48
+ /// Gets the networkId of the server
49
+ /// </summary>
50
+ public uint ServerNetId => NetworkConfig . NetworkTransport . ServerNetId ;
51
+ /// <summary>
48
52
/// The clientId the server calls the local client by, only valid for clients
49
53
/// </summary>
50
54
public uint LocalClientId
@@ -123,6 +127,62 @@ internal set
123
127
/// The current NetworkingConfiguration
124
128
/// </summary>
125
129
public NetworkConfig NetworkConfig ;
130
+ /// <summary>
131
+ /// Delegate used for incomming custom messages
132
+ /// </summary>
133
+ /// <param name="clientId">The clientId that sent the message</param>
134
+ /// <param name="reader">The reader containing the message data</param>
135
+ public delegate void CustomMessageDelegete ( uint clientId , BitReader reader ) ;
136
+ /// <summary>
137
+ /// Event invoked when custom messages arrive
138
+ /// </summary>
139
+ public event CustomMessageDelegete OnIncommingCustomMessage ;
140
+
141
+ internal void InvokeOnIncommingCustomMessage ( uint clientId , BitReader reader )
142
+ {
143
+ if ( OnIncommingCustomMessage != null ) OnIncommingCustomMessage ( clientId , reader ) ;
144
+ }
145
+
146
+ /// <summary>
147
+ /// Sends custom message to a list of clients
148
+ /// </summary>
149
+ /// <param name="clientIds">The clients to send to, sends to everyone if null</param>
150
+ /// <param name="writer">The message writer containing the data</param>
151
+ /// <param name="channel">The channel to send the data on</param>
152
+ public void SendCustomMessage ( List < uint > clientIds , BitWriter writer , string channel = "MLAPI_DEFAULT_MESSAGE" )
153
+ {
154
+ if ( ! isServer )
155
+ {
156
+ if ( LogHelper . CurrentLogLevel <= LogLevel . Error ) LogHelper . LogWarning ( "Can not send custom message to multiple users as a client" ) ;
157
+ return ;
158
+ }
159
+ if ( clientIds == null )
160
+ {
161
+ for ( int i = 0 ; i < ConnectedClientsList . Count ; i ++ )
162
+ {
163
+ InternalMessageHandler . Send ( ConnectedClientsList [ i ] . ClientId , "MLAPI_CUSTOM_MESSAGE" , channel , writer ) ;
164
+ }
165
+ }
166
+ else
167
+ {
168
+ for ( int i = 0 ; i < clientIds . Count ; i ++ )
169
+ {
170
+ InternalMessageHandler . Send ( clientIds [ i ] , "MLAPI_CUSTOM_MESSAGE" , channel , writer ) ;
171
+ }
172
+ }
173
+ }
174
+
175
+ /// <summary>
176
+ /// Sends a custom message to a specific client
177
+ /// </summary>
178
+ /// <param name="clientId">The client to send the message to</param>
179
+ /// <param name="writer">The message writer containing the data</param>
180
+ /// <param name="channel">The channel tos end the data on</param>
181
+ public void SendCustomMessage ( uint clientId , BitWriter writer , string channel = "MLAPI_DEFAULT_MESSAGE" )
182
+ {
183
+ InternalMessageHandler . Send ( clientId , "MLAPI_CUSTOM_MESSAGE" , channel , writer ) ;
184
+ }
185
+
126
186
127
187
#if ! DISABLE_CRYPTOGRAPHY
128
188
internal EllipticDiffieHellman clientDiffieHellman ;
@@ -363,6 +423,7 @@ private object Init(bool server)
363
423
MessageManager . messageTypes . Add ( "MLAPI_NETWORKED_VAR_UPDATE" , MLAPIConstants . MLAPI_NETWORKED_VAR_UPDATE ) ;
364
424
MessageManager . messageTypes . Add ( "MLAPI_SERVER_RPC" , MLAPIConstants . MLAPI_SERVER_RPC ) ;
365
425
MessageManager . messageTypes . Add ( "MLAPI_CLIENT_RPC" , MLAPIConstants . MLAPI_CLIENT_RPC ) ;
426
+ MessageManager . messageTypes . Add ( "MLAPI_CUSTOM_MESSAGE" , MLAPIConstants . MLAPI_CUSTOM_MESSAGE ) ;
366
427
367
428
return settings ;
368
429
}
@@ -755,7 +816,6 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId, uint
755
816
{
756
817
#region INTERNAL MESSAGE
757
818
758
- //MLAPI message
759
819
switch ( messageType )
760
820
{
761
821
case MLAPIConstants . MLAPI_CONNECTION_REQUEST :
@@ -803,6 +863,9 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId, uint
803
863
case MLAPIConstants . MLAPI_CLIENT_RPC :
804
864
if ( isClient ) InternalMessageHandler . HandleClientRPC ( clientId , reader , channelId ) ;
805
865
break ;
866
+ case MLAPIConstants . MLAPI_CUSTOM_MESSAGE :
867
+ InternalMessageHandler . HandleCustomMessage ( clientId , reader , channelId ) ;
868
+ break ;
806
869
}
807
870
808
871
#endregion
0 commit comments