Skip to content

This is a general-purpose network communication framework for Roblox game development, designed to simplify the use of RemoteEvents.

License

Notifications You must be signed in to change notification settings

ls9512/Roblox_ClientServerFrame

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

996.icu LICENSE

Roblox_ClientServerFrame

This is a framework designed to encapsulate Roblox RemoteEvent functionality, providing a universal network communication interface to significantly simplify and standardize the network communication development process. It helps avoid the frequent creation and management of numerous RemoteEvents. Through a few automatically maintained events and a standardized communication message structure, it automatically handles request parsing, invocation, and response processing. This framework has already been applied in several real-world projects.

File

NetClient.lua

The core client script. It initializes client-side network communication and is responsible for sending requests to the server and receiving returned results. All such communications must be done via this script.

NetUtil.lua

A utility class that handles general-purpose tasks such as event creation and error handling. Usually, it does not need to be manually invoked.

NetServer.lua

The core server script. It receives requests from clients and dispatches them to specific modules, processes the return results, and sends them back to the clients. It can also be used to actively send messages to specific clients or all players.

How to use

Usage Guidelines

Client-to-server request code must be called through LocalScript, while server-side logic must be invoked through Script. There are two types of network requests: Request and Broadcast. The routing structure follows a Module / Action / Param pattern, where functionality is grouped by Module, each containing multiple Actions (request handlers), each of which can accept custom Params. More details and examples are provided below.

Request

Sent proactively from the client to the server. The server parses the request route and executes specific logic or queries data, optionally returning a result. In server scripts under ServerStorage/Net/Request, create scripts named after the module, for example, Game.lua for managing core game logic — in this case, the module name is Game. Inside Game.lua, define request-handling functions where the function name serves as the Action name, and the parameters must be in the form (player, param) — representing the request sender and parameters. Both parameters and return values are optional, depending on the specific use case.

Broadcast

Proactively sent from the server to the client when needed. The client parses and executes the corresponding logic. These should be placed in ReplicatedStorage/Net/Broadcast, and named consistently with the corresponding Request modules.

Sample

Client Script

Init Client

-- Call NetClient:Init in StarterPlayer/StarterPlayerScripts
local NetClient = require(game.ReplicatedStorage.Script.Net.NetClient)
NetClient:Init()

Request without return value async

function NetClientDemo:RequestTest1()
	NetClient:Request("ServerRequestModuleDemo", "OnRequest", { Message = "Send message to server"})
end

Request with return value async

function NetClientDemo:RequestTest2()
	NetClient:Request("ServerRequestModuleDemo", "OnRequest", { Message = "Send message to server"}, function(result)
		print(result)
	end)
end

Request without param and return value async

function NetClientDemo:RequestTest3()
	NetClient:Request("ServerRequestModuleDemo", "OnRequest")
end

Request and wait for return value

function NetClientDemo:RequestTest4()
	local result = NetClient:RequestWait("ServerRequestModuleDemo", "OnRequest")
	print(result)
end

Request many action in once

function NetClientDemo:RequestTest5()
	NetClient:RequestQueue({
		{
			Module = "ServerRequestModuleDemo",
			Action = "OnRequest", 
			Param = { 
				Message = "Message1"
			}
		},
		{
			Module = "ServerRequestModuleDemo",
			Action = "OnRequest", 
			Param = { 
				Message = "Message2"
			}
		}
	}, function(result)
		local result1 = result[1]
		print(result1)
		
		local result2 = result[2]
		print(result2)
	end)
end

Client Broadcast Process

local ClientBroadcastModuleDemo = {}

function ClientBroadcastModuleDemo:OnBroadcast(player, param)
	local message = param.Message
	print(message)
end

return ClientBroadcastModuleDemo

Server Script

Init Server

-- Call NetServer:Init in ServerStorage
local NetServer = require(game.ServerStorage.Net.NetServer)
NetServer:Init()

Server Request Process

local ServerRequestModuleDemo = {}

function ServerRequestModuleDemo:OnRequest(player, param)
	if param ~= nil then
		local message = param.Message
		print(message)
	else
		print("Request without message")
	end
	
	return true
end

return ServerRequestModuleDemo

Boradcast to one player

function NetServerDemo:BroadcastTest1()
	local player = game.Players:GetPlayers()[1]
	NetServer:Broadcast("ClientBroadcastModuleDemo", "OnBroadcast", { Message = "Broadcast to client message"})
end

Broadcast to all player

function NetServerDemo:BroadcastTest2()
	NetServer:BroadcastAll("ClientBroadcastModuleDemo", "OnBroadcast", { Message = "Broadcast to client message"})
end

About

This is a general-purpose network communication framework for Roblox game development, designed to simplify the use of RemoteEvents.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages