mirror of
https://github.com/gmod-integration/lua.git
synced 2025-03-15 21:17:34 +00:00
remake networking
This commit is contained in:
parent
91e4029aed
commit
f5a707eea4
32
lua/gmod_integration/client/cl_net.lua
Normal file
32
lua/gmod_integration/client/cl_net.lua
Normal file
|
@ -0,0 +1,32 @@
|
|||
//
|
||||
// Network
|
||||
//
|
||||
|
||||
/*
|
||||
Upload
|
||||
0 - Say I'm ready
|
||||
Receive
|
||||
1 - Sync Chat
|
||||
*/
|
||||
|
||||
// Send
|
||||
function gmInte.SendNet(id, args, func)
|
||||
net.Start("gmIntegration")
|
||||
net.WriteUInt(id, 8)
|
||||
net.WriteString(util.TableToJSON(args || {}))
|
||||
func && func()
|
||||
net.SendToServer()
|
||||
end
|
||||
|
||||
// Receive
|
||||
local netFunc = {
|
||||
[1] = function(args)
|
||||
chat.AddText(unpack(args))
|
||||
end
|
||||
}
|
||||
|
||||
net.Receive("gmIntegration", function()
|
||||
local id = net.ReadUInt(8)
|
||||
local args = util.JSONToTable(net.ReadString())
|
||||
netFunc[id] && netFunc[id](args)
|
||||
end)
|
|
@ -2,8 +2,25 @@
|
|||
// Network
|
||||
//
|
||||
|
||||
/*
|
||||
Upload
|
||||
1 - Add Chat Message
|
||||
Receive
|
||||
0 - Player is Ready
|
||||
*/
|
||||
|
||||
util.AddNetworkString("gmIntegration")
|
||||
|
||||
// Send
|
||||
function gmInte.SendNet(id, data, ply, func)
|
||||
net.Start("gmIntegration")
|
||||
net.WriteUInt(id, 8)
|
||||
net.WriteString(util.TableToJSON(data))
|
||||
func && func()
|
||||
net.Send(ply)
|
||||
end
|
||||
|
||||
// Receive
|
||||
local netFuncs = {
|
||||
[0] = function(ply)
|
||||
gmInte.userFinishConnect(ply)
|
||||
|
@ -14,9 +31,5 @@ net.Receive("gmIntegration", function(len, ply)
|
|||
if !ply:IsPlayer() then return end
|
||||
local id = net.ReadUInt(8)
|
||||
local data = util.JSONToTable(net.ReadString() || "{}")
|
||||
|
||||
// check if argument is valid
|
||||
if netFuncs[id] then
|
||||
netFuncs[id](ply, data)
|
||||
end
|
||||
netFuncs[id] && netFuncs[id](ply, data)
|
||||
end)
|
Loading…
Reference in New Issue
Block a user