Difference between revisions of "lua-enet"
m |
m |
||
(27 intermediate revisions by 9 users not shown) | |||
Line 1: | Line 1: | ||
− | + | {{newin|[[0.9.0]]|090|type=module}} | |
− | ENet's purpose is to provide a relatively thin, simple and robust network communication layer on top of UDP (User Datagram Protocol).The primary feature it provides is optional reliable, in-order delivery of packets. | + | {{notice|Official documentation for lua-enet is available [http://leafo.net/lua-enet/ here]. ENet's features are listed on its [http://enet.bespin.org/Features.html homepage]. The official documentation may have typos. The documentation on this wiki reflects Löve's implementation, meaning it should be safe to follow what's written here.}} |
+ | |||
+ | lua-enet is simply some Lua bindings for ENet. | ||
+ | |||
+ | ENet's purpose is to provide a relatively thin, simple and robust network communication layer for games on top of UDP (User Datagram Protocol).The primary feature it provides is optional reliable, in-order delivery of packets. | ||
ENet omits certain higher level networking features such as authentication, lobbying, server discovery, encryption, or other similar tasks that are particularly application specific so that the library remains flexible, portable, and easily embeddable. | ENet omits certain higher level networking features such as authentication, lobbying, server discovery, encryption, or other similar tasks that are particularly application specific so that the library remains flexible, portable, and easily embeddable. | ||
Line 14: | Line 18: | ||
|[[enet.peer | peer]] | |[[enet.peer | peer]] | ||
|An ENet peer which data packets may be sent or received from. | |An ENet peer which data packets may be sent or received from. | ||
+ | |- | ||
+ | |[[enet.event | event]] | ||
+ | |A simple table containing information on an event. | ||
|} | |} | ||
Line 21: | Line 28: | ||
!Description | !Description | ||
|- | |- | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
− | |[[enet. | + | |[[enet.host_create | host_create]] |
− | |Returns | + | |Returns a new host. |
|- | |- | ||
− | |[[enet. | + | |[[enet.linked_version| linked_version]] |
− | |Returns | + | |Returns the included ENet's version string. |
|} | |} | ||
Line 110: | Line 40: | ||
<source lang="Lua"> | <source lang="Lua"> | ||
-- server.lua | -- server.lua | ||
− | require "enet" | + | local enet = require "enet" |
local host = enet.host_create("localhost:6789") | local host = enet.host_create("localhost:6789") | ||
− | local event = host:service(100) | + | while true do |
− | while event do | + | local event = host:service(100) |
− | + | while event do | |
− | + | if event.type == "receive" then | |
− | + | print("Got message: ", event.data, event.peer) | |
− | + | event.peer:send( "pong" ) | |
− | + | elseif event.type == "connect" then | |
− | + | print(event.peer, "connected.") | |
− | + | elseif event.type == "disconnect" then | |
+ | print(event.peer, "disconnected.") | ||
+ | end | ||
+ | event = host:service() | ||
end | end | ||
− | |||
end | end | ||
</source> | </source> | ||
Line 129: | Line 61: | ||
<source lang="Lua"> | <source lang="Lua"> | ||
-- client.lua | -- client.lua | ||
− | require "enet" | + | local enet = require "enet" |
local host = enet.host_create() | local host = enet.host_create() | ||
local server = host:connect("localhost:6789") | local server = host:connect("localhost:6789") | ||
− | local event = host:service(100) | + | while true do |
− | while event do | + | local event = host:service(100) |
− | + | while event do | |
− | + | if event.type == "receive" then | |
− | + | print("Got message: ", event.data, event.peer) | |
− | + | event.peer:send( "ping" ) | |
− | + | elseif event.type == "connect" then | |
− | + | print(event.peer, "connected.") | |
− | + | event.peer:send( "ping" ) | |
− | + | elseif event.type == "disconnect" then | |
+ | print(event.peer, "disconnected.") | ||
+ | end | ||
+ | event = host:service() | ||
end | end | ||
− | |||
end | end | ||
</source> | </source> | ||
+ | |||
+ | This is another example that creates a server (receiver) and a client (sender) on the same computer with the same script. The client will say "Hi" to the server endlessly because ClientSend() is called inside love.update. | ||
+ | |||
+ | <source lang="Lua"> | ||
+ | enet = require "enet" | ||
+ | |||
+ | enethost = nil | ||
+ | hostevent = nil | ||
+ | clientpeer = nil | ||
+ | |||
+ | function love.load(args) | ||
+ | |||
+ | -- establish host for receiving msg | ||
+ | enethost = enet.host_create("localhost:6750") | ||
+ | |||
+ | -- establish a connection to host on same PC | ||
+ | enetclient = enet.host_create() | ||
+ | clientpeer = enetclient:connect("localhost:6750") | ||
+ | |||
+ | end | ||
+ | |||
+ | function love.update(dt) | ||
+ | ServerListen() | ||
+ | ClientSend() | ||
+ | end | ||
+ | |||
+ | function love.draw() | ||
+ | end | ||
+ | |||
+ | function ServerListen() | ||
+ | |||
+ | hostevent = enethost:service(100) | ||
+ | |||
+ | if hostevent then | ||
+ | print("Server detected message type: " .. hostevent.type) | ||
+ | if hostevent.type == "connect" then | ||
+ | print(hostevent.peer, "connected.") | ||
+ | end | ||
+ | if hostevent.type == "receive" then | ||
+ | print("Received message: ", hostevent.data, hostevent.peer) | ||
+ | end | ||
+ | end | ||
+ | end | ||
+ | |||
+ | function ClientSend() | ||
+ | enetclient:service(100) | ||
+ | clientpeer:send("Hi") | ||
+ | end | ||
+ | </source> | ||
+ | |||
+ | == See Also == | ||
+ | * [[parent::love]] | ||
+ | * [[socket]] | ||
+ | == Other Languages == | ||
+ | {{i18n|lua-enet}} | ||
+ | |||
+ | [[Category:Libraries]] | ||
+ | [[Category:ThirdParty]] | ||
+ | {{#set:Description=Multiplayer networking module for games.}} | ||
+ | {{#set:LOVE Version=0.9.0}} | ||
+ | {{#set:Keyword=Networking}} |
Latest revision as of 03:03, 5 July 2023
Available since LÖVE 0.9.0 |
This module is not supported in earlier versions. |
Official documentation for lua-enet is available here. ENet's features are listed on its homepage. The official documentation may have typos. The documentation on this wiki reflects Löve's implementation, meaning it should be safe to follow what's written here. |
lua-enet is simply some Lua bindings for ENet.
ENet's purpose is to provide a relatively thin, simple and robust network communication layer for games on top of UDP (User Datagram Protocol).The primary feature it provides is optional reliable, in-order delivery of packets.
ENet omits certain higher level networking features such as authentication, lobbying, server discovery, encryption, or other similar tasks that are particularly application specific so that the library remains flexible, portable, and easily embeddable.
Types
Type | Description |
---|---|
host | An ENet host for communicating with peers. |
peer | An ENet peer which data packets may be sent or received from. |
event | A simple table containing information on an event. |
Functions
Function | Description |
---|---|
host_create | Returns a new host. |
linked_version | Returns the included ENet's version string. |
Examples
server.lua
-- server.lua
local enet = require "enet"
local host = enet.host_create("localhost:6789")
while true do
local event = host:service(100)
while event do
if event.type == "receive" then
print("Got message: ", event.data, event.peer)
event.peer:send( "pong" )
elseif event.type == "connect" then
print(event.peer, "connected.")
elseif event.type == "disconnect" then
print(event.peer, "disconnected.")
end
event = host:service()
end
end
client.lua
-- client.lua
local enet = require "enet"
local host = enet.host_create()
local server = host:connect("localhost:6789")
while true do
local event = host:service(100)
while event do
if event.type == "receive" then
print("Got message: ", event.data, event.peer)
event.peer:send( "ping" )
elseif event.type == "connect" then
print(event.peer, "connected.")
event.peer:send( "ping" )
elseif event.type == "disconnect" then
print(event.peer, "disconnected.")
end
event = host:service()
end
end
This is another example that creates a server (receiver) and a client (sender) on the same computer with the same script. The client will say "Hi" to the server endlessly because ClientSend() is called inside love.update.
enet = require "enet"
enethost = nil
hostevent = nil
clientpeer = nil
function love.load(args)
-- establish host for receiving msg
enethost = enet.host_create("localhost:6750")
-- establish a connection to host on same PC
enetclient = enet.host_create()
clientpeer = enetclient:connect("localhost:6750")
end
function love.update(dt)
ServerListen()
ClientSend()
end
function love.draw()
end
function ServerListen()
hostevent = enethost:service(100)
if hostevent then
print("Server detected message type: " .. hostevent.type)
if hostevent.type == "connect" then
print(hostevent.peer, "connected.")
end
if hostevent.type == "receive" then
print("Received message: ", hostevent.data, hostevent.peer)
end
end
end
function ClientSend()
enetclient:service(100)
clientpeer:send("Hi")
end
See Also
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