Network
HTTP requests, binary WebSocket messages and local WebSocket servers.
- HTTP
- httpget
- game:HttpGet
- http_request
- WebSocket clients and local servers
- WebSocket.connect
- WebSocket.create
- Connection methods and state
- WebSocketConnection:Send
- WebSocketConnection:SendBinary
- WebSocketConnection:Close
- Server methods, state and signals
- WebSocketServer:Send
- WebSocketServer:SendBinary
- WebSocketServer:Shutdown
HTTP calls yield and require a yieldable coroutine. Response bodies are binary-safe strings.
HTTP
httpget
Yieldshttpget(url: string): stringhttpget sends an HTTP GET request and returns the response body.
Also available as http_get.
Parameters
urlstring- The URL of the remote endpoint.
Returns
stringThe binary-safe HTTP response body.
game:HttpGet
Yieldsgame:HttpGet(url: string): stringgame:HttpGet sends an HTTP GET request and returns the response body.
Also available as game:HttpGetAsync.
Parameters
urlstring- The URL of the remote endpoint.
Returns
stringThe binary-safe HTTP response body.
http_request
Yieldshttp_request(options: RequestOptions): HttpResponsehttp_request sends an HTTP request with the supplied method, headers, and body, then returns its response details.
Also available as request and http.request.
Parameters
optionsRequestOptions- RequestOptions containing Url and optional Method, Body, and Headers.
Returns
HttpResponseStatusCode, StatusMessage, Success, Body, Headers, and Error for the request.
Example
WebSocket clients and local servers
WebSocket, Websocket and websocket refer to the same read-only API table. Socket/server objects are read-only tables with Signal objects for events. Message data is always delivered as a binary-safe string plus an isBinary boolean. Use ws://127.0.0.1 for a server created here; create does not configure TLS.
WebSocket.connect
YieldsWebSocket.connect(url: string): WebSocketConnectionWebSocket.connect opens a connection to a WebSocket server.
Also available as WebSocket.Connect, WebSocket.new, WebSocket.New, Websocket.connect, Websocket.Connect, Websocket.new, Websocket.New, websocket.connect, websocket.Connect, websocket.new and websocket.New.
Parameters
urlstring- A WebSocket server URL beginning with ws:// or wss://.
Returns
WebSocketConnectionA connected WebSocketConnection object.
WebSocket.create
WebSocket.create(port: number, allowLocalConnection: boolean?): WebSocketServerWebSocket.create starts a WebSocket server on the selected local port.
Also available as WebSocket.Create, Websocket.create, Websocket.Create, websocket.create and websocket.Create.
Parameters
portnumber- The local port on which the server should listen.
allowLocalConnectionboolean??- Optional.Whether to listen on all local IPv4 interfaces instead of loopback only. Default
false.
Returns
WebSocketServerThe listening WebSocketServer object.
Example
Connection methods and state
Use colon calls. A send queues data for the transport; it does not wait for a peer response. Text frames require valid UTF-8. Binary frames do not perform UTF-8 validation. Both have an 8 MiB message limit and bounded outgoing queues (16 MiB). Sending after closure is a no-op after argument validation.
WebSocketConnection:Send
socket:Send(data: string | buffer, binary: boolean?): ()WebSocketConnection:Send sends a text or binary message to the connected peer.
Parameters
datastring | buffer- The binary data to process.
binaryboolean??- Optional.Whether the message should use binary framing instead of text framing. Default
false.
Returns
()This function returns no values.
WebSocketConnection:SendBinary
socket:SendBinary(data: string | buffer): ()WebSocketConnection:SendBinary sends a message as binary data.
Parameters
datastring | buffer- The binary data to process.
Returns
()This function returns no values.
WebSocketConnection:Close
socket:Close(): ()WebSocketConnection:Close closes the WebSocket connection.
Returns
()This function returns no values.
Server methods, state and signals
WebSocketServer:Send
server:Send(data: string | buffer, binary: boolean?): ()WebSocketServer:Send broadcasts a text or binary message to all connected clients.
Parameters
datastring | buffer- The binary data to process.
binaryboolean??- Optional.Whether the message should use binary framing instead of text framing. Default
false.
Returns
()This function returns no values.
WebSocketServer:SendBinary
server:SendBinary(data: string | buffer): ()WebSocketServer:SendBinary broadcasts a binary message to all connected clients.
Parameters
datastring | buffer- The binary data to process.
Returns
()This function returns no values.
WebSocketServer:Shutdown
server:Shutdown(): ()WebSocketServer:Shutdown stops the server and closes its client connections.
Returns
()This function returns no values.
Written for Lynx 0.747
Something unclear? Tell us on Discord