CoAP响应不包含远程发送方的定义

本文关键字:定义 程发送 包含远 响应 CoAP | 更新日期: 2023-09-27 18:15:05

我正在尝试在Visual Studio 2015中使用CoAPSharp二进制文件制作CoAP非服务器。我的目标设备是带有Windows IoT核心的树莓派。

"Remotesender"部分出现错误。我不知道如何解决它

/// <summary>
/// Called when a request is received
/// </summary>
/// <param name="coapReq">The CoAPRequest object</param>
void OnCoAPRequestReceived(CoAPRequest coapReq)
{
    //This sample only works on NON requests of type GET
    //This sample simualtes a temperature sensor at the path "sensors/temp"
    string reqURIPath = (coapReq.GetPath() != null) ? coapReq.GetPath    ().ToLower() : "";
/**
        * Draft 18 of the specification, section 5.2.3 states, that if     against a NON message,
        * a response is required, then it must be sent as a NON message
        */
    if (coapReq.MessageType.Value != CoAPMessageType.NON)
    {
        //only NON  combination supported..we do not understand this send a RST back
        CoAPResponse msgTypeNotSupported = new CoAPResponse    (CoAPMessageType.RST, /*Message type*/
                                                            CoAPMessageCode.NOT_IMPLEMENTED, /*Not implemented*/
                                                            coapReq.ID.Value /*copy message Id*/);
        msgTypeNotSupported.Token = coapReq.Token; //Always match the     request/response token
        msgTypeNotSupported.RemoteSender = coapReq.RemoteSender;
        //send response to client
        this._coapServer.Send(msgTypeNotSupported);
    }
    else if (coapReq.Code.Value != CoAPMessageCode.GET)
    {
        //only GET method supported..we do not understand this send a RST back
        CoAPResponse unsupportedCType = new CoAPResponse    (CoAPMessageType.RST, /*Message type*/
                                        CoAPMessageCode.METHOD_NOT_ALLOWED, /*Method not allowed*/
                                        coapReq.ID.Value /*copy message Id*/);
        unsupportedCType.Token = coapReq.Token; //Always match the request/response token
        unsupportedCType.RemoteSender = coapReq.RemoteSender;
        //send response to client
        this._coapServer.Send(unsupportedCType);
}
    else if (reqURIPath != "sensors/temp")
{
        //classic 404 not found..we do not understand this send a RST back 
        CoAPResponse unsupportedPath = new CoAPResponse    (CoAPMessageType.RST, /*Message type*/
                                            CoAPMessageCode.NOT_FOUND, /*Not found*/
                                            coapReq.ID.Value /*copy message Id*/);
        unsupportedPath.Token = coapReq.Token; //Always match the     request/response token
        unsupportedPath.RemoteSender = coapReq.RemoteSender;
        //send response to client
        this._coapServer.Send(unsupportedPath);
    }
    else
    {
        //All is well...send the measured temperature back
    //Again, this is a NON message...we will send this message as a JSON
    //string
    Hashtable valuesForJSON = new Hashtable();
    valuesForJSON.Add("temp", this.GetRoomTemperature());
    string tempAsJSON = JSONResult.ToJSON(valuesForJSON);
    //Now prepare the object
    CoAPResponse measuredTemp = new CoAPResponse(CoAPMessageType.NON, /*Message type*/
                                        CoAPMessageCode.CONTENT, /*Carries content*/
                                        coapReq.ID.Value/*copy message Id*/);
    measuredTemp.Token = coapReq.Token; //Always match the request/response token
    //Add the payload
    measuredTemp.Payload = new CoAPPayload(tempAsJSON);
    //Indicate the content-type of the payload
    measuredTemp.AddOption(CoAPHeaderOption.CONTENT_FORMAT,
                        AbstractByteUtils.GetBytes(CoAPContentFormatOption.APPLICATION_JSON));
    //Add remote sender address details
    measuredTemp.RemoteSender = coapReq.RemoteSender;
    //send response to client
    this._coapServer.Send(measuredTemp);
}
}
错误:

错误:CS1061 c#不包含for的定义,也找不到接受类型的第一个参数的扩展方法(您是否缺少using指令或程序集引用?)

只需遵循CoAPSharp官方网站的教程。

CoAP响应不包含远程发送方的定义

如果您想在带有Windows 10 IoT核心的树莓派上运行CoAPSharp库,您需要下载Windows 10 IoT核心的实验版本。URL为http://www.coapsharp.com/releases/。参见最后一个下载链接。

另外,您可能希望获得源代码并重新编译以获得最新的二进制文件,以便在主项目中引用。

2016年11月3日新增信息:好了,我明白问题出在哪里了。这里是一个完整的大答案:-)

  1. CoAPSharp实验库有一个小bug(虽然与你的问题无关)。我是CoAPSharp的开发人员之一,为建立这个库的公司工作。带有修复程序的更新库应该在一两天内可用。问题是同步接收。

  2. 你正在尝试运行的样本,是NETMF,而不是Windows 10物联网核心,这就是为什么你得到的错误。树莓派的实验版本没有样本。为了帮助你解决这个问题,我给出了下面一步一步的解决方案:

  3. 首先,下载最新的CoAPSharp实验库来修复同步接收错误

  4. 接下来,创建一个解决方案,在其中创建一个UWA项目并将CoAPSharp库添加到解决方案中。在西澳大学项目中引用图书馆。

5.1在这个项目中为树莓派创建一个小的服务器代码,如下所示:
    /// <summary>
    /// We will start a server locally and then connect to it
    /// </summary>
    private void TestLocalCoAPServer()
    {
        /*_coapServer variable is a class level variable*/
        _coapServer = new CoAPServerChannel();
        _coapServer.CoAPRequestReceived += OnCoAPRequestReceived;
        _coapServer.Initialize(null, 5683);
    }
    /// <summary>
    /// Gets called everytime a CoAP request is received
    /// </summary>
    /// <param name="coapReq">The CoAP Request object instance</param>
    private async void OnCoAPRequestReceived(CoAPRequest coapReq)
    {
        //send ACK back
        Debug.WriteLine("Received Request::" + coapReq.ToString());
        CoAPResponse coapResp = new CoAPResponse(CoAPMessageType.ACK, CoAPMessageCode.CONTENT, coapReq);
        coapResp.AddPayload("GOT IT!");
        await _coapServer.Send(coapResp);
    }

5.2接下来,确保树莓派上的5683端口没有被阻塞。如果是,则使用powershell解除阻塞。

5.3接下来,在桌面上创建一个新的解决方案(我使用Windows 10和Visual Studio 2015社区版),并添加另一个CoAPSharp树莓派实验库的副本。

现在在桌面上创建另一个UWA项目。同时,在这个新项目中引用CoAPSharp项目。

5.4现在在这个项目中编写一个客户端,它将从托管在树莓派上的服务器发送/接收CoAP消息:

    private async void TestCoAPAsyncClient()
    {
        /*_coapAsyncClient is declared at class level*/
        this._coapAsyncClient = new CoAPClientChannel();
        /*minwinpc was the name of the device running Windows IoT core on Raspberry Pi*/
        this._coapAsyncClient.Initialize("minwinpc", 5683);
        this._coapAsyncClient.CoAPError += delegate (Exception e, AbstractCoAPMessage associatedMsg) {
            Debug.WriteLine("Exception e=" + e.Message);
            Debug.WriteLine("Associated Message=" + ((associatedMsg != null) ? associatedMsg.ToString() : "NULL"));
        };
        this._coapAsyncClient.CoAPRequestReceived += delegate (CoAPRequest coapReq) {
            Debug.WriteLine("REQUEST RECEIVED !!!!!!!!!!!!!!" + coapReq.ToString());
        };
        this._coapAsyncClient.CoAPResponseReceived += delegate (CoAPResponse coapResp) {
            Debug.WriteLine("RESPONSE RECEIVED <<<<<<<<<<<<<" + coapResp.ToString());
        };
        CoAPRequest req = new CoAPRequest(CoAPMessageType.CON, CoAPMessageCode.GET, 100);
        //req.SetURL("coap://capi.coapworks.com:5683/v1/time/pcl?mid=CPWK-TESTM");
        req.SetURL("coap://localhost:5683/someurl");
        await this._coapAsyncClient.Send(req);
    }
  • 你可以在点击按钮或加载页面时调用TestCoAPAsyncClient()方法。

  • 我使用的设置是,一个桌面与Windows 10和Visual Studio社区版2015。这是连接到运行Windows 10 IoT核心(操作系统:10.0.10240.16384)的树莓派。我的台式机和树莓派都连接到了以太网。

  • CoAPSharp团队在链接http://www.coapsharp.com/coap-server-windows-10-iot-core-raspberry-pi/添加了一个示例来进一步阐述。