Page 1 of 1

How to make websockets connection

Posted: Sunday 29 October 2017 0:47
by febalci
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?

Re: How to make websockets connection

Posted: Sunday 29 October 2017 21:44
by Dnpwwo
@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

Re: How to make websockets connection

Posted: Wednesday 22 November 2017 17:58
by febalci
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
@Dnpwwo thanks for the suggestions. I am still stuck with this:

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...

Re: How to make websockets connection

Posted: Thursday 23 November 2017 8:36
by Dnpwwo
@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.

Re: How to make websockets connection

Posted: Thursday 23 November 2017 19:46
by febalci
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.
@Dnpwwo,

I solved it by using TCP/IP, None connection, i can send pings now. I do not know why i insisted on HTTP connection :D Thanks anyway.