Skip to content

Request obs = Request.NewGet(); obs.URI = new Uri("coap://127.0.0.1/time"); obs.MarkObserve(); Observe fail. ??? #78

@cwf689

Description

@cwf689

I use CoAP.Net 1.04.
server code:
//----------------------------------------------
//Resource code:
class TimeResource : Resource
{
private System.Threading.Timer _timer;
private DateTime _now;

  public TimeResource(String name)
      : base(name)
  {
      Attributes.Title = "GET the current time";
      Attributes.AddResourceType("CurrentTime");
      Observable = true;

      _timer = new System.Threading.Timer(Timed, null, 0, 10000);
  }

  private void Timed(Object o)
  {
      _now = DateTime.Now;
      Changed();
      Debug.WriteLine($"{_now},数值改变");
  }

  protected override void DoGet(CoapExchange exchange)
  {
      exchange.Respond(StatusCode.Content, _now.ToString(), MediaType.TextPlain);
      Debug.WriteLine($"{DateTime.Now},数值改变时间:{_now.ToString()}");
  }              CoapServer server = new CoapServer();

//----------------------------------------------
//start server code:
server.Add(new TimeResource("time"));
server.Start();
//----------------------------------------------
client code:
public void Observe()
{
Request obs = Request.NewGet();
obs.URI = new Uri("coap://127.0.0.1/time");
obs.MarkObserve();
// add event handler
obs.Respond += delegate (Object? sender, ResponseEventArgs e)
{
// this is a new notification
Response response = e.Response;
// TODO update window state here
// you may need to switch to the UI thread first
};
//obs.SetPayload("data from client");
obs.Send();
}
//----------------------------------------------
question:
obs.Respond += delegate (Object? sender, ResponseEventArgs e) ;
delegate will no any response
but i remove "obs.MarkObserve();" ,delegate will response received
so i can not Observe. what's wrong?
help me! thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions