-
Notifications
You must be signed in to change notification settings - Fork 1
melon.net
melon.net
Network handlers and abstractions
melon

melon.net.AwaitNotify(id: string, on: function)
Argument and Return information
| # | Name | Type | Description |
|---|---|---|---|
| 1 | id | string | The identifier of the notification |
| 2 | on | function | The callback, called with the status and data passed from the server |
Awaits a notification from the server
local loading = true
melon.net.AwaitNotify("create_file", function(status)
print("Status: ", status)
loading = false
end )
net.Start("create_file")
net.WriteString("some.txt")
net.SendToServer()

melon.net.ClearAwait(id: string)
Argument and Return information
| # | Name | Type | Description |
|---|---|---|---|
| 1 | id | string | The identifier of the notification |
Removes an await notification callback

melon.net.Notify(ply: Player, id: string, type: melon.net.NOTIFY_, data: any)
Argument and Return information
| # | Name | Type | Description |
|---|---|---|---|
| 1 | ply | Player | The player to notify |
| 2 | id | string | The string ID of the notification for callbacks on the client |
| 3 | type | melon.net.NOTIFY_ | The notification type |
| 4 | data | any | Data to send to the client callback, uses net.WriteType so be careful |
net.Receive("create_file", function(_, ply)
local name = net.ReadString()
local f = file.Open(name, "w", "DATA")
if not f then
return melon.net.Notify(ply, "create_file", melon.net.NOTIFY_FAILURE)
end
f:Write("created!")
f:Close()
melon.net.Notify(ply, "create_file", melon.net.NOTIFY_SUCCESS)
end )

melon.net.Schema(identifier: string) -> melon.net.SchemaObj
Argument and Return information
| # | Name | Type | Description |
|---|---|---|---|
| 1 | identifier | string | String identifier for the schema |
| # | Name | Type | Description |
|---|---|---|---|
| 1 | obj | melon.net.SchemaObj | The schema object |
Creates a new schema object
---- Creates and registers the schema
local schema = melon.net.Schema("unique_name")
--- Contains a string
:Value ("SomeString", melon.net.TYPE_STRING)
---- And another schema
:Schema("SomeSchema", "some_other_schema")
--- Called when the schema message gets recieved
function schema:Recv(sender)
print(self.SomeString) -- "hi!"
print(self.SomeSchema.TestString) -- "hello!"
print(self.SomeSchema.TestInteger) -- nil
end
--- Registers another schema
melon.net.Schema("some_other_schema")
--- Contains another string
:Value("TestString", melon.net.TYPE_STRING)
--- Contains an optional integer
:Value("TestInteger", melon.net.TYPE_INTEGER, true)
--- Due to this being used as a sub-schema, it does not need a recv assigned to it
schema:Send({
SomeString = "hi!",
SomeSchema = {
TestString = "hello!",
--- Omitting TestInteger
}
}, player) --- Sending to `player`, if youre sending to the server from the client this can be omitted

melon.net.SchemaFromTable(name: string, tbl: table, done: table) -> melon.net.SchemaObj
Argument and Return information
| # | Name | Type | Description |
|---|---|---|---|
| 1 | name | string | Name of the schema to be registered as |
| 2 | tbl | table | Table to make a schema out of |
| 3 | done | table | Table of already created schemas to avoid infinite recursion |
| # | Name | Type | Description |
|---|---|---|---|
| 1 | obj | melon.net.SchemaObj | The schema object |

melon.net.Unwatch(msg: string, name: string)
Argument and Return information
| # | Name | Type | Description |
|---|---|---|---|
| 1 | msg | string | Message name added with [util.AddNetworkString] |
| 2 | name | string | Identifier of the watcher to be removed |
Unwatches a network message added with melon.net.Watch
melon.net.Unwatch("ping_net_name", "Other Identifier")

melon.net.Watch(msg: string, name: string, callback: fn)
Argument and Return information
| # | Name | Type | Description |
|---|---|---|---|
| 1 | msg | string | Message name added with [util.AddNetworkString] to watch |
| 2 | name | string | Identifier for the watcher |
| 3 | callback | fn | Function callback for whenever the listener recieves an input |
util.AddNetworkString("ping_net_name")
melon.net.Watch("ping_net_name", "Identifier", function(len, ply)
net.Start("ping_net_name")
net.WriteString("Pong :)")
net.Send(ply)
end )
melon.net.Watch("ping_net_name", "Other Identifier", function(len, ply)
print("Ponged from Identifier :)")
end ) Net Schema Object
All of these values have Set*/Get* methods for them. They can also be indexed by their name, object.*
| Name | Type | Comment |
|---|---|---|
| Identifier | string | String identifier for this message |
Internal initialization for a Schema Object

melon.net.SchemaObj:RecvOn(on: melon.net.RECV_ON_) -> self
Argument and Return information
| # | Name | Type | Description |
|---|---|---|---|
| 1 | on | melon.net.RECV_ON_ | Where to allow recieving from |
| # | Name | Type | Description |
|---|---|---|---|
| 1 | self | self | The SchemaObj |

melon.net.SchemaObj:Array(name: string, type: melon.net.TYPE_, optional?: bool) -> self
Argument and Return information
| # | Name | Type | Description |
|---|---|---|---|
| 1 | name | string | Keyname of the array |
| 2 | type | melon.net.TYPE_ | Type enum of the data being sent |
| 3 | optional? | bool | Is this array optional |
| # | Name | Type | Description |
|---|---|---|---|
| 1 | self | self | The SchemaObj |

melon.net.SchemaObj:Recv(data: table, sender: Player, obj: melon.net.SchemaObj)
Argument and Return information
| # | Name | Type | Description |
|---|---|---|---|
| 1 | data | table | The data recieved |
| 2 | sender | Player | If on the server then this is the player that sent the message |
| 3 | obj | melon.net.SchemaObj | The SchemaObj |

melon.net.SchemaObj:Send(tbl: table, to: table<Player>) -> bool?
Argument and Return information
| # | Name | Type | Description |
|---|---|---|---|
| 1 | tbl | table | Table to write |
| 2 | to | table<Player> | A table of players or a player, accepts what net.Send does, doesn't matter on the client since it uses net.SendToServer. |
| # | Name | Type | Description |
|---|---|---|---|
| 1 | success | bool | Was the write successful |

melon.net.SchemaObj:Read() -> table
Reads the schema table from the net message

melon.net.SchemaObj:WriteValue()

melon.net.SchemaObj:Validate(args: table) -> bool?, number, any
Argument and Return information
| # | Name | Type | Description |
|---|---|---|---|
| 1 | args | table | Table to validate if it meets the schema or not |
| # | Name | Type | Description |
|---|---|---|---|
| 1 | does | bool | Does this table match the schema |
| 2 | key | number | If not valid, key of the bad value |
| 3 | value | any | If not valid, the invalid schema name |
Is the argument table provided valid to this schema

melon.net.SchemaObj:Value(name: string, type: melon.net.TYPE_, optional?: bool) -> self
Argument and Return information
| # | Name | Type | Description |
|---|---|---|---|
| 1 | name | string | Keyname of the value |
| 2 | type | melon.net.TYPE_ | Type of the value |
| 3 | optional? | bool | Is this value optional |
| # | Name | Type | Description |
|---|---|---|---|
| 1 | self | self | The SchemaObj |
Adds a value row to the schema

melon.net.SchemaObj:ReadValue()

melon.net.SchemaObj:ValidateValue()

melon.net.SchemaObj:Schema(name: string, identifier: string, optional?: bool) -> self
Argument and Return information
| # | Name | Type | Description |
|---|---|---|---|
| 1 | name | string | Keyname of the schema |
| 2 | identifier | string | Identifier for the schema thats registered in melon.net.schemas |
| 3 | optional? | bool | Is this schema optional |
| # | Name | Type | Description |
|---|---|---|---|
| 1 | self | self | The SchemaObj |
Adds a schema row to the schema, used to send nested schemas
Describes the type of notification for melon.net.Notify
| Name | Description |
|---|---|
| NOTIFY_OTHER | Custom notification |
| NOTIFY_SUCCESS | Successful response |
| NOTIFY_WARNING | Warning response |
| NOTIFY_FAILURE | Failure response |
| NOTIFY_INSUFFICIENT_PERMISSIONS | Insufficient permissions, duh |
| Name | Description |
|---|---|
| RECV_ON_CLIENT | Recieve on the client |
| RECV_ON_SERVER | Recieve on the server |
| RECV_ON_SHARED | Recieve from both |