Example #1
0
        public void OnDataReceived(object sender, DataReceivedEvent e)
        {
            Debug.Log("Got a message from a network client ");
            Debug.Log("what message ? " + e);
            Debug.Log("what message ? " + e.Data);

            var netMsg = PrimeNetMessage.Deserialize(e.Data);

            Debug.Log("message desrialized Body of message is {" + netMsg.MessageBody + "}");

            if (netMsg.NetMessage == EPrimeNetMessage.ClientDisconnected || netMsg.NetMessage == EPrimeNetMessage.ServerDisconnected)
            {
                Debug.Log("Disconnected");
                var id     = int.Parse(netMsg.MessageBody);
                var client = _clientList.Find(i => i.ClientNumber == id);
                client.DataReceived -= OnDataReceived;

                _clientList.Remove(client);

                if (netMsg.NetMessage == EPrimeNetMessage.ServerDisconnected)
                {
                    StartSocketClient(); // go back into  connecting to server
                }
            }

            PublishNetworkMessage(new NetworkMessageEvent(netMsg));
        }
        // Wrap event invocations inside a protected virtual method
        // to allow derived classes to override the event invocation behavior
        protected virtual void PublishDataReceived(DataReceivedEvent e)
        {
            // Make a temporary copy of the event to avoid possibility of
            // a race condition if the last subscriber unsubscribes
            // immediately after the null check and before the event is raised.

            _lastMessage = e;

            DataReceived?.Invoke(this, e);
        }