Websocket handling - nos86/VSCP-Helper-for-Python GitHub Wiki
This page is dedicated to all classes implemented as low-level layer used to handle the communication with the VSCP Daemon installed on server. Classes present in vscphelper.websocket
are:
This class (vscphelper.websocket.websocket) is in charge to handle the websocket connection with VSCP Daemon, exchanging message and handling answers and events.
Protocol used in communication is completely described here
Functions are:
init(hostname='localhost', port=8080, debug = False, timeout=2, eventCallback=None)
- hostname defines the ip of server where VSCPd is installed. Default value is localhost
- port defines the listening port on server for websocket. Default value is the VSCPd default port (8080)
- timeout defines the maximum time to get an answer after a string is sent. Default value is 2 seconds
-
eventCallback defines a function will be called when a streaming event is received. This parameter is mandatory and the callsign of function is defined as
event_callback(answer)
As soon as the object is initialized, websocket is opened.
If connection fails, an VSCP_ERROR_COMMUNICATION
Exception is risen otherwise, as soon as the authentication seed is sent by server, it is stored in object variable named seed
setTimeout(self, timeout)
This function allows to change the maximum time to get an answer from server.
Timeout is applied only for answer expected after a string is sent. Event data is not affected by this value.
If zero is passed as argument, ValueError will be risen
send(self, msg)
This function allows to send a string (msg
) to server. It's a blocking routine that returns the answer from websocket. Returned object is vschelper.websocket.answer
. If server do not answer in time, VSCPException
is risen with code: VSCP_ERROR_TIMEOUT
Variables are:
-
connected: boolean variable that indicates if websocket is connected or not
- seed: string variable that contains the seed sent by websocket for authentication
Example how-to you this class:
def receiveEvent(self, msg):
print(msg.message)
ws = websocket(hostname='127.0.0.1', port=8080, eventCallback = receiveEvent, debug=True)
ws.send("C;NOOP")