Bundle Shared networking types used across all HTTP libraries: Url (parsing and construction), Response (status code, headers, body), Cookie, and WebDownloader for streaming downloads. Compile with -lib net.
WebSocket
Web socket client
client := WebSocket->New(Url->New("ws://echo.websocket.org"));
if(client->Connect()) {
client->ReadSocketText()->PrintLine();
client->WriteTextSocket("Hello World!");
client->ReadSocketText()->PrintLine();
client->CloseSocket();
};Operations
- New
- AddHeader
- CloseSocket
- Connect
- EnableCookies
- GetCookies
- GetIssuer
- GetStatusCode
- GetStatusMessage
- GetSubject
- PingSocket
- ReadSocketBinary
- ReadSocketText
- RemoveHeader
- WriteSocket
AddHeader #
Adds a HTTPS request header
method : public : AddHeader(name:String, value:String) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| name | String | header name |
| value | String | header value |
Example
ws := Web.HTTP.WebSocket->New(Web.HTTP.Url->New("wss://secure.example.com/ws"));
ws->AddHeader("Authorization", "Bearer token123");
ws->Connect();CloseSocket #
Closes the web socket connection
method : public : CloseSocket() ~ NilExample
ws->CloseSocket();CloseSocket #
Closes the web socket connection
method : public : CloseSocket(reason:Int) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| reason | Int | reason to close socket |
Example
ws->CloseSocket(1000); # 1000 = normal closureConnect #
Opens a connection to a web socket
method : public : Connect() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if connected, false otherwise |
Example
ws := Web.HTTP.WebSocket->New(Web.HTTP.Url->New("wss://echo.websocket.org"));
if(ws->Connect()) {
"Connected"->PrintLine();
ws->CloseSocket();
};EnableCookies #
Sets cookie support
method : public : EnableCookies(cookies_enabled:Bool) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| cookies_enabled | Bool | true if cookies are enabled, false otherwise |
GetCookies #
Gets the cookies
method : public : GetCookies() ~ Vector<Cookie>Return
| Type | Description |
|---|---|
| Vector<Cookie> | vector of cookies |
GetIssuer #
Gets the socket's X.509 certificate issuer name
method : public : GetIssuer() ~ StringReturn
| Type | Description |
|---|---|
| String | certificate issuer name |
GetStatusCode #
Gets the last status code
method : public : GetStatusCode() ~ IntReturn
| Type | Description |
|---|---|
| Int | last status code |
Example
ws := Web.HTTP.WebSocket->New(Web.HTTP.Url->New("wss://echo.websocket.org"));
ws->Connect();
ws->GetStatusCode()->PrintLine(); # 101GetStatusMessage #
Gets the last status message
method : public : GetStatusMessage() ~ StringReturn
| Type | Description |
|---|---|
| String | last status message |
Example
ws := Web.HTTP.WebSocket->New(Web.HTTP.Url->New("wss://echo.websocket.org"));
if(<>ws->Connect()) {
ws->GetStatusMessage()->PrintLine(); # error message
};GetSubject #
Gets the socket's X.509 certificate subject name
method : public : GetSubject() ~ StringReturn
| Type | Description |
|---|---|
| String | certificate subject name |
New # constructor
Constructor
New(url:Web.HTTP.Url, cookies_enabled:Bool)Parameters
| Name | Type | Description |
|---|---|---|
| url | Url | URL |
| cookies_enabled | Bool | true if cookies are enabled, false otherwise |
PingSocket #
Pings the websocket server
method : public : PingSocket() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if pinged, false otherwise |
Example
ws := Web.HTTP.WebSocket->New(Web.HTTP.Url->New("wss://echo.websocket.org"));
if(ws->Connect()) {
if(ws->PingSocket()) {
"Server alive"->PrintLine();
};
ws->CloseSocket();
};ReadSocketBinary #
Reads binary input from the web socket
method : public : ReadSocketBinary() ~ Byte[]Return
| Type | Description |
|---|---|
| Byte | binary input read, Nil otherwise |
Example
ws := Web.HTTP.WebSocket->New(Web.HTTP.Url->New("wss://binary.example.com"));
if(ws->Connect()) {
data := ws->ReadSocketBinary();
data->Size()->PrintLine();
ws->CloseSocket();
};ReadSocketText #
Reads text from the web socket
method : public : ReadSocketText() ~ StringReturn
| Type | Description |
|---|---|
| String | text read, Nil otherwise |
Example
ws := Web.HTTP.WebSocket->New(Web.HTTP.Url->New("wss://echo.websocket.org"));
if(ws->Connect()) {
ws->WriteSocket("Hello");
msg := ws->ReadSocketText();
msg->PrintLine();
ws->CloseSocket();
};RemoveHeader #
Removes a HTTPS request header
method : public : RemoveHeader(name:String) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| name | String | header name |
WriteSocket #
Writes binary output to the web socket
method : public : WriteSocket(data:Byte[], fragment:Bool) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| data | Byte | binary payload |
| fragment | Bool | true if fragmented, false otherwise |
Return
| Type | Description |
|---|---|
| Bool | true if successful, false otherwise |
Example
ws := Web.HTTP.WebSocket->New(Web.HTTP.Url->New("wss://echo.websocket.org"));
if(ws->Connect()) {
ws->WriteSocket("ping"->ToByteArray(), false);
ws->CloseSocket();
};WriteSocket #
Writes text to the web socket
method : public : WriteSocket(message:String, fragment:Bool) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| message | String | message |
| fragment | Bool | true if fragmented, false otherwise |
Return
| Type | Description |
|---|---|
| Bool | true if successful, false otherwise |
Example
ws := Web.HTTP.WebSocket->New(Web.HTTP.Url->New("wss://echo.websocket.org"));
if(ws->Connect()) {
ws->WriteSocket("Hello, WebSocket!");
ws->ReadSocketText()->PrintLine();
ws->CloseSocket();
};