Пример #1
0
    public void OnClientDisconnect(int connectionId)
    {
        clients.Remove(connectionId);                                            // remove client from clients list
        PlayersHolder.GetComponent <PlayerHandler>().RemovePlayer(connectionId); // remove it to server hiarchy
        ServerNET_SendFunction.playerDisConnected(connectionId);                 // dont send it to disconnected players

        Debug.Log("player " + connectionId + " disconnected.");
    }
Пример #2
0
    /*---------------------------- Event Handlers ---------------------------------*/

    public void OnClientConnect(int connectionId)
    {
        clients.Add(connectionId);                                                // add client to clients list
        PlayersHolder.GetComponent <PlayerHandler>().InitNewPlayer(connectionId); // add it to server hiarchy
        ServerNET_SendFunction.playerConnected(connectionId);                     // send it to connected players

        Debug.Log("player " + connectionId + " connected.");
    }
    /*------------------------ handle connection ------------------------------*/
    // recieave ping request
    public static void recieavePing(byte[] data)
    {
        int   ClientId       = byteArrayToInt(data, 1);   // int 4 bytes
        float pingSendedTime = byteArrayTofloat(data, 5); // float 4 bytes

        // calculate time taken for ping request to arrive
        float pingLatency = Time.time - pingSendedTime;

        // Debug.Log("recieaved ping request from " + ClientId + ".");

        // response with pong
        ServerNET_SendFunction.sendPong(ClientId, pingLatency);
    }
Пример #4
0
    private IEnumerator updatePlayersPos()
    {
        while (true)
        {
            // Debug.Log("updating players position");

            yield return(new WaitForSeconds(RESET_POS_TIME));

            for (int i = 0; i < players.transform.childCount; i++)
            {
                // get player
                player = players.transform.GetChild(i).GetComponent <Player>();

                // broadcast position of player
                ServerNET_SendFunction.UpdatePositionRotationY(player.ConnectionId, player.Position, player.Rotation.y);
            }
        }
    }
Пример #5
0
    public void init()
    {
        // protocol init
        NetworkTransport.Init();
        ServerNET_RecieaveFunction.init(this);
        ServerNET_SendFunction.init(this);

        ConnectionConfig cc = new ConnectionConfig();

        reliableChanel   = cc.AddChannel(QosType.Reliable); // ReliableSequenced, Reliable
        unReliableChanel = cc.AddChannel(QosType.Unreliable);

        HostTopology topo = new HostTopology(cc, MAX_USER);

        // server socket
        hostId    = NetworkTransport.AddHost(topo, PORT, null);
        isStarted = true;

        Debug.Log("opening connection on port:" + PORT + ".");
    }
    // on update movement recieaved
    private static void UpdateMovement(byte[] data)
    {
        int     ClientId           = byteArrayToInt(data, 1);     // int 4 bytes
        Vector3 ClientNewPosition  = byteArrayToVector3(data, 5); // vector3 12 bytes
        float   ClientNewRotationY = byteArrayTofloat(data, 17);  // float 4 bytes
        int     ClientKey          = byteArrayToInt(data, 21);    // vector3 12 bytes

        // update position
        if (server.getPlayerFromClientID(ClientId) != null)
        {
            Player pawn = server.getPlayerFromClientID(ClientId).GetComponent <Player>();

            pawn.setPosition(ClientNewPosition);
            pawn.setRotationY(ClientNewRotationY);
            pawn.setKey(ClientKey);
        }
        // Debug.Log("updating player movement " + ClientId + " ClientNewPosition to:" + ClientNewPosition + " ClientNewRotationY to:" + ClientNewRotationY + " ClientKey to:" + ClientKey);

        // send info to all
        ServerNET_SendFunction.SendMovement(ClientId, ClientNewPosition, ClientNewRotationY, ClientKey);
    }