Example #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();
    }
 protected virtual void OnUserJoinedLobby(object source, EventArgs e)
 {
     UserJoinedLobby?.Invoke(source, e);
 }