Search Unity

[Solved] How to use OnSerializeNetworkView() to send message from client to server?

Discussion in 'Scripting' started by asperatology, May 29, 2015.

  1. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    I have almost all the basic Server/Client networking model set up. The last bit I need to do is to make the client send out an acknowledgement, which is key to keeping the Server/Client network design in sync with each other.

    This is currently my code for OnSerializeNetworkView():

    Code (CSharp):
    1. public void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info) {
    2.     Debug.Log("On serialize network view.");
    3.     if (stream.isReading) {
    4.         bool serverReadyFlag = false, clientReadyFlag = false;
    5.         stream.Serialize(ref serverReadyFlag);
    6.         stream.Serialize(ref clientReadyFlag);
    7.  
    8.         //Check the undetermined flags according to the network state.
    9.         if (Network.isClient) {
    10.             this.serverIsReady = serverReadyFlag;
    11.         }
    12.         else if (Network.isServer) {
    13.             this.clientIsReady = clientReadyFlag;
    14.         }
    15.  
    16.         if (serverReadyFlag && clientReadyFlag) {
    17.             this.everythingIsReady = true;
    18.         }
    19.     }
    20.     else if (stream.isWriting) {
    21.         stream.Serialize(ref this.serverIsReady);
    22.         stream.Serialize(ref this.clientIsReady);
    23.     }
    24.     Debug.Log("Boolean values - [Server][Client][Everything]: " + this.serverIsReady.ToString() + " " + this.clientIsReady.ToString() + " " + this.everythingIsReady.ToString());
    25. }
    The problem with this code setup is the server keeps writing bitstreams to the client. And the client is missing a way to send to the server a bitstream data.

    • Does anyone know how to get the client to send an acknowledgement back to the server via OnSerializeNetworkView()?
    • If that's now how to do it, what other ways can I do to make the client send an acknowledgement back to the server?
    Thanks in advance.
     
  2. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    I am going to answer my own question. I went ahead and did the second option of using an alternate method to make the client send an acknowledgement to the server. There is no point in using the OnSerializeNetworkView() to do acknowledgements, as the whole operation is one-sided. The client only reads from the bitstream, and the server only sends the bitstream. There's no other way around.

    Moving along, here's the code:

    Code (CSharp):
    1.     public void OnPlayerConnected(NetworkPlayer player) {
    2.         Debug.LogWarning("On player connected.");
    3.         this.clientIsReady = true;
    4.         if (this.serverIsReady) {
    5.             this.everythingIsReady = true;
    6.         }
    7.     }
    8.  
    9.     public void OnConnectedToServer() {
    10.         Debug.LogWarning("On connected to server.");
    11.         this.clientIsReady = true;
    12.         if (this.serverIsReady) {
    13.             this.everythingIsReady = true;
    14.         }
    15.     }
    When the client has connected to the server, it triggers both the OnPlayerConnected() and OnConnectedToServer() on the server side. I don't know the exact difference between the two, but that is how it works.

    Below is the full code. Make good use of it.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GetReady : MonoBehaviour {
    5.     private bool serverIsReady;
    6.     private bool clientIsReady;
    7.     private bool everythingIsReady;
    8.  
    9.     public void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info) {
    10.         if (stream.isReading) {
    11.             bool serverReadyFlag = false, clientReadyFlag = false;
    12.             stream.Serialize(ref serverReadyFlag);
    13.             stream.Serialize(ref clientReadyFlag);
    14.  
    15.             if (Network.isClient) {
    16.                 this.serverIsReady = serverReadyFlag;
    17.             }
    18.             else if (Network.isServer) {
    19.                 this.clientIsReady = clientReadyFlag;
    20.             }
    21.  
    22.             if (serverReadyFlag && clientReadyFlag) {
    23.                 this.everythingIsReady = true;
    24.             }
    25.         }
    26.         else if (stream.isWriting) {
    27.             stream.Serialize(ref this.serverIsReady);
    28.             stream.Serialize(ref this.clientIsReady);
    29.         }
    30.     }
    31.  
    32.     public void OnPlayerConnected(NetworkPlayer player) {
    33.         Debug.LogWarning("On player connected.");
    34.         this.clientIsReady = true;
    35.         if (this.serverIsReady) {
    36.             this.everythingIsReady = true;
    37.         }
    38.     }
    39.  
    40.     public void OnConnectedToServer() {
    41.         Debug.LogWarning("On connected to server.");
    42.         this.clientIsReady = true;
    43.         if (this.serverIsReady) {
    44.             this.everythingIsReady = true;
    45.         }
    46.     }
    47.  
    48.     public void OnDisconnectedFromMasterServer(NetworkDisconnection info) {
    49.         Debug.LogWarning("On disconnected from master server.");
    50.     }
    51.  
    52.     public void OnDisconnectedFromServer(NetworkDisconnection info) {
    53.         if (Network.isServer) {
    54.             Debug.LogWarning("On disconnecting from server as server.");
    55.             this.serverIsReady = false;
    56.             this.everythingIsReady = false;
    57.         }
    58.         else if (Network.isClient) {
    59.             Debug.LogWarning("On disconnecting from server as client.");
    60.             this.clientIsReady = false;
    61.             this.everythingIsReady = false;
    62.         }
    63.     }
    64.  
    65.     public void OnFailedToConnect(NetworkConnectionError error) {
    66.         Debug.LogWarning("On failed to connect.");
    67.     }
    68.  
    69.     public void OnFailedToConnectToMasterServer(NetworkConnectionError error) {
    70.         Debug.LogWarning("On failed to connect to master server.");
    71.     }
    72.  
    73.     public void OnMasterServerEvent(MasterServerEvent msEvent) {
    74.         Debug.LogWarning("On master server event. Event: " + msEvent.ToString());
    75.     }
    76.  
    77.     public void OnPlayerDisconnected(NetworkPlayer player) {
    78.         Debug.LogWarning("On player disconnected.");
    79.         this.everythingIsReady = false;
    80.         this.clientIsReady = false;
    81.     }
    82.  
    83.     public void OnServerInitialized() {
    84.         Debug.LogWarning("On server initialized.");
    85.         this.serverIsReady = true;
    86.     }
    87.  
    88.     public void OnGUI() {
    89.         if (this.everythingIsReady) {
    90.             GUI.contentColor = Color.black;
    91.             GUI.Label(new Rect(150f, 0f, 400f, 30f), "This is now connected and working.");
    92.         }
    93.     }
    94.  
    95.     public void Start() {
    96.         this.serverIsReady = false;
    97.         this.clientIsReady = false;
    98.         this.everythingIsReady = false;
    99.     }
    100.  
    101.     public bool IsReady() {
    102.         return this.everythingIsReady;
    103.     }
    104.  
    105.     [RPC]
    106.     public void RPC_Ready() {
    107.         if (Network.isClient) {
    108.             this.clientIsReady = true;
    109.         }
    110.         else if (Network.isServer) {
    111.             this.serverIsReady = true;
    112.         }
    113.     }
    114. }
    115.