コード例 #1
0
        /// <summary>
        /// These functions are running on a different thread, not in the main thread,
        /// so if any error happens, i wont get anything on console unless used with try/catch Debug.Log function
        /// </summary>
        private void OnMsgReceivedByClient(string _msg)
        {
            mIsGameStarted = true;
            byte[] receivedBytes = NetworkUtil.GetBytes(_msg);
            Debug.Log(_msg);
            Debug.Log("received by mono " + Encoding.ASCII.GetString(receivedBytes));

            try {
                MemoryStream    memStream = new MemoryStream();
                BinaryFormatter binForm   = new BinaryFormatter();
                memStream.Write(receivedBytes, 0, receivedBytes.Length);
                memStream.Seek(0, SeekOrigin.Begin);
                MethodPacket obj = (MethodPacket)binForm.Deserialize(memStream);
                mMethodStack.Enqueue(obj);
                Debug.Log("Method successfully enqued...");
            } catch (Exception _exc) {
                Debug.LogError("ERROR in processing method packet: " + _exc.Message);
            }

            //====Right below, it was the initial prototype without using C# reflection, it was becoming difficult to maintain with packets,
            //====so genuinly felt the need to use a more robust way to implement rpc calls

            // IMessagePacket mp = JsonUtility.FromJson<IMessagePacket>(_msg);
            // Debug.Log("Client received :: " + mp);
            // switch ((MessageType)mp.msgType)
            // {
            //     case MessageType.START_GAME:
            //         isGameStarted = true;
            //         packetStack.Enqueue(mp);
            //         break;
            //     case MessageType.SIMPLE_MSG:
            //         SimpleMessagePacket smp = JsonUtility.FromJson<SimpleMessagePacket>(_msg);
            //         packetStack.Enqueue(smp);
            //         break;
            //     default:
            //         break;
            // }
        }