Exemplo n.º 1
0
        private void MessageReceiver()
        {
            try
            {
                string ip = "193.219.91.103";
                //string ip = "doesntexist";
                //string ip = "127.0.0.1";
                ushort port = 7099;
                //ushort port = 54000;

                if (client.Connect(ip, port))
                {
                    // Wait for packet
                    while (client.PacketCount() == 0)
                    {
                        Thread.Sleep(10);
                    }

                    // Confirm connection
                    Packet result = client.GetPacket();
                    if (Encoding.ASCII.GetString(result.Data) == "Connected.")
                    {
                        connected = true;
                    }
                    else
                    {
                        connected = false;
                        return;
                    }
                }
                else
                {
                    connected = false;
                    return;
                }

                // Send request to open chat
                client.Send(new Packet
                {
                    PacketId = (uint)PacketType.OPEN_CHAT,
                    Data     = Encoding.ASCII.GetBytes(eventId.ToString())
                });

                // Wait for packet
                while (client.PacketCount() == 0)
                {
                    Thread.Sleep(10);
                }

                // Read confirmation packet
                client.GetPacket();

                // Load previous messages
                while (client.PacketCount() > 0)
                {
                    Packet message   = client.GetPacket();
                    string msgString = Encoding.ASCII.GetString(message.Data);
                    messages.Add(new Message(DataList.FromList(Json.ToList(msgString))));
                    Console.WriteLine(messages.Last().Content);
                }

                // Populate the chat window
                foreach (var msg in messages)
                {
                    AddMessage(msg);
                }

                // Listen for new messages
                while (true)
                {
                    if (!client.Connected())
                    {
                        break;
                    }

                    while (client.PacketCount() > 0)
                    {
                        Packet packetMessage = client.GetPacket();
                        Packet packetEventId = client.GetPacket();

                        Message message = new Message(DataList.FromList(Json.ToList(Encoding.ASCII.GetString(packetMessage.Data))));
                        string  eventId = Encoding.ASCII.GetString(packetEventId.Data);
                        if (this.eventId.ToString() == eventId)
                        {
                            Console.WriteLine(message.Content);
                            AddMessage(message);
                        }
                    }

                    Thread.Sleep(100);
                }
            }
            catch
            {
                return;
            }
        }
Exemplo n.º 2
0
 public bool CheckGateConnected()
 {
     return(null != m_gateClient && m_gateClient.Connected());
 }
Exemplo n.º 3
0
    void Update()
    {
        if (goingRight)
        {
            pos    = clientPlayer.transform.position;
            pos.x += Time.deltaTime * speed;
            clientPlayer.transform.position = pos;

            MovementMessage mv = new MovementMessage();
            mv.type      = MessageType.CLIENT;
            mv.x         = pos.x;
            mv.y         = pos.y;
            mv.frequency = Time.time;

            c.ClientSend(mv);
        }

        if (goingLeft)
        {
            pos    = clientPlayer.transform.position;
            pos.x -= Time.deltaTime * speed;
            clientPlayer.transform.position = pos;

            MovementMessage mv = new MovementMessage();
            mv.type      = MessageType.CLIENT;
            mv.x         = pos.x;
            mv.y         = pos.y;
            mv.frequency = Time.time;

            c.ClientSend(mv);
        }

        // Messages
        switch (c.received.type)
        {
        case MessageType.NONE:
            break;

        case MessageType.SERVER:
        {
            MovementMessage mm = (MovementMessage)c.received;
            serverPlayer.transform.position = new Vector3(mm.x, mm.y);
        }
        break;

        case MessageType.BALL:
        {
            MovementMessage mm = (MovementMessage)c.received;
            startBallPos       = ball.transform.position;
            endBallPos         = new Vector3(mm.x, mm.y);
            frequency          = mm.frequency;
            timeStartedLerping = Time.time;
        }
        break;

        case MessageType.RESET:
        {
            Debug.LogError("RESET BALL POS");
        }
        break;

        default:
            Debug.Log("Unhandled message type " + c.received.type);
            break;
        }
        c.received.OnRead(); // Always set a message to read on recieved

        // State
        switch (state)
        {
        case GameState.STOP:
            if (c.Connected())
            {
                state = GameState.START;
            }
            break;

        case GameState.START:
            //Start
            c.ClientSend(new StartMessage());

            Debug.LogError("CLIENT SENT START");

            state = GameState.GAME;
            break;

        case GameState.GAME:

            break;
        }


        // Lerp
        float lerpPercentage = (Time.time - timeStartedLerping) / frequency;

        //Debug.Log(string.Format("lerpPercent[{0}] = (time[{1}] - tS[{2}]) / tTRG[{3}]", lerpPercentage, Time.time, timeStartedLerping, frequency));
        ball.transform.position = Vector3.Lerp(startBallPos, endBallPos, lerpPercentage);
    }