Exemple #1
0
    /// <summary>
    /// called when someone connects to server
    /// </summary>
    /// <param name="connection"></param>
    public override void Connected(BoltConnection connection)
    {
        IProtocolToken acceptToken  = connection.AcceptToken;
        IProtocolToken connectToken = connection.ConnectToken;

        var log = LogEvent.Create();
        //log.Message = string.Format("{0} connected", connection.RemoteEndPoint);

        // connected to server
        CredentialToken token = (CredentialToken)connectToken;

        if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name != GameManager.Instance.MenuSceneName)
        {
            Debug.Log("client tried to connect during gameplay: " + token.UserName);
            return;
        }

        Debug.Log("token connected: " + token.UserName);

        log.message += "connected: " + token.UserName;
        log.Send();


        // maintain credential/connection database
        ServerManager.Instance.Connections.Add(token, connection);
        ServerManager.Instance.Credentials.Add(connection, token);
        ServerManager.Instance.AddToSession(token);

        // grab all connections to the server, get their credentials
        // serialize that list, and send it as an event
        List <CredentialToken> connectedCredentials = new List <CredentialToken>();

        foreach (BoltConnection conn in BoltNetwork.connections)
        {
            if (conn != connection)
            {
                connectedCredentials.Add((CredentialToken)conn.ConnectToken);
                Debug.Log(((CredentialToken)conn.ConnectToken).DisplayName);

                // tell all clients except for this one that we're joining
                var userJoined = UserJoinedLobby.Create(conn);

                userJoined.UserToken = token;

                userJoined.Send();
            }
        }

        BinaryFormatter bf = new BinaryFormatter();
        MemoryStream    ms = new MemoryStream();

        bf.Serialize(ms, connectedCredentials);

        // send the list of connected clients to the newly-connected client
        SendConnectedClients sendClients = SendConnectedClients.Create(connection);

        sendClients.BinaryData = ms.ToArray();

        sendClients.Send();
    }
Exemple #2
0
    /// <summary>
    /// Sent by the server
    /// Received by a newly-connected client
    /// Server sends list of connected clients to the server to populate server lobby GUI
    /// </summary>
    /// <param name="evnt"></param>
    public override void OnEvent(SendConnectedClients evnt)
    {
        MemoryStream    ms = new MemoryStream();
        BinaryFormatter bf = new BinaryFormatter();

        ms.Write(evnt.BinaryData, 0, evnt.BinaryData.Length);
        ms.Seek(0, SeekOrigin.Begin);

        List <CredentialToken> connectedCredentials = (List <CredentialToken>)bf.Deserialize(ms);

        foreach (CredentialToken token in connectedCredentials)
        {
            //Messenger.Broadcast("UserAddedToLobby", token.DisplayName);
            ServerManager.Instance.AddToSession(token);
        }
    }