IObservable <MqttApplicationMessage>不包含“在哪里”的定义

时间:2019-09-19 11:21:16

标签: c# xamarin.android mqtt

我正在尝试使用this在Xamarin Android应用中实现MQTT。但是我遇到了错误

  

'IObservable<MqttApplicationMessage>'不包含'Where'的定义,并且找不到可访问的扩展方法'Where'接受类型'IObservable<MqttApplicationMessage>'的第一个参数(您是否缺少using指令?还是程序集引用?)。

试图删除Where只是为了看看它是否可以工作,而是得到

  

无法将lambda表达式转换为类型'IObserver<MqttApplicationMessage>',因为它不是委托类型。

尝试使用Ably.IO和PubNub并能够实现其发布/订阅功能,但是由于其消息速率限制,我正在尝试这样做,希望它没有消息速率限制。 (例如:大约30-35msg / s的限制)

using System.Collections.Generic;
using System.Text;
using Android.App;
using Android.OS;
using Android.Widget;
using OxyPlot;
using OxyPlot.Series;
using OxyPlot.Xamarin.Android;
using OxyPlot.Axes;
using PubnubApi;
using System.Linq;
using Java.Lang;
using IO.Ably;
using IO.Ably.Realtime;
using System.Drawing;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.ComponentModel;
using System.Net.Mqtt;
using System.Reactive.Linq;
using System.Threading.Tasks;

protected override async void OnCreate(Bundle savedInstanceState)
{ 
    base.OnCreate(savedInstanceState);

    var configuration = new MqttConfiguration
    {
        //  BufferSize = 128 * 1024,
        Port = 1883,
        KeepAliveSecs = 10,
        WaitTimeoutSecs = 2,
        MaximumQualityOfService = MqttQualityOfService.AtMostOnce,
        AllowWildcardsInTopicFilters = true
    };
    var client = await MqttClient.CreateAsync("test.mosquitto.org", configuration);
    var sessionState = await client.ConnectAsync
        (new MqttClientCredentials(clientId: "foo"), cleanSession: true);
    await client.SubscribeAsync("foo/bar/sampletopic", 
        MqttQualityOfService.AtMostOnce); //QoS0

    client.MessageStream.Where(msg => msg.Topic == "foo/bar/sampletopic").
      Subscribe(msg =>RcvdMsgtext.text=Encoding.ASCII.GetString(msg.payload) );

    pubbtn.Click += async delegate
    {

        var message1 = new MqttApplicationMessage(
            "foo/bar/sampletopic",
            Encoding.UTF8.GetBytes("Foo Message 1"));
        await client.PublishAsync(message1, MqttQualityOfService.AtMostOnce);
    }

//note: Removed some parts but basically all i did was placed  it on OnCreate, changed it
//to async. In using Ably or PubNub "async" is not needed in OnCreate

在单击“发布”按钮时期望显示/更改的消息。

编辑:能够通过检查我的引用来绕过此错误,看到带有黄色图标的System.Reactive,双击它,并且IObservable错误消失了,但是产生了错误,需要我添加System.Threading.Tasks.Extensions我在4.0.0版上做了(阅读有关使用此版本进行构建的知识)。

编辑:在代码中添加了使用。.

现在,代码在此行中运行:

var client = await MqttClient.CreateAsync("test.mosquitto.org",       configuration);
       var sessionState = await client.ConnectAsync
       (new MqttClientCredentials(clientId: "foo"), cleanSession: true);

我收到错误

  

System.TypeLoadException:无法解析来自typeref的令牌01000023的类型(程序集“ System.Core,版本= 2.0.5.0,文化=中性,PublicKeyToken = 7cec85d7bea7798e”中的预期类“ System.Reactive.Concurrency.IScheduler”)

1 个答案:

答案 0 :(得分:0)

确保System.Reactive.Linq在“使用”部分代码以及在引用中。对于VS 2017,可能会使用较低版本,因为这还需要您添加System.Threading.Tasks.Extensions

还使用M2MQTT作为替代。