我试图在Visual 2015中使用CoAP二进制文件制作一个CoAPSharp非服务器。我的目标设备是带有IoT内核的Raspberry。
“剩馀”部分有错误。我不知道怎么解决
/// <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官方网站上的教程。
发布于 2016-11-01 03:28:55
如果您想要在带有Windows10 IoT内核的Raspberry上运行IoT库,您需要下载Windows10Windows IoT核心的实验性版本。网址是http://www.coapsharp.com/releases/。请参阅最后一个下载链接。
此外,您可能希望您获得源代码并重新编译,以便在您的主项目中引用最新的二进制文件。
补充信息添加在3-11-16:好的,我知道问题是什么.以下是一个完整而庞大的答案:-)
5.1在此项目中为Raspberry Pi创建一个小型服务器代码,如下所示:
/// <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消息:
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);
}CoAPSharp团队在链接http://www.coapsharp.com/coap-server-windows-10-iot-core-raspberry-pi/中添加了一个示例,以进一步阐述这一点。
https://stackoverflow.com/questions/40279559
复制相似问题