コード例 #1
0
        public async Task PublishAsync(string resourceUriString, string contentType, byte[] payload, bool confirmable, Action <CodeType, string, byte[]> action)
        {
            if (!channel.IsConnected)
            {
                await ConnectAsync();
            }

            session.UpdateKeepAliveTimestamp();

            byte[] token         = CoapToken.Create().TokenBytes;
            ushort id            = session.CoapSender.NewId(token, null, action);
            string scheme        = channel.IsEncrypted ? "coaps" : "coap";
            string coapUriString = GetCoapUriString(scheme, config.Authority, resourceUriString); //String.Format("{0}://{1}?r={2}", scheme, config.Authority, resourceUriString);

            RequestMessageType mtype = confirmable ? RequestMessageType.Confirmable : RequestMessageType.NonConfirmable;
            CoapRequest        cr    = new CoapRequest(id, mtype, MethodType.POST, token, new Uri(coapUriString), MediaTypeConverter.ConvertToMediaType(contentType), payload);

            queue.Enqueue(cr.Encode());

            while (queue.Count > 0)
            {
                byte[] message = queue.Dequeue();
                Task   t       = channel.SendAsync(message);
                await Task.WhenAll(t);
            }
        }
コード例 #2
0
        public async Task ObserveAsync(string resourceUriString, Action <CodeType, string, byte[]> action)
        {
            if (!channel.IsConnected)
            {
                await ConnectAsync();
            }

            session.UpdateKeepAliveTimestamp();

            byte[]      token         = CoapToken.Create().TokenBytes;
            ushort      id            = session.CoapSender.NewId(token, true, action);
            string      scheme        = channel.IsEncrypted ? "coaps" : "coap";
            string      coapUriString = GetCoapUriString(scheme, resourceUriString);
            CoapRequest cr            = new CoapRequest(id, RequestMessageType.NonConfirmable, MethodType.GET, token,
                                                        new Uri(coapUriString), null)
            {
                Observe = true
            };

            observers.Add(resourceUriString, Convert.ToBase64String(token));
            byte[] observeRequest = cr.Encode();
            await channel.SendAsync(observeRequest);
        }