Difference between revisions of "enet.peer:send"

m (Send a packet using Data instead of a lua string)
m
 
Line 34: Line 34:
 
local enet = require("enet")
 
local enet = require("enet")
 
local host = enet.host_create()
 
local host = enet.host_create()
local server = host:connect("localhost:10001", 64, 0)
+
local server = host:connect("localhost:10001", 0)
 
host:service(100)
 
host:service(100)
  

Latest revision as of 13:10, 10 May 2023

Queues a packet to be sent to the peer.

Function

Synopsis

peer:send(data, channel, flag)

Arguments

string data
The contents of the packet, it must be a string.
number channel
The channel to send the packet on. Optional. Defaults to 0.
string flag
flag is one of "reliable", "unsequenced", or "unreliable". Reliable packets are guaranteed to arrive, and arrive in the order in which they are sent. Unreliable packets arrive in the order in which they are sent, but they aren't guaranteed to arrive. Unsequenced packets are neither guaranteed to arrive, nor do they have any guarantee on the order they arrive. Optional. Defaults to reliable.

Returns

Nothing.

Function

Available since LÖVE 12.0
This variant is not supported in earlier versions.

Synopsis

peer:send(pointer, size, channel, flag)

Arguments

light userdata pointer
The contents of the packet.
number size
The size of the memory starting at the pointer.
number channel
The channel to send the packet on. Optional. Defaults to 0.
string flag
flag is one of "reliable", "unsequenced", or "unreliable". Reliable packets are guaranteed to arrive, and arrive in the order in which they are sent. Unreliable packets arrive in the order in which they are sent, but they aren't guaranteed to arrive. Unsequenced packets are neither guaranteed to arrive, nor do they have any guarantee on the order they arrive. Optional. Defaults to reliable.

Returns

Nothing.

Examples

Available since LÖVE 12.0
This variant is not supported in earlier versions.

Send a packet using Data instead of a String

-- client
local enet = require("enet")
local host = enet.host_create()
local server = host:connect("localhost:10001", 0)
host:service(100)

-- populate byteData
local byteData = love.data.newByteData(5)
local ffi = require("ffi")
local ptr = ffi.cast("void*", byteData:getFFIPointer())
ffi.copy(ptr, "hello")

server:send(byteData:getPointer(), byteData:getSize(), 11, "reliable") -- send data

love.update = function()
  host:service(10)
end
-- server
local enet = require("enet")
local host = enet.host_create("*:10001", 64, 0)

local text, channel
love.update = function()
  local event = host:service(10)
  if event and event.type == "receive" then
    text = event.data
    channel = event.channel
  end
end

love.draw = function()
-- prints 'hello' once client joins and the channel it was sent on
  love.graphics.print(tostring(text).."\nChannel: "..tostring(channel))
end

See Also

Other Languages