KnitServer
Knit server-side lets developers create services and expose methods and signals to the clients.
local Knit = require(somewhere.Knit)
-- Load service modules within some folder:
Knit.AddServices(somewhere.Services)
-- Start Knit:
Knit.Start():andThen(function()
print("Knit started")
end):catch(warn)
Types
Middleware
ServerMiddlewareFn
For more info, see ServerComm documentation.
ServerMiddleware
An array of server middleware functions.
ServiceDef
Used to define a service when creating it in CreateService
.
The middleware tables provided will be used instead of the Knit-level
middleware (if any). This allows fine-tuning each service's middleware.
These can also be left out or nil
to not include middleware.
Service
ServiceClient
KnitOptions
- Middleware will apply to all services except ones that define their own middleware.
Properties
Util
This item is read only and cannot be modified. Read OnlyKnitServer.Util:
Folder
References the Util folder. Should only be accessed when using Knit as a standalone module. If using Knit from Wally, modules should just be pulled in via Wally instead of relying on Knit's Util folder, as this folder only contains what is necessary for Knit to run in Wally mode.
Functions
CreateService
Constructs a new service.
caution
Services must be created before calling Knit.Start()
.
-- Create a service
local MyService = Knit.CreateService {
Name = "MyService",
Client = {},
}
-- Expose a ToAllCaps remote function to the clients
function MyService.Client:ToAllCaps(player, msg)
return msg:upper()
end
-- Knit will call KnitStart after all services have been initialized
function MyService:KnitStart()
print("MyService started")
end
-- Knit will call KnitInit when Knit is first started
function MyService:KnitInit()
print("MyService initialize")
end
AddServices
Requires all the modules that are children of the given parent. This is an easy way to quickly load all services that might be in a folder.
Knit.AddServices(somewhere.Services)
AddServicesDeep
Requires all the modules that are descendants of the given parent.
GetService
Gets the service by name. Throws an error if the service is not found.
GetServices
Gets a table of all services.
CreateSignal
KnitServer.
CreateSignal
(
) →
SIGNAL_MARKER
Returns a marker that will transform the current key into a RemoteSignal once the service is created. Should only be called within the Client table of a service.
See RemoteSignal documentation for more info.
local MyService = Knit.CreateService {
Name = "MyService",
Client = {
-- Create the signal marker, which will turn into a
-- RemoteSignal when Knit.Start() is called:
MySignal = Knit.CreateSignal(),
},
}
function MyService:KnitInit()
-- Connect to the signal:
self.Client.MySignal:Connect(function(player, ...) end)
end
CreateUnreliableSignal
KnitServer.
CreateUnreliableSignal
(
) →
UNRELIABLE_SIGNAL_MARKER
Returns a marker that will transform the current key into an unreliable RemoteSignal once the service is created. Should only be called within the Client table of a service.
See RemoteSignal documentation for more info.
Unreliable Events
Internally, this uses UnreliableRemoteEvents, which allows for network communication that is unreliable and unordered. This is useful for events that are not crucial for gameplay, since the delivery of the events may occur out of order or not at all.
See the documentation for UnreliableRemoteEvents for more info.
CreateProperty
KnitServer.
CreateProperty
(
initialValue:
any
) →
PROPERTY_MARKER
Returns a marker that will transform the current key into a RemoteProperty once the service is created. Should only be called within the Client table of a service. An initial value can be passed along as well.
RemoteProperties are great for replicating data to all of the clients. Different data can also be set per client.
See RemoteProperty documentation for more info.
local MyService = Knit.CreateService {
Name = "MyService",
Client = {
-- Create the property marker, which will turn into a
-- RemoteProperty when Knit.Start() is called:
MyProperty = Knit.CreateProperty("HelloWorld"),
},
}
function MyService:KnitInit()
-- Change the value of the property:
self.Client.MyProperty:Set("HelloWorldAgain")
end
Start
Starts Knit. Should only be called once.
Optionally, KnitOptions
can be passed in order to set
Knit's custom configurations.
caution
Be sure that all services have been created before
calling Start
. Services cannot be added later.
Knit.Start():andThen(function()
print("Knit started!")
end):catch(warn)
Example of Knit started with options:
Knit.Start({
Middleware = {
Inbound = {
function(player, args)
print("Player is giving following args to server:", args)
return true
end
},
},
}):andThen(function()
print("Knit started!")
end):catch(warn)
OnStart
KnitServer.
OnStart
(
) →
Promise
Returns a promise that is resolved once Knit has started. This is useful
for any code that needs to tie into Knit services but is not the script
that called Start
.
Knit.OnStart():andThen(function()
local MyService = Knit.Services.MyService
MyService:DoSomething()
end):catch(warn)