[Module] Range's NetworkManager

I wrote my own net manager for easy data exchange between the client and the server, including the generation of HMAC for verifying client data.

The module creates an object of the NetworkManager class, which creates and defines Remotes for the server and client

Server API:

Code
-- Create a new network for server/client replication ๐Ÿ‘
-- Types: 1 - event, 2 - function, 3 - bindable, 4 - event + function
local MyNewNet = NetworkManager.new("MyNewNet", 4)
MyNewNet.Ratelimit = 2
-- Modes:
-- - Protected		| add a secure code to remotes โœ… (more bytes)
-- - Fast (Server)	| make event unreliable ๐Ÿš€ (may cause loosing events)
MyNewNet:SetMode("Fast")
MyNewNet:SetMode("Protected")

MyNewNet:BindToEvent(function(Player: Player, ...)
	MyNewNet:Fire(Player, `Hello, {Player.DisplayName}!`)
end, "SomeEvent")

MyNewNet:BindToFunction(function(Player: Player, ...)
	return `Hello, {Player.DisplayName}!`
end)

-- MyNewNet:UnBindFromEvent("SomeEvent")
-- MyNewNet:UnBindFromFunction()

MyNewNet:Fire(Player1, ...)
MyNewNet:FireAll(...)
MyNewNet:FireClients({Player1, Player2, ...}, ...)
MyNewNet:FireClientsExcept(Player1, ...)
MyNewNet:FireClientsExcept({Player1, Player2, ...}, ...)
MyNewNet:FireClientsInRadius(25, Vector3.zero, ...)

Client API:

Code
local MyNewNet = NetworkManager.new("MyNewNet", 4)
MyNewNet:BindToEvent(function(...) print(...) end)
MyNewNet:Fire("Hello world!")
print(MyNewNet:Invoke("Hello world!"))

:rocket: FILE NetworkManager.rbxm (40,0 ะšะ‘)
:warning: NET NAME AND TYPE MUST BE SAME ON SERVER AND CLIENT!

10 Likes

seems neato great work!!! :hearts: :hearts: :hearts: :hearts: :hearts: :hearts:

How would i access the net objects from a different script?, without making a new net

No way, this is done for the safety of the โ€œProtectedโ€ mode. You can change the code of the module so that it saves instances to the table instead of โ€œtrueโ€ and returns it when creating it, if there is this instance in the table.

function NetworkManager.new(Name: string, Type: string?): NetworkManager
	Type = NetworkType[Type] or NetworkType.Event

	repeat task.wait() until ReplicatedStorage:GetAttribute("ServerId")
	local HashedName: string = HashLib.md5(Name..ReplicatedStorage:GetAttribute("ServerId")..Type):sub(27)
	
	if ExistingNetworks[HashedName] then return ExistingNetworks[HashedName] end -- changed checks
	ExistingNetworks[HashedName] = self -- changed true -> self
	
	local self = setmetatable({}, NetworkManager)
	self._type = Type
	self._hashedName = HashedName
	self._Maid = RangesMaid.new(self)
	
	self._RemoteEvent = nil
	self._RemoteFunction = nil
	self._BindableEvent = nil
	
	self._fast = nil
	self._protected = nil
	
	self.Alive = true
	
	self:_Init(Type)
 	
	return self
end

Oh, heavens :weary:

Jokes aside, the only gripe I have with this module is that theres not really any autocomplete, everything is *error-type* or any, so Iโ€™ll probably be sticking to my own module NetSignal for now.

Great stuff though, although I really do not understand how that hashing works and why it makes the signal more secure :sweat_smile: An explanation may be in line