Example #1
0
    void Update()
    {
        // client
        if (client.Connected)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                client.Send(new byte[] { 0x1 });
            }

            // show all new messages
            Telepathy.Message msg;
            while (client.GetNextMessage(out msg))
            {
                switch (msg.eventType)
                {
                case Telepathy.EventType.Connected:
                    Debug.Log("Connected");
                    break;

                case Telepathy.EventType.Data:
                    Debug.Log("Data: " + BitConverter.ToString(msg.data));
                    break;

                case Telepathy.EventType.Disconnected:
                    Debug.Log("Disconnected");
                    break;
                }
            }
        }

        // server
        if (server.Active)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                server.Send(0, new byte[] { 0x2 });
            }

            // show all new messages
            Telepathy.Message msg;
            while (server.GetNextMessage(out msg))
            {
                switch (msg.eventType)
                {
                case Telepathy.EventType.Connected:
                    Debug.Log(msg.connectionId + " Connected");
                    break;

                case Telepathy.EventType.Data:
                    Debug.Log(msg.connectionId + " Data: " + BitConverter.ToString(msg.data));
                    break;

                case Telepathy.EventType.Disconnected:
                    Debug.Log(msg.connectionId + " Disconnected");
                    break;
                }
            }
        }
    }
Example #2
0
    private void Update()
    {
        // server
        if (server.Active)
        {
            // show all new messages
            Telepathy.Message msg;
            while (server.GetNextMessage(out msg))
            {
                switch (msg.eventType)
                {
                case Telepathy.EventType.Connected:
                    ClientConnected(msg);
                    break;

                case Telepathy.EventType.Data:
                    ProcessData(msg);
                    break;

                case Telepathy.EventType.Disconnected:
                    ClientDisconnected(msg);
                    break;
                }
            }
        }
    }
Example #3
0
    static void Main(string[] args)
    {
        server = new Telepathy.Server();
        server.Start(1337);

        Task.Run(() =>
        {
            while (server.Active)
            {
                Telepathy.Message msg;
                while (server.GetNextMessage(out msg))
                {
                    switch (msg.eventType)
                    {
                    case Telepathy.EventType.Connected:
                        // msg.connectionIdはクライアント接続時に+1される数値。1始まり
                        Console.WriteLine("Telepathy.EventType.Connected : msg.connectionId=" + msg.connectionId);
                        break;

                    case Telepathy.EventType.Data:
                        Console.WriteLine("Telepathy.EventType.Data : msg.connectionId=" + msg.connectionId + ", msg.data" + System.Text.Encoding.UTF8.GetString(msg.data));

                        // reply
                        Console.WriteLine("send massage from server...");
                        server.Send(msg.connectionId, System.Text.Encoding.UTF8.GetBytes("send message from server..."));
                        break;

                    case Telepathy.EventType.Disconnected:
                        Console.WriteLine("Telepathy.EventType.Disconnected : msg.connectionId=" + msg.connectionId);
                        break;
                    }
                }
            }
        });

        bool break_flag = false;

        while (break_flag == false)
        {
            ConsoleKey k = Console.ReadKey().Key;
            switch (k)
            {
            case ConsoleKey.L:
                break;

            case ConsoleKey.Escape:
                break_flag = true;
                break;
            }
        }

        server.Stop();
    }
Example #4
0
    private void Update()
    {
        if (server == null)
        {
            return;
        }

        Telepathy.Message message;

        while (server.GetNextMessage(out message))
        {
            if (message.eventType == Telepathy.EventType.Data)
            {
                server.Send(message.connectionId, message.data);
            }
        }
    }
Example #5
0
        void FixedUpdate()
        {
            Telepathy.Message msg;
            while (server.GetNextMessage(out msg))
            {
                switch (msg.eventType)
                {
                case Telepathy.EventType.Connected:
                    OnClientConnected(new KNetConnection((short)msg.connectionId));
                    break;

                case Telepathy.EventType.Data:
                    OnBytesRecieved(msg.data);
                    break;

                case Telepathy.EventType.Disconnected:
                    //OnClientDisconnected();
                    break;
                }
            }
        }
        public void Run()
        {
            try
            {
                networkPlayersDictionary = new Dictionary <long, NetworkPlayer>();

                server = new Telepathy.Server();
                server.Start(1337);

                while (server.Active)
                {
                    // reply to each incoming message
                    Message msg;
                    while (server.GetNextMessage(out msg))
                    {
                        if (msg.eventType == EventType.Connected)
                        {
                            OnClientConnected(msg.connectionId);
                        }
                        else if (msg.eventType == EventType.Data)
                        {
                            server.Send(msg.connectionId, msg.data);
                            OnMessageReceived(new NetworkMessage(msg.data), msg.connectionId);
                        }
                        else if (msg.eventType == EventType.Disconnected)
                        {
                            OnClientDisconnected(msg.connectionId);
                        }
                    }

                    SendPlayerPositions();

                    System.Threading.Thread.Sleep(15);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }
Example #7
0
    public void Update()
    {
        if (Input.GetKeyDown(KeyCode.PageUp))
        {
            changeModel(currentServerIndex - 1);
        }
        else if (Input.GetKeyDown(KeyCode.PageDown))
        {
            changeModel(currentServerIndex + 1);
        }

        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hitPoint;
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hitPoint, Mathf.Infinity))
            {
                currentPosition.visible = true;
                Vector3 pos = currentTransform.InverseTransformPoint(hitPoint.point);
                currentPosition.x = pos.x; currentPosition.y = pos.y; currentPosition.z = pos.z;
                hudPoint.Show();
            }
            else
            {
                currentPosition.visible = false;
                hudPoint.Hide();
            }
            changePosition(currentPosition.x, currentPosition.y, currentPosition.z, currentPosition.visible);
        }

        if (hudPoint.enable)
        {
            hudPoint.SetPoint(currentTransform.TransformPoint(new Vector3(currentPosition.x, currentPosition.y, currentPosition.z)));
        }

        if (server == null)
        {
            return;
        }
        if (!server.Active)
        {
            return;
        }
        Telepathy.Message msg;
        while (server.GetNextMessage(out msg))
        {
            switch (msg.eventType)
            {
            case Telepathy.EventType.Connected:
            {
                Debug.Log(msg.connectionId + " Connected");
                connections.Add(msg.connectionId);
                OnClientConnected(msg.connectionId);
            }
            break;

            case Telepathy.EventType.Disconnected:
            {
                Debug.Log(msg.connectionId + " Disconnected");
                connections.Remove(msg.connectionId);
            }
            break;
            }
        }
    }
Example #8
0
    void Update()
    {
        // client
        if (client.Connected)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                client.Send(new byte[] { 0x1 });

                //client.Send(Telepathy.Utils.StringToBytes(GameObject.Find("PlayerObject(Clone)").name)); // sends the players name as a byte[]
                //byte[] bytesvalue = Telepathy.Utils.StringToBytes(GameObject.Find("PlayerObject").name);
            }
            if (Input.GetKeyDown(KeyCode.LeftAlt))
            {
                GameObject.Instantiate(SquarePrefab, Vector3.zero, Quaternion.identity);
            }
            // show all new messages
            Telepathy.Message msg;
            while (client.GetNextMessage(out msg))
            {
                switch (msg.eventType)
                {
                case Telepathy.EventType.Connected:
                    Debug.Log("Connected");
                    break;

                case Telepathy.EventType.Data:
                    Debug.Log("Data: " + BitConverter.ToString(msg.data));
                    break;

                case Telepathy.EventType.Disconnected:
                    Debug.Log("Disconnected");
                    break;
                }
            }
        }

        // server
        if (server.Active)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                server.Send(0, new byte[] { 0x2 });
            }

            // show all new messages
            Telepathy.Message msg;
            while (server.GetNextMessage(out msg))
            {
                switch (msg.eventType)
                {
                case Telepathy.EventType.Connected:
                    Debug.Log(msg.connectionId + " Connected");
                    break;

                case Telepathy.EventType.Data:
                    Debug.Log(msg.connectionId + " Data: " + BitConverter.ToString(msg.data));
                    break;

                case Telepathy.EventType.Disconnected:
                    Debug.Log(msg.connectionId + " Disconnected");
                    break;
                }
            }
        }
    }