Exemplo n.º 1
0
        public void SendChatMessage(string message)
        {
            var msg = new MessageChatMessage()
            {
                Username = Username,
                Message  = message
            };

            socket?.Send(JsonConvert.SerializeObject(msg));
        }
Exemplo n.º 2
0
        public Multiplayer(string Server, string Channel, string Username)
        {
            this.Channel  = Channel;
            this.Username = Username;

            socket = new WebSocket(Server);

            socket.OnOpen += (sender, e) =>
            {
                MessageConnect connect = new MessageConnect()
                {
                    Channel  = this.Channel,
                    Username = this.Username,
                    Version  = Shared.Version
                };

                socket.Send(JsonConvert.SerializeObject(connect));
            };

            socket.OnClose += (sender, e) =>
            {
                if (!ManualClose && !string.IsNullOrWhiteSpace(e.Reason))
                {
                    OnConnectionFailed?.Invoke(this, new ConnectionFailedEventArgs());
                }
            };

            socket.OnMessage += (sender, e) =>
            {
                MessageType messageType = JsonConvert.DeserializeObject <MessageType>(e.Data);
                if (messageType.Type == 0) // Connection Successful
                {
                    MessageConnectionSuccessful connectionSuccessful = JsonConvert.DeserializeObject <MessageConnectionSuccessful>(e.Data);

                    IsHost = connectionSuccessful.IsHost;

                    ConnectionSuccessfulEventArgs succ = new ConnectionSuccessfulEventArgs()
                    {
                        IsHost       = connectionSuccessful.IsHost,
                        HostUsername = connectionSuccessful.HostUsername
                    };

                    OnConnectionSuccessful?.Invoke(this, succ);
                }
                else if (messageType.Type == 1) // Username in use
                {
                    OnUsernameInUse?.Invoke(this, new UsernameInUseEventArgs());
                    socket.Close();
                }
                else if (messageType.Type == 2) // Host left the channel
                {
                    OnHostLeftChannel?.Invoke(this, new HostLeftChannelEventArgs());
                    socket.Close();
                }
                else if (messageType.Type == 3)
                {
                    MessageVersionMismatch mismatch = JsonConvert.DeserializeObject <MessageVersionMismatch>(e.Data);

                    var args = new VersionMismatchEventArgs()
                    {
                        Version = mismatch.Version
                    };

                    OnVersionMismatch?.Invoke(this, args);
                }
                // -------
                else if (messageType.Type == 10) // User Joined
                {
                    MessageUserJoined user = JsonConvert.DeserializeObject <MessageUserJoined>(e.Data);

                    var args = new UserJoinedEventArgs()
                    {
                        Username = user.Username
                    };

                    OnUserJoined?.Invoke(this, args);
                }
                else if (messageType.Type == 11) // User Left
                {
                    MessageUserLeft user = JsonConvert.DeserializeObject <MessageUserLeft>(e.Data);

                    var args = new UserLeftEventArgs()
                    {
                        Username = user.Username
                    };

                    OnUserLeft?.Invoke(this, args);
                }
                else if (messageType.Type == 12) // Chat Message
                {
                    MessageChatMessage chatMessage = JsonConvert.DeserializeObject <MessageChatMessage>(e.Data);

                    var args = new ChatMessageEventArgs()
                    {
                        Username = chatMessage.Username,
                        Message  = chatMessage.Message
                    };

                    OnChatMessage?.Invoke(this, args);
                }
                // -------
                else if (messageType.Type == 20) // Time Update
                {
                    MessageTimeUpdate timeUpdate = JsonConvert.DeserializeObject <MessageTimeUpdate>(e.Data);

                    var args = new TimeUpdateEventArgs()
                    {
                        Remaining = timeUpdate.Remaining,
                        Total     = timeUpdate.Total
                    };

                    OnTimeUpdate?.Invoke(this, args);
                }
                else if (messageType.Type == 21) // Send Effect
                {
                    MessageEffect effect = JsonConvert.DeserializeObject <MessageEffect>(e.Data);

                    var args = new EffectEventArgs()
                    {
                        Word     = effect.Word,
                        Duration = effect.Duration,
                        Voter    = effect.Voter,
                        Seed     = effect.Seed
                    };

                    OnEffect?.Invoke(this, args);
                }
                else if (messageType.Type == 22) // Votes
                {
                    MessageVotes votes = JsonConvert.DeserializeObject <MessageVotes>(e.Data);

                    var args = new VotesEventArgs()
                    {
                        Effects    = votes.Effects,
                        Votes      = votes.Votes,
                        LastChoice = votes.LastChoice
                    };

                    OnVotes?.Invoke(this, args);
                }
            };
        }