Difference between revisions of "enet.peer:send"

(Created page with "Queues a packet to be sent to peer. == Function == === Synopsis === <source lang="lua"> peer:send(data, channel, flag) </source> === Arguments === {{param|string...")
 
m
 
(9 intermediate revisions by 5 users not shown)
Line 1: Line 1:
Queues a packet to be sent to [[enet.peer | peer]].
+
Queues a packet to be sent to the [[enet.peer | peer]].
  
 
== Function ==
 
== Function ==
Line 9: Line 9:
 
{{param|string|data|The contents of the packet, it must be a [[string]].}}
 
{{param|string|data|The contents of the packet, it must be a [[string]].}}
 
{{param|number|channel|The channel to send the packet on. Optional. Defaults to 0.}}
 
{{param|number|channel|The channel to send the packet on. Optional. Defaults to 0.}}
{{param|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. Unsequenced packets are unreliable and have no guarantee on the order they arrive. Optional. Defaults to reliable.}}
+
{{param|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 ===
 
=== Returns ===
None
+
Nothing.
 +
 
 +
== Function ==
 +
{{newin|[[12.0]]|120|type=variant}}
 +
=== Synopsis ===
 +
<source lang="lua">
 +
peer:send(pointer, size, channel, flag)
 +
</source>
 +
=== Arguments ===
 +
{{param|light userdata|pointer|The contents of the packet.}}
 +
{{param|number|size|The size of the memory starting at the pointer.}}
 +
{{param|number|channel|The channel to send the packet on. Optional. Defaults to 0.}}
 +
{{param|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 ==
 +
{{newin|[[12.0]]|120|type=variant}}
 +
=== Send a packet using [[Data]] instead of a [[String]] ===
 +
<source lang="lua">
 +
-- 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
 +
</source>
 +
<source lang="lua">
 +
-- 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
 +
</source>
  
 
== See Also ==
 
== See Also ==
 
* [[parent::lua-enet]]
 
* [[parent::lua-enet]]
 
* [[enet.peer]]
 
* [[enet.peer]]
 +
* [[enet.peer:receive]]
 +
* [[enet.host]]
 +
* [[enet.event]]
 
== Other Languages ==
 
== Other Languages ==
 
{{i18n|enet.peer:send}}
 
{{i18n|enet.peer:send}}

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