LÖVE CÖNNECTION
LÖVE CÖNNECTION is a networking module coded and maintained by Nicholas Scott [1]
When the module is loaded with require() it will return a table of functions for the list I will be assuming that the require is set equal to Net:
Contents
Functions
- Net:connectedUsers() --Returns a list of currently connected users
- Net:kickUser( id, reason ) --Kicks the user with specified id and the reason is currently printed to console
- Net:isServer() --Returns true if currently running as a server
- Net:isClient() --Returns true if currently running as a client
- Net:setMaxPing( ping ) --If called while running as server it will set the maximum ping( milliseconds ) for clients before they timeout, if called as client it will set the ping time before it pings the server again
- Net:init( state ) --Starts the networking, state needs to be either server, to run as a server, or client, to run as a client
- Net:update( dt ) --Add this into any ACTIVE love.update() and pass the deltatime from the love.update!! IMPORTANT!!!!
- Net:connect( ip, port ) --If called as server you must set ip to nil and port to the port you want to listen on, defaults to 20024, when called as client ip and port are required, and port must be the same as the servers, obviously :D
- Net:disconnect() --Disconnects from online, if it is ran from client it will nil out the socket entirely and send a disconnect packet to the server it was on
- Net:registerCMD( cmd, function ) --Registers a command for networking, cmd is a string name of the command you want to register, and function is the function ran when a client( or server ) receives the command specified. More on this below :D
- Net:send( table, cmd, param, id ) --Id is only supplied if running as server, table is a table of data you want to send, cmd is the command your running on the other end, must be registered, param is parameters to the command, id is the id of the client you want to send to( only if server leave nil if client)
- Net:encode( table ) --Encodes a table of data into a string to be sent across the network
- Net:decode( string ) --Decodes a string into a table of data when received
Events
These are called the same way you call love.update() function Net.event.server.userConnected( id ) end
Client Events
- Net.event.client.connect( ip, port) --Called when client connects to a server with net:connect() ip is ip of server and port is port of server
- Net.event.client.receive( table, dt, cmd, param ) --Called when client receives a packet table is the table client sent, dt deltatime of net:update(), cmd is the command from client, param is parameters from the client
- Net.event.client.disconnect() --Called when client disconnects from server with net:disconnect()
- Net.event.client.cmdRegistered( cmd, functionS ) --Called when a command is registered, cmd is the cmd and function is the function called by the cmd
- Net.event.client.send( table, cmd, param ) --Called when a packet is sent to the server, table is the table being sent, cmd is the cmd, param is parameters
- Net.event.client.kickedFromServer( reason ) --Called when we( the client ) is kicked from the server, reason is the reason for the kick
Server Events
- Net.event.server.userConnected( id ) --Called when a user connects to the server
- Net.event.server.userDisconnected( id ) --Called when a user disconnects from the server
- Net.event.server.userTimedOut( id ) --Called when a user times out from the server
- Net.event.server.userKicked( id, reason ) --Called when a user is kicked, reason is the reason they were kicked
- Net.event.server.receive( table, dt, id, cmd, param ) --Called when we receive a packet, table is the table sent by client, id is id of client, dt is deltatime of net:update(), cmd is the command sent by client, param is parameters of the client
- Net.event.server.connect( port ) --Called when we connect to a port, port is the port connected to
- Net.event.server.disconnect() --Called when we disconnect from the port and stop listening to packets
- Net.event.server.cmdRegistered( cmd, functionS) --Called when we register a command, cmd is the command registered, function is the function called by the cmd
- Net.event.server.send( table, cmd, param, id ) --Called when we send a packet to a client, table is the table sent, cmd is the cmd sent, param is the parameters sent, id is the id of the client we sent to
Variables/Tables
- Net.client = table --Holds Ip and port of currently connected server, if not connected ip and port are nil
- Net.server = table --Holds the port currently being listened on, nil if not listening
- Net.commands = table --Holds all currently registered commands in format CMD = FUNCTION, command editing function coming soon!
- Net.users = table --Holds all currently connected users in format ID = PLAYERTABLE, player table editing function coming soon!
- Net.maxPing = integer --Current max ping of server, or client
- Net.connected = boolean --True if connected to a server or listening to a port, false if not
Tutorial
Server Setup
Step 1: Require the module as a variable
Net = require( "net" )
Step 2: Initialize the networking
Net:init( "Server" )
Step 3: Connect to a port to listen for packets on
Net:connect( nil, 25045 )
Step 4: Register some commands for received packets, the table is a table encoded and sent to us by the client :D useful for this
Net:registerCMD( "updateStatsOfPlayer", function( table, parameters, id, dt ) Net.users[id].name = table.name Net.users[id].color = table.color end )
Step 5: Send some packets to a client! :D This will print to every client on the server's console "Hello There Mr. Client!"
for id,playerdata in pairs( Net.users ) do
Net:send( {}, "print", "Hello There Mr. Client!", id )
end
Client Setup
Step 1: Require the module as a variable
Net = require( "net" )
Step 2: Initialize the networking
Net:init( "Client" )
Step 3: Connect to a server, this will also initialize the handshake feature and allow us to use net:send()
Net:connect( "127.0.0.1", 25045 )
Step 4: Register some commands for received packets, the table is a table encoded and sent to us by the client :D useful for this
Net:registerCMD( "updateStatsOfBattlefield", function( table, parameters, id, dt )Tank.x = table.x Tank.y = table.y Tank.name = table.name end )
Step 5: Send some packets to the server! :D This will print to the servers console "Hello there Mr. Server!", the {} is an empty table, THIS IS REQUIRE DON'T LEAVE IT NIL
Net:send( {}, "print", "Hello There Mr. Server!" )
Other Languages
Dansk –
Deutsch –
English –
Español –
Français –
Indonesia –
Italiano –
Lietuviškai –
Magyar –
Nederlands –
Polski –
Português –
Română –
Slovenský –
Suomi –
Svenska –
Türkçe –
Česky –
Ελληνικά –
Български –
Русский –
Српски –
Українська –
עברית –
ไทย –
日本語 –
正體中文 –
简体中文 –
Tiếng Việt –
한국어
More info