Example #1
0
        public async Task <dynamic> PostQuery(GraphQLQuery query)
        {
            HttpContent         httpContent = new StringContent(query.getPayload(), Encoding.UTF8, "application/json");
            HttpResponseMessage response    = await httpClient.PostAsync(httpEndpoint, httpContent);

            response.EnsureSuccessStatusCode();
            Stream stream = await response.Content.ReadAsStreamAsync();

            return(DeserializeJsonFromStream <dynamic>(stream));
        }
Example #2
0
        public async Task <bool> AddSubscription(GraphQLQuery query, Action callbackMethod)
        {
            HttpContent         httpContent = new StringContent(query.getPayload(), Encoding.UTF8, "application/json");
            HttpResponseMessage response    = await httpClient.PostAsync(httpEndpoint, httpContent);

            response.EnsureSuccessStatusCode();

            Stream stream = await response.Content.ReadAsStreamAsync();

            SubscriptionHttpResponseModel result = DeserializeJsonFromStream <SubscriptionHttpResponseModel>(stream);

            Console.WriteLine(JsonConvert.SerializeObject(result));

            Uri    wsUri    = null;
            string clientId = null;
            string topic    = result.extensions.subscription.newSubscriptions["onUpdateCharacter"]["topic"];

            foreach (var conn in result.extensions.subscription.mqttConnections)
            {
                if (wsUri == null)
                {
                    foreach (var t in conn.topics)
                    {
                        if (String.Equals(topic, t))
                        {
                            wsUri    = new Uri(conn.url);
                            clientId = conn.client;
                            Console.WriteLine("Topic == " + t);
                            break;
                        }
                        else
                        {
                            Console.WriteLine("Topic != " + t);
                        }
                    }
                }
            }
            if (wsUri == null)
            {
                Console.WriteLine("wsUri still null");
            }

            string wsEndpoint = wsUri.Host + wsUri.PathAndQuery + wsUri.Fragment;

            var factory    = new MqttFactory();
            var mqttClient = factory.CreateMqttClient();
            var options    = new MqttClientOptionsBuilder()
                             .WithWebSocketServer(wsEndpoint)
                             .WithTls()
                             .WithClientId(clientId)
                             .Build();

            Console.WriteLine(JsonConvert.SerializeObject(options));
            MQTTnet.Client.Connecting.MqttClientAuthenticateResult r0 = mqttClient.ConnectAsync(options, CancellationToken.None).Result;
            Console.WriteLine("MQTT Connect: " + r0.ResultCode.ToString());

            mqttClient.UseApplicationMessageReceivedHandler(e =>
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Received message:");
                Console.WriteLine($"+ Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}");
                Console.WriteLine();
                Console.ResetColor();
            });

            MqttClientSubscribeResult r1 = mqttClient.SubscribeAsync(new TopicFilterBuilder().WithTopic(topic).Build()).Result;

            Console.WriteLine("MQTT Subscription: " + r1.Items.ToString());
            while (true)
            {
            }


/*
 *          mqttClient.UseConnectedHandler(async e =>
 *          {
 *              Console.WriteLine("Connected with server!");
 *              await mqttClient.SubscribeAsync(new TopicFilterBuilder().WithTopic(topic).Build());
 *              Console.WriteLine("Subscribed!");
 *
 *          });
 */
            return(true);

            // TODO: Return something about the callback?
        }