router

Allows for communication across networks via Socket IO.
Essentially allowing you to communicate to whatever instances you have open from a master controller.
Its essentially just a chat-room where you manage the clients.


Example

-- connect to backend hosted on your own network.
local handle = router.spawn("http://127.0.0.1:8000")

function handle:open()
    print("Connected!")

    -- list all clients active on the network
    for k, v in pairs(self.clients) do
        print(k, v)
    end

    -- destroy after x amount of seconds
    timer.Simple(5, function()
        handle:destruct()
    end)
end

function handle:error(...)
    print("Error: ", ...)
end

function handle:close(...)
    print("Close: ", ...)
end

function handle:operator(from, op, data)
    -- We received a message from someone
    print("Operator: ", from, op, data)
end

-- connect to network
handle:connect()

Functions

router.spawn(address: string, headers?: {[index: string]: string}): router.handle

router.spawn(address: string, headers?: {[index: string]: string}): router.handle
  • Creates a new router handle.
  • By providing a "unique" it will maintain that as it's unique identifier when connecting.
  • Keep in mind these "unique" values must always be "unique" for connection stability.

Handle

handle:connect()

handle:connect()
  • Attempts to connect to the router network.

handle:disconnect()

handle:disconnect()
  • Disconnects from the router network.

handle:destruct()

handle:destruct()
  • Disconnects & destroys any active instances of Socket IO.

handle:is_connected(): boolean

handle:is_connected(): boolean
  • If the router client is currently connected to a network.

handle:broadcast(op: string, t: {[index: string | number]: any})

handle:broadcast(op: string, t: {[index: string | number]: any})
  • Sends an operation to all clients on the network.

handle:omit(clients: string[] | router.client[], op: string, t: {[index: string | number]: any})

handle:omit(clients: string[] | router.client[], op: string, t: {[index: string | number]: any})
  • Sends a message to all clients on the network, except for those listed.
  • You can supply either the unique IDs or the clients themselves.

handle:at(clients: string[] | router.client[], op: string, t: {[index: string | number]: any})

handle:at(clients: string[] | router.client[], op: string, t: {[index: string | number]: any})
  • Sends a message to all clients on the network, only for those listed.
  • You can supply either the unique IDs or the clients themselves.

handle:datastore(key: string, value: string)

handle:datastore(key: string, value: string)
  • Sends & stores a key and value on the client for all clients to view.
  • On other clients they can access it via client.datastore

handle.clients: router.client[]

handle.clients: router.client[]
  • A list of connected clients on the network.

These are callbacks you can override on the class itself.

handle:open()

handle:open()
  • Called when a connection has been made.

handle:close(code: number, reason: string)

handle:close(code: number, reason: string)
  • Called when a connection has been closed.

handle:error(code: number, reason: string)

handle:error(code: number, reason: string)
  • Called when an internal error from Socket IO occures.

handle:connected(client: router.client)

handle:connected(client: router.client)
  • Called when another router client has connected to the network.

handle:disconnected(client: router.client)

handle:disconnected(client: router.client)
  • Called when another router client has disconnected from the network.

handle:message(payload: string, is_binary: boolean)

handle:message(payload: string, is_binary: boolean)
  • Called when a message has been received.
  • Do note this is for internal use and debugging only.

handle:operator(from: router.client, op: string, data: {[index: string | number]: any})

handle:operator(from: router.client, op: string, data: {[index: string | number]: any})
  • Called when an operation has been received from send, broadcast, omit or at

Client

The client themselves that you see from your router's perspective.
You can target these clients and send messages to them.

client:fullname(): string

client:fullname(): string
  • Retrieves the full name of the client (includes name, unique and ip)

client:uniquename(): string

client:uniquename(): string
  • Retrieves the unique name of the client (inlcudes unique and ip)

client:alive(): boolean

client:alive(): boolean
  • Returns if the client is currently connected and on your list of clients

client:send(): boolean

client:send(op: string, t: {[index: string | number]: any})
  • Sends an operation to this specific client on the network.

client.name: string

client.name: string
  • The name the client associates by.
  • This is not to be made use of as an identifier.

client.game: string

client.game: string
  • The game the client associates by.
  • This is not to be made use of as an identifier.

client.unique: string

client.unique: string
  • The unique ID the client associates by.
  • This is used internally for targeting.

client.unique: string

client.ip: string
  • The IP address the client associates by.
  • This is not to be made use of as an identifier.

client.datastore: {[index: string]: string}

client.datastore: {[index: string]: string}
  • A list of persistant data that exists per-client.

client.router: router.handle

client.router: router.handle
  • The associated router handler.