Functions of ballerina.net.ws package
public function <HandshakeConnection conn> cancelHandshake(int statusCode, string reason)
Cancels the handshake
Receiver:
Receiver Name | Data Type | Description |
---|---|---|
conn | HandshakeConnection | A HandshakeConnection struct |
Parameters:
Parameter Name | Data Type | Description |
---|---|---|
statusCode | int | Status code for closing the connection |
reason | string | Reason for closing the connection |
public function <Connection conn> closeConnection(int statusCode, string reason)
Close the connection
Receiver:
Receiver Name | Data Type | Description |
---|---|---|
conn | Connection | A Connection struct |
Parameters:
Parameter Name | Data Type | Description |
---|---|---|
statusCode | int | Status code for closing the connection |
reason | string | Reason for closing the connection |
public function <Connection conn> getID() (string)
Gets the ID of the WebSocket connection
Receiver:
Receiver Name | Data Type | Description |
---|---|---|
conn | Connection | A Connection struct |
Return Parameters:
Return Variable | Data Type | Description |
---|---|---|
string | ID of the connection |
public function <Connection conn> getNegotiatedSubProtocol() (string)
Gets the negotiated sub protocol of the connection
Receiver:
Receiver Name | Data Type | Description |
---|---|---|
conn | Connection | A Connection struct |
Return Parameters:
Return Variable | Data Type | Description |
---|---|---|
string | Negotiated sub protocol |
public function <Connection conn> getParentConnection() (Connection)
Gets the parent connection if there is one
Receiver:
Receiver Name | Data Type | Description |
---|---|---|
conn | Connection | Connection for which the parent connection should be retrieved |
Return Parameters:
Return Variable | Data Type | Description |
---|---|---|
Connection | The parent connection if it exists, else it is null |
public function <Connection conn> getUpgradeHeader(string key) (string)
Gets a value of a header
Receiver:
Receiver Name | Data Type | Description |
---|---|---|
conn | Connection | A Connection struct |
Parameters:
Parameter Name | Data Type | Description |
---|---|---|
key | string | Key of the header for which the value should be retrieved |
Return Parameters:
Return Variable | Data Type | Description |
---|---|---|
string | Value of the header if it exists, else it is null |
public function <Connection conn> getUpgradeHeaders() (map)
Gets a map of all the upgrade headers of the connection
Receiver:
Receiver Name | Data Type | Description |
---|---|---|
conn | Connection | A Connection struct |
Return Parameters:
Return Variable | Data Type | Description |
---|---|---|
map | Map of all the headers received in the connection upgrade |
public function <Connection conn> isOpen() (boolean)
Checks whether the connection is still open or not.
Receiver:
Receiver Name | Data Type | Description |
---|---|---|
conn | Connection | A Connection struct |
Return Parameters:
Return Variable | Data Type | Description |
---|---|---|
boolean | True if the connection is open |
public function <Connection conn> isSecure() (boolean)
Checks whether the connection is secure or not
Receiver:
Receiver Name | Data Type | Description |
---|---|---|
conn | Connection | A Connection struct |
Return Parameters:
Return Variable | Data Type | Description |
---|---|---|
boolean | True if the connection is secure |
public function <Connection conn> ping(blob data)
Ping the connection
Receiver:
Receiver Name | Data Type | Description |
---|---|---|
conn | Connection | A Connection struct |
Parameters:
Parameter Name | Data Type | Description |
---|---|---|
data | blob | Binary data to be sent |
public function <Connection conn> pong(blob data)
Send pong message to the connection
Receiver:
Receiver Name | Data Type | Description |
---|---|---|
conn | Connection | A Connection struct |
Parameters:
Parameter Name | Data Type | Description |
---|---|---|
data | blob | Binary data to be sent |
public function <Connection conn> pushBinary(blob data)
Push binary data to the connection
Receiver:
Receiver Name | Data Type | Description |
---|---|---|
conn | Connection | A Connection struct |
Parameters:
Parameter Name | Data Type | Description |
---|---|---|
data | blob | Binary data to be sent |
public function <Connection conn> pushText(string text)
Push text to the connection
Receiver:
Receiver Name | Data Type | Description |
---|---|---|
conn | Connection | A Connection struct |
Parameters:
Parameter Name | Data Type | Description |
---|---|---|
text | string | Text to be sent |
Connectors of ballerina.net.ws package
public connector WsClient (string url, string callbackService)
WebSocket client connector for connecting to a WebSocket backend
Parameters:
Parameter Name | Data Type | Description |
---|---|---|
url | string | WebSocket URL for the backend |
callbackService | string | Callback service to listen to the incoming messages from the backend |
action connect(ClientConnectorConfig config)
Connect to remote endpoint
Parameters:
Parameter Name | Data Type | Description |
---|---|---|
config | ClientConnectorConfig | ClientConnectorConfig for the connection |
Return Parameters:
Return Variable | Data Type | Description |
---|---|---|
Connection | New WebSocket connection for the connected backend | |
WsConnectorError |
action connectWithDefault()
Connect to remote endpoint with default configuration
Return Parameters:
Return Variable | Data Type | Description |
---|---|---|
Connection | New WebSocket connection for the connected backend | |
WsConnectorError |
Structs of ballerina.net.ws package
public struct BinaryFrame
Represents a WebSocket binary frame in Ballerina.
Fields:
Field Name | Data Type | Description |
---|---|---|
data | blob | Binary data of the frame |
isFinalFragment | boolean | Check whether this is the final frame. True if the frame is final frame. |
public struct ClientConnectorConfig
Configuration struct for WebSocket client connection
Fields:
Field Name | Data Type | Description |
---|---|---|
subProtocols | string[] | Negotiable sub protocols for the client |
parentConnectionID | string | Connection ID of the parent connection to which it should be bound to when connecting |
customHeaders | map | Custom headers which should be sent to the server |
idleTimeoutInSeconds | int | Idle timeout of the client. Upon timeout, onIdleTimeout resource in the client service will be triggered (if there is one defined). |
public struct CloseFrame
Represents a WebSocket close frame in Ballerina.
Fields:
Field Name | Data Type | Description |
---|---|---|
statusCode | int | Status code for the reason of the closure of the connection |
reason | string | Reason to close the connection |
public struct Connection
Represents a WebSocket connection in ballerina. This include all connection oriented operations.
Fields:
Field Name | Data Type | Description |
---|---|---|
attributes | map | Custom user attributes |
public struct HandshakeConnection
Represent the details needed before the Handshake is done.
Fields:
Field Name | Data Type | Description |
---|---|---|
connectionID | string | ID of the connection |
isSecure | boolean | True if the connection is secured |
upgradeHeaders | map | Received headers in the connection upgrade |
public struct PingFrame
Represents a WebSocket ping frame in Ballerina.
Fields:
Field Name | Data Type | Description |
---|---|---|
data | blob | Data of the frame |
public struct PongFrame
Represents a WebSocket pong frame in Ballerina.
Fields:
Field Name | Data Type | Description |
---|---|---|
data | blob | Data of the frame |
public struct TextFrame
Represents a WebSocket text frame in Ballerina.
Fields:
Field Name | Data Type | Description |
---|---|---|
text | string | Text in the text frame |
isFinalFragment | boolean | Check whether this is the final frame. True if the frame is final frame. |
public struct WsConnectorError
Error struct for WebSocket connection errors
Fields:
Field Name | Data Type | Description |
---|---|---|
msg | string | An error message explaining the error |
cause | error | The error that caused HttpConnectorError to be returned |
stackTrace | StackFrame[] | Represents the invocation stack if WsConnectorError is thrown |
Annotations of ballerina.net.ws package
annotation clientService
Service annotation to mark a WS service as a client service for a WS client connector.
annotation configuration
Configuration for a WebSocket service.
Attributes:
Attribute Name | Data Type | Description |
---|---|---|
basePath | string | Path of the WebSocket service |
subProtocols | string[] | Negotiable sub protocol by the service |
host | string | Host of the service |
port | int | Port number of the service |
wssPort | int | WSS port number of service |
idleTimeoutInSeconds | int | Idle timeout for the client connection. This can be triggered by putting onIdleTimeout resource in WS service. |
keyStoreFile | string | File path to keystore file |
keyStorePassword | string | The keystore password |
certPassword | string | The certificate password |