Raw Socket Interface
Contents
Overview
NOTE: This interface is in alpha status and some users have reported issues with their hubs when using this, use at your own risk and please report any issues in the community Hubitat allows for opening and maintaining a connection to a tcp socket endpoint from the hub. All code needs to be contained in a driver, there is no option to open a socket connection from an app. Hubitat provides methods to connect and disconnect to the endpoint, in addition it is required to create a method in the driver that accepts incoming messages (parse) and another method that will be called with any status updates for the endpoint (socketStatus).
Methods
Hubitat Provided Methods (Since 2.1.2)
connect
- Signature
void connect(String ip, int port)
void connect(String ip, int port, [options (name/value pairs)])
- Parameters
- ip - The ip address to connect to.
- port - The port to connect to.
- options - Optional parameters to configure the web socket connection. Possible values:
- byteInterface - defaults to 'false'. If set to true, messages are handled as byte arrays and will be hex string encoded.
- readDelay - Optional. The amount of time in milliseconds between reads from the socket, defaults to 150. (Cannot be used with eol)
- eol - Optional. A character that denotes the end of a line. (Cannot be used with readDelay)
- Examples
// Connect with basic settings interfaces.rawSocket.connect("192.168.1.15", 8064)
close
- Signature
void close()
- Examples
// Close connection interfaces.rawSocket.close()
sendMessage
- Signature
void sendMessage(String message)
- Parameters
- message - The message to send. If this connection is a byte interface, then this must be a hex encoded String.
- Examples
// Send string message interfaces.rawSocket.sendMessage("My message")
Hubitat Provided Methods (Deprecated)
webSocketConnect
- Signature
void hubitat.helper.InterfaceUtils.webSocketConnect(DeviceWrapper device, String ip, int port)
void hubitat.helper.InterfaceUtils.webSocketConnect(DeviceWrapper device, String ip, int port, [options (name/value pairs)])
- Parameters
- device - The current device that is calling the method.
- ip - The ip address to connect to.
- port - The port to connect to.
- options - Optional parameters to configure the web socket connection. Possible values:
- byteInterface - defaults to 'false'. If set to true, messages are handled as byte arrays and will be hex string encoded.
- readDelay - Optional. The amount of time in milliseconds between reads from the socket, defaults to 150. (Cannot be used with eol)
- eol - Optional. A character that denotes the end of a line. (Cannot be used with readDelay)
webSocketClose
- Signature
void webSocketClose(DeviceWrapper device)
- Parameters
- device - The current device that is calling the method.
sendWebSocketMessage
- Signature
void sendWebSocketMessage(DeviceWrapper device, String message)
- Parameters
- device - The current device that is calling the method.
- message - The message to send. If this connection is a byte interface, then this must be a hex encoded String.
User defined methods
parse(String message)
- This method is called with any incoming messages from the socket server. This is a standard method for drivers.
socketStatus(String message)
- This method is called with any status messages from the socket client connection (disconnections, errors during connect, etc)