简体   繁体   English

SSE 与乐山 LWM2M 演示服务器

[英]SSE with Leshan LWM2M Demo Server

I am trying to do an http api that interact with a Leshan Demo Server.我正在尝试做一个与乐山演示服务器交互的 http api。 I was trying to handle the OBSERVE in LWM2M, but I need to handle the notification with http.我试图在 LWM2M 中处理 OBSERVE,但我需要使用 http 处理通知。 I discovered that leshan notify using SSE.我发现乐山通知使用 SSE。 So I was trying to implement the sse client in python using requests and sseclient.因此,我尝试使用请求和 sseclient 在 python 中实现 sse 客户端。

This is my code:这是我的代码:

    response= requests.post(url_request , "format=TLV" , stream= True)    
    client = sseclient.SSEClient(response)
    for event in client.events():
        print(json.loads(event.data))

I tried to run my script but it seems like the stream is not opening and it close immediately without waiting for the answer of the server, even if requests by default implement keep_alive for TCP connection under HTTP and the stream is True. I tried to run my script but it seems like the stream is not opening and it close immediately without waiting for the answer of the server, even if requests by default implement keep_alive for TCP connection under HTTP and the stream is True.

Does someone know why?有人知道为什么吗?

Reading the sseclient documentation, the correct way so use SSEClient seems to be:阅读sseclient文档,使用 SSEClient 的正确方法似乎是:

from sseclient import SSEClient
messages = SSEClient('http://example.com/sse_stream/')
for msg in messages:
    do_something_useful(msg)

Reading the answer on Leshan Github, the stream URL for Leshan Server Demo seems to be http://your.leshan.server.org/event?ep=your_device_endpoint_name Reading the answer on Leshan Github, the stream URL for Leshan Server Demo seems to be http://your.leshan.server.org/event?ep=your_device_endpoint_name

So I tried that:所以我尝试了:

from sseclient import SSEClient
messages = SSEClient('http://localhost:8080/event?ep=my_device')
for msg in messages:
    print (msg.event, msg.data)

And it works for me: Getting this kind of results when I observe the temperature instance of Leshan Client Demo :它对我有用:当我观察 Leshan Client Demo 的温度实例时得到这种结果:

(u'NOTIFICATION', u'{"ep":"my_device","res":"/3303/0","val":{"id":0,"resources":[{"id":5601,"value":-18.9},{"id":5602,"value":31.2},{"id":5700,"value":-18.4},{"id":5701,"value":"cel"}]}}')
(u'COAPLOG', u'{"timestamp":1592296453808,"incoming":true,"type":"CON","code":"POST","mId":29886,"token":"889372029F81C124","options":"Uri-Path: \\"rd\\", \\"reWfKIgPYD\\"","ep":"my_device"}')
(u'COAPLOG', u'{"timestamp":1592296453809,"incoming":false,"type":"ACK","code":"2.04","mId":29886,"token":"889372029F81C124","ep":"my_device"}')
(u'UPDATED', u'{"registration":{"endpoint":"my_device","registrationId":"reWfKIgPYD","registrationDate":"2020-06-16T10:02:25+02:00","lastUpdate":"2020-06-16T10:34:13+02:00","address":"127.0.0.1:44400","lwM2mVersion":"1.0","lifetime":300,"bindingMode":"U","rootPath":"/","objectLinks":[{"url":"/","attributes":{"rt":"\\"oma.lwm2m\\""}},{"url":"/1/0","attributes":{}},{"url":"/3/0","attributes":{}},{"url":"/6/0","attributes":{}},{"url":"/3303/0","attributes":{}}],"secure":false,"additionalRegistrationAttributes":{}},"update":{"registrationId":"reWfKIgPYD","identity":{"peerAddress":{}},"additionalAttributes":{}}}')
(u'COAPLOG', u'{"timestamp":1592296455150,"incoming":true,"type":"NON","code":"2.05","mId":29887,"token":"3998C5DE2588F835","options":"Content-Format: \\"application/vnd.oma.lwm2m+tlv\\" - Observe: 2979","payload":"Hex:e3164563656ce8164408c03199999999999ae815e108c032e66666666666e815e208403f333333333333","ep":"my_device"}')

If you are interested by notification only, just add a if msg.event == 'NOTIFICATION': block.如果您只对通知感兴趣,只需添加一个if msg.event == 'NOTIFICATION':块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM