-
States that represent the client connecting to a server.
Fields:
- connecting
- In the process of connecting to the server.
- acknowledging_connect
- connection_pending
- connection_succeeded
-
All of the possible connection statuses for a client connection.
Fields:
- disconnected
- Disconnected from the server.
- connecting
- In the process of connecting to the server.
- acknowledging_connect
- connection_pending
- connection_succeeded
- connected
- Successfully connected to the server.
- disconnect_later
- Disconnecting, but only after sending all queued packets.
- disconnecting
- In the process of disconnecting from the server.
- acknowledging_disconnect
- zombie
- unknown
See also:
-
States that represent the client disconnecting from a server.
Fields:
- disconnect_later
- Disconnecting, but only after sending all queued packets.
- disconnecting
- In the process of disconnecting from the server.
- acknowledging_disconnect
-
Valid modes for sending messages.
Fields:
- reliable
- Message is guaranteed to arrive, and arrive in the order in which it is sent.
- unsequenced
- Message has no guarantee on the order that it arrives.
- unreliable
- Message is not guaranteed to arrive.
A Lua networking library for LÖVE games.
Tables
Class Server
Manages all clients and receives network events.
-
Enables an adaptive order-2 PPM range coder for the transmitted data of all peers.
Both the client and server must both either have compression enabled or disabled.
Note: lua-enet does not currently expose a way to disable the compression after it has been enabled.
-
Get the IP address or hostname that the server was created with.
Returns:
- string
-
Gets the Client object associated with an enet peer.
Parameters:
- peer peer
- An enet peer.
Returns:
- Client Object associated with the peer.
-
Gets the Client object that has the given connection id.
Parameters:
- number connectId
- The unique client connection id.
Returns:
-
Get the Client object that has the given peer index.
Parameters:
- index
Returns:
-
Get the number of Clients that are currently connected to the server.
Returns:
- number The number of active clients.
-
Get the table of Clients actively connected to the server.
Returns:
- {Client,...}
-
Get the last time when network events were serviced.
Returns:
- number Timestamp of the last time events were serviced.
-
Get the number of allocated channels.
Channels are zero-indexed, e.g. 16 channels allocated means that the maximum channel that can be used is 15.
Returns:
- number Number of allocated channels.
-
Get the number of allocated slots for peers.
Returns:
- number Number of allocated slots.
-
Get the timeout for packets.
Returns:
- number Time to wait for incoming packets in milliseconds. initial default is 0.
-
Get the enet_peer that has the given index.
Parameters:
- index
Returns:
- enet_peer The underlying enet peer object.
-
Get the socket address of the host.
Returns:
- string A description of the socket address, in the format "A.B.C.D:port" where A.B.C.D is the IP address of the used socket.
-
Get the total received data since the server was created.
Returns:
- number The total received data in bytes.
-
Get the total number of packets (messages) received since the server was created.
Returns:
- number The total number of received packets.
See also:
-
Get the total sent data since the server was created.
Returns:
- number The total sent data in bytes.
-
Get the total number of packets (messages) sent since the server was created.
Everytime a message is sent or received, the corresponding figure is incremented. Therefore, this is not necessarily an accurate indicator of how many packets were actually exchanged over the network.
Returns:
- number The total number of sent packets.
-
Log an event.
Alias for Server.logger:log.
Parameters:
- string event
- The type of event that happened.
- string data
- The message to log.
Usage:
if somethingBadHappened then server:log("error", "Something bad happened!") end
-
Add a callback to an event.
Parameters:
- string event
- The event that will trigger the callback.
- function callback
- The callback to be triggered.
Returns:
- function The callback that was passed in.
Usage:
server:on("connect", function(data, client) print("Client connected!") end)
-
Remove a specific callback for an event.
Parameters:
- function callback
- The callback to remove.
Returns:
- boolean Whether or not the callback was removed.
Usage:
local callback = server:on("chatMessage", function(message) print(message) end) server:removeCallback(callback)
-
Send a message to all clients.
Parameters:
- string event
- The event to trigger with this message.
- data
- The data to send.
Usage:
server:sendToAll("gameStarting", true)
-
Send a message to all clients, except one.
Useful for when the client does something locally, but other clients need to be updated at the same time. This way avoids duplicating objects by never sending its own event to itself in the first place.
Parameters:
- Client client
- The client to not receive the message.
- string event
- The event to trigger with this message.
- data
- The data to send.
-
Send a message to a single peer.
Useful to send data to a newly connected player without sending to everyone who already received it.
Parameters:
- enet_peer peer
- The enet peer to receive the message.
- string event
- The event to trigger with this message.
- data
- data to send to the peer.
Usage:
server:sendToPeer(peer, "initialGameInfo", {...})
-
Set the incoming and outgoing bandwidth limits.
Parameters:
- number incoming
- The maximum incoming bandwidth in bytes.
- number outgoing
- The maximum outgoing bandwidth in bytes.
-
Set the default send channel for all future outgoing messages.
The initial default is 0.
Parameters:
- number channel
- Channel to send data on.
-
Set the default send mode for all future outgoing messages.
The initial default is "reliable".
Parameters:
- string mode
- A valid send mode.
See also:
-
Set the maximum number of channels.
Parameters:
- number limit
- The maximum number of channels allowed. If it is 0, then the maximum number of channels available on the system will be used.
-
Set the timeout to wait for packets.
Parameters:
- number timeout
- Time to wait for incoming packets in milliseconds. The initial default is 0.
-
Set the data schema for an event.
Schemas allow you to set a specific format that the data will be sent. If the client and server both know the format ahead of time, then the table keys do not have to be sent across the network, which saves bandwidth.
Parameters:
- string event
- The event to set the data schema for.
- {string,...} schema
- The data schema.
Usage:
server = sock.newServer(...) client = sock.newClient(...) -- Without schemas client:send("update", { x = 4, y = 100, vx = -4.5, vy = 23.1, rotation = 1.4365, }) server:on("update", function(data, client) -- data = { -- x = 4, -- y = 100, -- vx = -4.5, -- vy = 23.1, -- rotation = 1.4365, -- } end) -- With schemas server:setSchema("update", { "x", "y", "vx", "vy", "rotation", }) -- client no longer has to send the keys, saving bandwidth client:send("update", { 4, 100, -4.5, 23.1, 1.4365, }) server:on("update", function(data, client) -- data = { -- x = 4, -- y = 100, -- vx = -4.5, -- vy = 23.1, -- rotation = 1.4365, -- } end)
-
Set the send channel for the next outgoing message.
The channel will be reset after the next message. Channels are zero-indexed and cannot exceed the maximum number of channels allocated. The initial default is 0.
Parameters:
- number channel
- Channel to send data on.
Usage:
server:setSendChannel(2) -- the third channel server:sendToAll("importantEvent", "The message")
-
Set the send mode for the next outgoing message.
The mode will be reset after the next message is sent. The initial default is "reliable".
Parameters:
- string mode
- A valid send mode.
See also:
Usage:
server:setSendMode("unreliable") server:sendToAll("playerState", {...})
-
Set the serialization functions for sending and receiving data.
Both the client and server must share the same serialization method.
Parameters:
- function serialize
- The serialization function to use.
- function deserialize
- The deserialization function to use.
Usage:
bitser = require "bitser" -- or any library you like server = sock.newServer("localhost", 22122) server:setSerialization(bitser.dumps, bitser.loads)
Server:enableCompression()
Server:getAddress()
Server:getClient(peer)
Server:getClientByConnectId(connectId)
Server:getClientByIndex(index)
Server:getClientCount()
Server:getClients()
Server:getLastServiceTime()
Server:getMaxChannels()
Server:getMaxPeers()
Server:getMessageTimeout()
Server:getPeerByIndex(index)
Server:getSocketAddress()
Server:getTotalReceivedData()
Server:getTotalReceivedPackets()
Server:getTotalSentData()
Server:getTotalSentPackets()
Server:log(event, data)
Server:on(event, callback)
Server:removeCallback(callback)
Server:sendToAll(event, data)
Server:sendToAllBut(client, event, data)
Server:sendToPeer(peer, event, data)
Server:setBandwidthLimit(incoming, outgoing)
Server:setDefaultSendChannel(channel)
Server:setDefaultSendMode(mode)
Server:setMaxChannels(limit)
Server:setMessageTimeout(timeout)
Server:setSchema(event, schema)
Server:setSendChannel(channel)
Server:setSendMode(mode)
Server:setSerialization(serialize, deserialize)
Class Client
Connects to servers.
-
Connect to the chosen server.
Connection will not actually occur until the next time Client:update is called.
Parameters:
- optional number code
- A number that can be associated with the connect event.
-
Disconnect from the server, if connected.
The client will disconnect the next time that network messages are sent.
Parameters:
- optional number code
- A code to associate with this disconnect event.
-
Disconnect from the server, if connected.
The client will disconnect after sending all queued packets.
Parameters:
- optional number code
- A code to associate with this disconnect event.
-
Disconnect from the server, if connected.
The client will disconnect immediately.
Parameters:
- optional number code
- A code to associate with this disconnect event.
-
Enables an adaptive order-2 PPM range coder for the transmitted data of all peers.
Both the client and server must both either have compression enabled or disabled.
Note: lua-enet does not currently expose a way to disable the compression after it has been enabled.
-
Get the IP address or hostname that the client was created with.
Returns:
- string
-
Get the unique connection id, if connected.
Returns:
- number The connection id.
-
Get the index of the enet peer.
All peers of an ENet host are kept in an array. This function finds and returns the index of the peer of its host structure.
Returns:
- number The index of the peer.
-
Get the last time when network events were serviced.
Returns:
- number Timestamp of the last time events were serviced.
-
Get the number of allocated channels.
Channels are zero-indexed, e.g. 16 channels allocated means that the maximum channel that can be used is 15.
Returns:
- number Number of allocated channels.
-
Get the timeout for packets.
Returns:
- number Time to wait for incoming packets in milliseconds. initial default is 0.
-
Get the enet_peer that has the given index.
Parameters:
- index
Returns:
- enet_peer The underlying enet peer object.
-
Return the round trip time (RTT, or ping) to the server, if connected.
It can take a few seconds for the time to approach an accurate value.
Returns:
- number The round trip time.
-
Get the socket address of the host.
Returns:
- string A description of the socket address, in the format "A.B.C.D:port" where A.B.C.D is the IP address of the used socket.
-
Get the current connection state, if connected.
Returns:
- string The connection state.
See also:
-
Get the total received data since the server was created.
Returns:
- number The total received data in bytes.
-
Get the total number of packets (messages) received since the client was created.
Returns:
- number The total number of received packets.
See also:
-
Get the total sent data since the server was created.
Returns:
- number The total sent data in bytes.
-
Get the total number of packets (messages) sent since the client was created.
Everytime a message is sent or received, the corresponding figure is incremented. Therefore, this is not necessarily an accurate indicator of how many packets were actually exchanged over the network.
Returns:
- number The total number of sent packets.
-
Gets whether the client is connected to the server.
Returns:
- boolean Whether the client is connected to the server.
Usage:
client:connect() client:isConnected() -- false -- After a few client updates client:isConnected() -- true
-
Gets whether the client is connecting to the server.
Returns:
- boolean Whether the client is connected to the server.
Usage:
client:connect() client:isConnecting() -- true -- After a few client updates client:isConnecting() -- false client:isConnected() -- true
-
Gets whether the client is disconnected from the server.
Returns:
- boolean Whether the client is connected to the server.
Usage:
client:disconnect() client:isDisconnected() -- false -- After a few client updates client:isDisconnected() -- true
-
Gets whether the client is disconnecting from the server.
Returns:
- boolean Whether the client is connected to the server.
Usage:
client:disconnect() client:isDisconnecting() -- true -- After a few client updates client:isDisconnecting() -- false client:isDisconnected() -- true
-
Log an event.
Alias for Client.logger:log.
Parameters:
- string event
- The type of event that happened.
- string data
- The message to log.
Usage:
if somethingBadHappened then client:log("error", "Something bad happened!") end
-
Add a callback to an event.
Parameters:
- string event
- The event that will trigger the callback.
- function callback
- The callback to be triggered.
Returns:
- function The callback that was passed in.
Usage:
client:on("connect", function(data) print("Connected to the server!") end)
-
Remove a specific callback for an event.
Parameters:
- function callback
- The callback to remove.
Returns:
- boolean Whether or not the callback was removed.
Usage:
local callback = client:on("chatMessage", function(message) print(message) end) client:removeCallback(callback)
-
Forcefully disconnects the client.
The server is not notified of the disconnection.
Parameters:
- Client client
- The client to reset.
-
Send a message to the server.
Parameters:
- string event
- The event to trigger with this message.
- data
- The data to send.
-
Set the incoming and outgoing bandwidth limits.
Parameters:
- number incoming
- The maximum incoming bandwidth in bytes.
- number outgoing
- The maximum outgoing bandwidth in bytes.
-
Set the default send channel for all future outgoing messages.
The initial default is 0.
Parameters:
- number channel
- Channel to send data on.
-
Set the default send mode for all future outgoing messages.
The initial default is "reliable".
Parameters:
- string mode
- A valid send mode.
See also:
-
Set the maximum number of channels.
Parameters:
- number limit
- The maximum number of channels allowed. If it is 0, then the maximum number of channels available on the system will be used.
-
Set the timeout to wait for packets.
Parameters:
- number timeout
- Time to wait for incoming packets in milliseconds. The initial default is 0.
-
Set how frequently to ping the server.
The round trip time is updated each time a ping is sent. The initial default is 500ms.
Parameters:
- number interval
- The interval, in milliseconds.
-
Set the data schema for an event.
Schemas allow you to set a specific format that the data will be sent. If the client and server both know the format ahead of time, then the table keys do not have to be sent across the network, which saves bandwidth.
Parameters:
- string event
- The event to set the data schema for.
- {string,...} schema
- The data schema.
Usage:
server = sock.newServer(...) client = sock.newClient(...) -- Without schemas server:send("update", { x = 4, y = 100, vx = -4.5, vy = 23.1, rotation = 1.4365, }) client:on("update", function(data) -- data = { -- x = 4, -- y = 100, -- vx = -4.5, -- vy = 23.1, -- rotation = 1.4365, -- } end) -- With schemas client:setSchema("update", { "x", "y", "vx", "vy", "rotation", }) -- client no longer has to send the keys, saving bandwidth server:send("update", { 4, 100, -4.5, 23.1, 1.4365, }) client:on("update", function(data) -- data = { -- x = 4, -- y = 100, -- vx = -4.5, -- vy = 23.1, -- rotation = 1.4365, -- } end)
-
Set the send channel for the next outgoing message.
The channel will be reset after the next message. Channels are zero-indexed and cannot exceed the maximum number of channels allocated. The initial default is 0.
Parameters:
- number channel
- Channel to send data on.
Usage:
client:setSendChannel(2) -- the third channel client:send("important", "The message")
-
Set the send mode for the next outgoing message.
The mode will be reset after the next message is sent. The initial default is "reliable".
Parameters:
- string mode
- A valid send mode.
See also:
Usage:
client:setSendMode("unreliable") client:send("position", {...})
-
Set the serialization functions for sending and receiving data.
Both the client and server must share the same serialization method.
Parameters:
- function serialize
- The serialization function to use.
- function deserialize
- The deserialization function to use.
Usage:
bitser = require "bitser" -- or any library you like client = sock.newClient("localhost", 22122) client:setSerialization(bitser.dumps, bitser.loads)
-
Change the probability at which unreliable packets should not be dropped.
Parameters:
- number interval
- Interval, in milliseconds, over which to measure lowest mean RTT. (default: 5000ms)
- number acceleration
- Rate at which to increase the throttle probability as mean RTT declines. (default: 2)
- number deceleration
- Rate at which to decrease the throttle probability as mean RTT increases.
-
Set the parameters for attempting to reconnect if a timeout is detected.
Parameters:
- optional number limit
- A factor that is multiplied with a value that based on the average round trip time to compute the timeout limit. (default: 32)
- optional number minimum
- Timeout value in milliseconds that a reliable packet has to be acknowledged if the variable timeout limit was exceeded. (default: 5000)
- optional number maximum
- Fixed timeout in milliseconds for which any packet has to be acknowledged.
Client:connect(code)
Client:disconnect(code)
Client:disconnectLater(code)
Client:disconnectNow(code)
Client:enableCompression()
Client:getAddress()
Client:getConnectId()
Client:getIndex()
Client:getLastServiceTime()
Client:getMaxChannels()
Client:getMessageTimeout()
Client:getPeerByIndex(index)
Client:getRoundTripTime()
Client:getSocketAddress()
Client:getState()
Client:getTotalReceivedData()
Client:getTotalReceivedPackets()
Client:getTotalSentData()
Client:getTotalSentPackets()
Client:isConnected()
Client:isConnecting()
Client:isDisconnected()
Client:isDisconnecting()
Client:log(event, data)
Client:on(event, callback)
Client:removeCallback(callback)
Client:reset(client)
Client:send(event, data)
Client:setBandwidthLimit(incoming, outgoing)
Client:setDefaultSendChannel(channel)
Client:setDefaultSendMode(mode)
Client:setMaxChannels(limit)
Client:setMessageTimeout(timeout)
Client:setPingInterval(interval)
Client:setSchema(event, schema)
Client:setSendChannel(channel)
Client:setSendMode(mode)
Client:setSerialization(serialize, deserialize)
Client:setThrottle(interval, acceleration, deceleration)
Client:setTimeout(limit, minimum, maximum)
sock
-
Creates a new Client instance.
Parameters:
- optional string/peer serverOrAddress
- Usually the IP address or hostname to connect to. It can also be an enet peer. (default: "localhost")
- optional number port
- Port number of the server to connect to. (default: 22122)
- optional number maxChannels
- Maximum channels available to send and receive data. (default: 1)
Returns:
- A new Client object.
See also:
Usage:
local sock = require "sock" -- Client that will connect to localhost:22122 (by default) client = sock.newClient() -- Client that will connect to localhost:1234 client = sock.newClient("localhost", 1234) -- Client that will connect to 123.45.67.89:1234, using two channels -- NOTE: Server must also allocate two channels! client = sock.newClient("123.45.67.89", 1234, 2)
-
Creates a new Server object.
Parameters:
- optional string address
- Hostname or IP address to bind to. (default: "localhost")
- optional number port
- Port to listen to for data. (default: 22122)
- optional number maxPeers
- Maximum peers that can connect to the server. (default: 64)
- optional number maxChannels
- Maximum channels available to send and receive data. (default: 1)
- optional number inBandwidth
- Maximum incoming bandwidth (default: 0)
- optional number outBandwidth
- Maximum outgoing bandwidth (default: 0)
Returns:
- A new Server object.
See also:
Usage:
local sock = require "sock" -- Local server hosted on localhost:22122 (by default) server = sock.newServer() -- Local server only, on port 1234 server = sock.newServer("localhost", 1234) -- Server hosted on static IP 123.45.67.89, on port 22122 server = sock.newServer("123.45.67.89", 22122) -- Server hosted on any IP, on port 22122 server = sock.newServer("*", 22122) -- Limit peers to 10, channels to 2 server = sock.newServer("*", 22122, 10, 2) -- Limit incoming/outgoing bandwidth to 1kB/s (1000 bytes/s) server = sock.newServer("*", 22122, 10, 2, 1000, 1000)