Hi all,
trying to make a websockets connection to www.seismicportal.eu. The client connects and listens for events and response event is in JSON. How do we make it under python or lua script?
How to make websockets connection
Moderators: leecollings, remb0
- Dnpwwo
- Posts: 820
- Joined: Sunday 23 March 2014 9:00
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Beta
- Location: Melbourne, Australia
- Contact:
Re: How to make websockets connection
@febalci,
The Python plugin framework can do that, have a look at the Kodi example here: https://github.com/domoticz/domoticz/bl ... es/Kodi.py.
Kodi's send JSON responses and event although this plugin JSON pings them as well.
Plugins are documented on the wiki: http://www.domoticz.com/wiki/Developing_a_Python_plugin
The Python plugin framework can do that, have a look at the Kodi example here: https://github.com/domoticz/domoticz/bl ... es/Kodi.py.
Kodi's send JSON responses and event although this plugin JSON pings them as well.
Plugins are documented on the wiki: http://www.domoticz.com/wiki/Developing_a_Python_plugin
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw
-
febalci
- Posts: 331
- Joined: Monday 03 July 2017 19:58
- Target OS: NAS (Synology & others)
- Domoticz version:
- Contact:
Re: How to make websockets connection
@Dnpwwo thanks for the suggestions. I am still stuck with this:Dnpwwo wrote: Sunday 29 October 2017 21:44 @febalci,
The Python plugin framework can do that, have a look at the Kodi example here: https://github.com/domoticz/domoticz/bl ... es/Kodi.py.
Kodi's send JSON responses and event although this plugin JSON pings them as well.
Plugins are documented on the wiki: http://www.domoticz.com/wiki/Developing_a_Python_plugin
Seismicportal requires a long-lived websocket connection. Whenever a new message arrives, the server just sends it in a JSON format. You have to keep the connection open to get new events. I tried 2 ways:
1. websocket-client module:
Everything works OK, i got the messages, the websocket-client send pings to the server in a timely manner, since if the server is not pinged, it has a timeout of 1 min. and it kicks the client out. The problem with this approach is that you have to use "ws.run_forever()" to keep the connection open, but since the Domoticz Plugin Framework doesn't support threading, the plugin thread just stucks in that line; event tough i close the domoticz server down, i have to kill the domoticz server process seperately, there's no way to use ws.close() since the ws.run_forever() keeps the plugin occupied.
2. Native HTTP method:
I tried to make an upgrade to the native plugin HTTP connection with:
Code: Select all
onstart()
self.socketConn = Domoticz.Connection(Name="EmscConn", Transport="TCP/IP", Protocol="HTTP", Address="www.seismicportal.eu", Port="80")
onconnect()
Headers = { 'Content-Type': 'text/xml; charset=utf-8', \
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', \
'Host': "www.seismicportal.eu", \
'User-Agent':'Domoticz/1.0', \
'Upgrade': 'websocket', \
'Connection':'keep-alive, Upgrade', \
'Sec-Websocket-Key': 'fv9RzjCDxpJj4FUXmUo7ZA==', \
'Sec-Websocket-Version': '13', \
'Sec-Websocket-Extensons':'permessage-deflate'}
Connection.Send({"Verb":"GET", "URL":"/standing_order/websocket", "Headers": Headers})
It looks like it is connected, but since i couldn't send a ping frame the connection drops out after 1 minute. Connection.send() only supports 'Verb' and 'Status', it doesn't support to send a raw frame. And also for the JSON:
Code: Select all
def DumpHTTPResponseToLog(httpDict):
if isinstance(httpDict, dict):
Domoticz.Log("HTTP Details ("+str(len(httpDict))+"):")
for x in httpDict:
if isinstance(httpDict[x], dict):
Domoticz.Log("--->'"+x+" ("+str(len(httpDict[x]))+"):")
for y in httpDict[x]:
Domoticz.Log("------->'" + y + "':'" + str(httpDict[x][y]) + "'")
else:
Domoticz.Log("--->'" + x + "':'" + str(httpDict[x]) + "'")
does not solve the incoming JSON, since the incoming message is not http defined i guess. I tried to make a JSON connection but this time the GET Verb for the elevated websocket connection creates an error.
Do you have any suggestions? I basically prefer to solve it in a native way but as i see there're not any examples, only the samsung tv plugin with Short-lived one-off send-receive connections using websocket-client module which doesn't help me much. Thanks...
- Dnpwwo
- Posts: 820
- Joined: Sunday 23 March 2014 9:00
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Beta
- Location: Melbourne, Australia
- Contact:
Re: How to make websockets connection
@febalci,
Looks like you've done the hard bit, a ping should be easy.
If you send me your plugin I could have a play with it for you.
Looks like you've done the hard bit, a ping should be easy.
If you send me your plugin I could have a play with it for you.
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw
-
febalci
- Posts: 331
- Joined: Monday 03 July 2017 19:58
- Target OS: NAS (Synology & others)
- Domoticz version:
- Contact:
Re: How to make websockets connection
@Dnpwwo,Dnpwwo wrote: Thursday 23 November 2017 8:36 @febalci,
Looks like you've done the hard bit, a ping should be easy.
If you send me your plugin I could have a play with it for you.
I solved it by using TCP/IP, None connection, i can send pings now. I do not know why i insisted on HTTP connection
Who is online
Users browsing this forum: Bing [Bot] and 1 guest