コード例 #1
0
        private void MessageReceived(object o, MessageEventArgs args)
        {
            if (!_enable)
            {
                return;
            }
            var msg = args.Data;

            var response = PlayerResponse.ToObject(msg);

            StatusReceived?.Invoke(response);
        }
コード例 #2
0
        private async void Connect()
        {
            try
            {
                if (messageWebSocket == null)
                {
                    messageWebSocket = new MessageWebSocket();
                    messageWebSocket.Control.MessageType = SocketMessageType.Utf8;
                    messageWebSocket.MessageReceived    += (sender, args) =>
                    {
                        using (DataReader reader = args.GetDataReader())
                        {
                            reader.UnicodeEncoding = UnicodeEncoding.Utf8;

                            try
                            {
                                string read     = reader.ReadString(reader.UnconsumedBufferLength);
                                var    response = PlayerResponse.ToObject(read);
                                StatusReceived?.Invoke(response);
                            }
                            catch (Exception ex)
                            {
                                LogReceived?.Invoke(ex.Message);
                            }
                        }
                    };
                    messageWebSocket.Closed += (sender, args) =>
                    {
                        _websocketConnected = false;
                        ConnectionChanged?.Invoke(_websocketConnected);
                    };
                }

                await messageWebSocket.ConnectAsync(new Uri(WebsocketUrl));

                messageWriter = new DataWriter(messageWebSocket.OutputStream);

                _websocketConnected = true;
                ConnectionChanged?.Invoke(_websocketConnected);
                AddRegister(_registered);
            }
            catch (Exception)
            {
                _websocketConnected = false;
                ConnectionChanged?.Invoke(_websocketConnected);
                await Task.CompletedTask;
            }
        }