Network
ℹ️
Page is being worked on.
Functions
Functions
serve
net.serve(options: ServeOptions): (boolean, string | ServerHandle)
Starts a server on the specified port.
Parameters
options:
ServeOptions?
- The options for the server.
Returns
boolean
- Whether the server started successfully.string
|ServerHandle
- The error message, or the server handle.
request
net.request(host: string, options: RequestOptions?): (boolean, string | Response)
Makes a request to the specified host.
Optional options, defaults to GET request.
TLS is supported.
Parameters
host: string
- The host to make the request to.options:
RequestOptions?
- The options for the request.
websocket
net.websocket(host: string, protocols: {string}?): (boolean, string | ClientWebSocket)
Creates a websocket client.
TLS is supported.
Parameters
host: string
- The host to connect to.protocols: {string}?
- The protocols to use.
Returns
boolean
- Whether the connection was successful.string
|ClientWebSocket
- The error message, or the websocket handle.
Types
ServerRequest
export type ServerRequest = {
method : string,
path : string,
headers : {[string] : string},
body : string,
}
ServerResponse
export type ServerResponse = {
statusCode : number,
headers : {[string] : string},
body : buffer | string,
}
WebSocket
export type WebSocket = userdata<{
--[[
Whether the websocket is connected.
]]
connected : boolean,
--[[
Sends a message to the websocket.
]]
send : (self: WebSocket, message: string) -> (),
--[[
Closes the websocket.
]]
close : (self: WebSocket) -> (),
}>
ClientWebSocket
export type ClientWebSocket = WebSocket & userdata<{
--[[
Binds a function to the open event.
*Only one function can be bound at a time*.
]]
bindOpen : (self: ClientWebSocket, func: () -> ()) -> (),
--[[
Binds a function to the message event.
*Only one function can be bound at a time*.
]]
bindMessage : (self: ClientWebSocket, func: (message: string) -> ()) -> (),
--[[
Binds a function to the close event.
*Only one function can be bound at a time*.
]]
bindClose : (self: ClientWebSocket, func: () -> ()) -> (),
}>
ServerWebSocketHandlers
export type ServerWebSocketHandlers = {
--[[
Function to check if the request should be upgraded to a websocket.
Should return true if the request should be upgraded.
@return boolean
]]
upgrade : ((request: ServerRequest) -> boolean)?,
--[[
A function event for when a websocket is opened.
*`WebSocket` class will keep the same reference per connection*.
]]
open : ((websocket: WebSocket) -> ())?,
--[[
A function event for when a websocket receives a message.
*`WebSocket` class will keep the same reference per connection*.
]]
message : ((websocket: WebSocket, message: string) -> ())?,
--[[
A function event for when a websocket is closed.
*`WebSocket` class will keep the same reference per connection*.
]]
close : ((websocket: WebSocket) -> ())?,
};
ServeOptions
export type ServeOptions = {
--[[
The port to bind to.
]]
port : number,
--[[
The address to bind to.
- Default: `0.0.0.0`
]]
address : string?,
--[[
Whether to reuse the address.
- Useful for multi-threaded servers.
Default: `false`
]]
reuseAddress : boolean?,
--[[
The function to handle requests.
]]
request : (request: ServerRequest) -> string | buffer | ServerResponse,
--[[
Functions to handle websockets.
]]
webhook : ServerWebSocketHandlers?,
}
BareRequestOptions
type BareRequestOptions = {
method : "GET",
headers : {[string] : string}?,
--[[
Whether to follow redirects.
- Default: `true`
]]
allowRedirects : boolean?,
}
BodyRequestOptions
type BodyRequestOptions = {
method : "POST" | "PUT" | "DELETE",
headers : {[string] : string}?,
body : string,
}
RequestOptions
export type RequestOptions = BareRequestOptions & BodyRequestOptions
ServerHandle
export type ServerHandle = userdata<{
--[[
Closes the server.
]]
close : (self: ServerHandle) -> (),
}>
Response
export type Response = {
ok : boolean,
statusCode : number,
statusReason : string,
headers : {[string] : string},
body : string,
}