-
Notifications
You must be signed in to change notification settings - Fork 1
Basic
Options is a variable you can create for handler settings. May not be declared.
It is declared like this:
options = {
Address = "/websocket", -- The path for the user.
WebSocket = true, -- Is it WebSocket?
}Now more about each value.
Address - needed to determine the path along which the handler is called. No ad required. According to the standard, it has values relative to the web folder (without specifying the folder itself in the path). Required Only has a String data type.
WebSocket - needed to define a WebSocket handler. Doesn't require an announcement. The default is false. Must have only the Bool data type.
It is always required to have a Handler function in it. The function, in turn, must take 1 parameter - request (the name can be anything). The function should look like this:
function Handler (request)
request.write("Hello world!")
endThe function is always called on any HTTP request. The function name should always be "Handler".
Here it will be about the parameter passed to the Handler function. The parameter has the following functionality:
request.method -- Used to determine the type of HTTP request
request.write("") -- Used to send a response to the client.
request.getQuery("") -- Used to get data from Query parameters.
request.getHeader("") -- Used to get data from HTTP headers.
request.getFormData({""}) -- Used to get data from HTTP POST requests.Created to determine the type of HTTP request.
Usage:
if request.method == "POST" then
postData = request.getFormData({"message"})
request.write(postData.message)
endUsed to send a response to the client. It takes only a string as a parameter.
Usage:
request.send("Hello world!")Used only to get Query parameters passed by the client. It takes only the parameter key as a string as an argument. The function returns only a string.
Usage: localhost:8080/?index=1234
index = request.getQuery("index")
request.write(query) -- The user will be shown "1234"Works on the principle of request.getQuery(). But only used to get headers.
Usage:
token = req.getHeader("Token")
if token == "1234" then
req.write("The token is valid.")
endUsed to get the data provided in the POST request. It takes as an argument only the table containing the keys of the values you want to get. Returns a table containing the key values.
Usage:
postData = req.getFormData({"message", "user"}) -- Getting message values
req.write(postData.message) -- Return message values
req.write(postData.user) -- Return user valuesBlomma has native support for WebSocket. The WebSocket handler is almost the same as a regular HTTP handler, i.e. by creating a .lua file in the web folder. In the handler file, we create options and set WebSocket = true.
options = {
WebSocket = true,
}Instead of creating a Handler function like in an HTTP handler, we create a WSHandler (short for WebSocket Handler).
function WSHandler(conn)
while true do
data = conn.read()
conn.write(data.mt, data.data)
end
endThis function (WSHandler) will be called each time a connection is created. Also, when creating a connection, the WebSocket handler file will be loaded into a new state of the Lua virtual machine. Those. in a handler websocket file, you are always interacting with only 1 connection. To interact connections with each other, use valueController.
The WSHandler function always takes 1 parameter, which accepts client connections.
Also in the handler there should be a function to close the processing of closing the connection. This function always takes 1 parameter to which data about closing is passed. It looks like this:
function onClose(data)
endExplore BlommaChat (If there is nothing there, then don't worry, I will describe how it works and a lesson on how to repeat it soon)
Writing an Echo server with Blomma is very easy. The whole code might look like this:
options = {
WebSocket = true,
}
function WSHandler(conn)
while true do
local msg = conn.read()
conn.write(msg.mt, msg.data)
end
end
function onClose()
endLooks very simple, right? But I recommend adding a check to see if the connection is open, this will save us from unnecessary bugs and errors during work. So our code now looks like this:
options = {
WebSocket = true,
}
local connectionIsOpen = false
function WSHandler(conn)
connectionIsOpen = true
while true do
local msg = conn.read()
if connectionIsOpen ~= true then
return
end
conn.write(msg.mt, msg.data)
end
end
function onClose()
connectionIsOpen = false
endThe client connection is passed as an argument to the WebSocket handler function. Usually the parameter is named conn
function WSHandler(conn)
endA function to send data to the client. The first argument it takes is the message type, which must always be an INT type, the second argument is the data to be sent, the data type of which must always be a string.
Usage:
conn.write(msg.mt, "Hello world!")Function for reading data from the client. Always returns a table with the read information.
Usage:
msg = conn.read()
conn.write(msg.mt, msg.data)Contains the type of message read
Contains read message data
Closes current connections.
Usage:
conn.close()function onClose(data)
endCalled if the connection has been closed, passes a table with information about the message as a parameter. The table is identical to the table returned by conn.read()