예제 #1
0
 private static void OnRoomJoin() => OnRoomJoined?.DelegateSafeInvoke();
예제 #2
0
        private void MessageHandler(object sender, MessageEventArgs e)
        {
            var bb = new ByteBuffer(e.RawData);

            ServerMessage msg = ServerMessage.GetRootAsServerMessage(bb);

            switch (msg.Type)
            {
            case msgType.RoomStateUpdate:
                //handle message
                if (msg.Data <StateUpdate>() == null)
                {
                    print("empty state update. this should not happen");
                    return;
                }
                StateUpdate?stateUpdate = msg.Data <StateUpdate>();
                if (stateUpdate != null)
                {
                    StateUpdate sup =
                        stateUpdate.Value;
                    UpdateLocalState(sup);
                }
                break;

            case msgType.SocketReady:
                print("connected to server");
                StringData?stringData = msg.Data <StringData>();
                if (stringData != null)
                {
                    OnConnectedArgs connectedEventArgs = new OnConnectedArgs
                    {
                        sid = stringData.Value.Data,
                    };
                    NeuraCore.Instance.sid = connectedEventArgs.sid;
                }
                if (OnConnected != null)
                {
                    OnConnected.Invoke(this, new EventArgs());
                }
                break;

            case msgType.SocketRoomJoined:
            {
                print("Joined room ");
                inRoom = true;
                UsersInRoom.Add(localUserName);
                NeuraCore.Instance.connectionState = ConnectionState.Connected;
                if (msg.DataType != Transport.FlatBuffers.msg.StateUpdate)
                {
                    return;
                }
                if (msg.Data <StateUpdate>().HasValue)
                {
                    var initStateSUP = msg.Data <StateUpdate>().Value;
                    UpdateLocalState(initStateSUP);
                }
                if (OnRoomJoined != null)
                {
                    OnRoomJoined.Invoke(this, RoomName);
                }
            }
            break;

            case msgType.RoomCreated:
                StringData?createMsg = msg.Data <StringData>();
                if (createMsg != null)
                {
                    //var rmName = createMsg.Value.Data;
                    //RoomName = rmName;
                    print("room " + RoomName + " has been created");
                    if (OnRoomCreated != null)
                    {
                        OnRoomCreated.Invoke(this, RoomName);
                    }
                    if (string.IsNullOrEmpty((globalState ?? (globalState = new RoomStateGen())).siteDrive))
                    {
                        //Handle things like critical room state here to make sure that the initial state sent has the required information
                        //For onsight the site drive is of critical importance so we are setting it below
                        //globalState.siteDrive = string.IsNullOrEmpty(requestedSiteDrive)
                        //    ? MultiUserConnectionManager.Instance.CurrentSiteDriveJSON
                        //    : requestedSiteDrive;
                    }
                    Debug.Assert(!string.IsNullOrEmpty(globalState.siteDrive));

                    NeuraCore.Instance.SendInitialState(ServerMessageFactory.BuildMessage(globalState));
                }
                break;

            case msgType.RoomUserOnjoined:
                StringData?joinedMsg = msg.Data <StringData>();
                if (joinedMsg != null)
                {
                    var user = joinedMsg.Value.Data;
                    print(user + " has joined the room");
                    UsersInRoom.Add(user);
                    if (OnUserJoined != null)
                    {
                        OnUserJoined.Invoke(this, new UserJoinedEventArgs
                        {
                            username = user
                        });
                    }
                }
                break;

            case msgType.RoomUserOnLeft:
                StringData?leftMsg = msg.Data <StringData>();
                if (leftMsg != null)
                {
                    var user = leftMsg.Value.Data;
                    print(user + " has left the room");
                    if (UsersInRoom.Contains(user))
                    {
                        UsersInRoom.Remove(user);
                    }
                    if (OnUserLeft != null)
                    {
                        OnUserLeft.Invoke(this, new UserLeftEventArgs
                        {
                            username = user
                        });
                    }
                }
                break;
            }
        }