首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CoAP响应不包含远程发送方的定义。

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

Stack Overflow用户
提问于 2016-10-27 08:19:08
回答 1查看 361关注 0票数 0

我试图在Visual 2015中使用CoAP二进制文件制作一个CoAPSharp非服务器。我的目标设备是带有IoT内核的Raspberry。

“剩馀”部分有错误。我不知道怎么解决

代码语言:javascript
复制
/// <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#不包含接受第一个类型参数的定义和扩展方法(您是缺少使用指令还是程序集引用?)

简单地遵循CoAPSharp官方网站上的教程。

EN

回答 1

Stack Overflow用户

发布于 2016-11-01 03:28:55

如果您想要在带有Windows10 IoT内核的Raspberry上运行IoT库,您需要下载Windows10Windows IoT核心的实验性版本。网址是http://www.coapsharp.com/releases/。请参阅最后一个下载链接。

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

补充信息添加在3-11-16:好的,我知道问题是什么.以下是一个完整而庞大的答案:-)

  1. CoAPSharp实验库有一个小错误(尽管与您的问题无关)。我是CoAPSharp的开发人员之一,为建立图书馆的公司工作。使用该修复程序更新的库应该在一两天内可用。问题是同步接收。
  2. 您正在尝试运行的示例是NETMF,而不是Windows10 IoT核心,这就是为什么您要获得错误。无样品进行覆盆子Pi实验释放。为了帮助你解决这个问题,我给出以下一步一步的解决方案:
  3. 首先,下载最新的CoAPSharp实验库,以获得用于同步接收错误的修复。
  4. 接下来,创建一个解决方案,其中,创建一个UWA项目并将CoAPSharp库添加到解决方案中。参考UWA项目中的库。

5.1在此项目中为Raspberry Pi创建一个小型服务器代码,如下所示:

代码语言:javascript
复制
    /// <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在Raspberry Pi上不被阻塞。如果是,则使用powershell解除阻塞。

5.3接下来,在桌面上创建一个新的解决方案(我使用了Windows10和VisualStudio2015CommunityEdition),并添加了另一个CoAPSharp Raspberry实验库副本。

5.3现在在桌面上创建另一个UWA项目。另外,请参考这个新项目中的CoAPSharp项目。

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

代码语言:javascript
复制
    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);
    }
  1. 您可以在单击按钮或加载页面时调用TestCoAPAsyncClient()方法。
  2. 我使用的设置是一个带有Windows 10和2015的桌面。这与运行Windows 10 IoT内核的Raspberry Pi连接(OS: 10.0.10240.16384)。我的桌面和Raspberry Pi都连接到以太网上。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40279559

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档