Difference between revisions of "enet.peer:send"

(Undo revision 27119 by Popolon (talk) (oops english version, not french one))
(Tag: Undo)
m
 
(2 intermediate revisions by the same user not shown)
Line 12: Line 12:
 
=== Returns ===
 
=== Returns ===
 
Nothing.
 
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 ==

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