-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.lua
71 lines (57 loc) · 2.66 KB
/
client.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
net = require("net")
function love.load()
print("CLIENT")
local address = "127.0.0.1:*" --just use a random port to connect
local max_connections = 1 --only connecting to server, so only 1 connection needed
local max_channels = 1
local in_bandwidth = 0 --unlimited bandwidth
local out_bandwidth = 0 --unlimited bandwidth
net.init(address, max_connections, max_channels, in_bandwidth, out_bandwidth)
net.connect("127.0.0.1:27015") --connect to the server
end
function love.update()
net.update() --calls all callbacks which have been queued up by thread
end
--will be called any time a state changes for address
--list of states here: https://leafo.net/lua-enet/#peerstate
net.state(function(address, state)
print("state changed for '" .. address .. "' to " .. state)
if state == "connected" then
print("connection established with " .. address)
end
end)
--callback will be called any time we receive a message with "ping"
net.receive("ping", function(address, roundTripTime)
--you'll need to send a lot more messages to average down the round trip time as it starts high and then lowers
print("received message: 'ping' from '" .. address .. "'", roundTripTime)
--the packet must be read in the order it was written in
local byte = net.readByte()
local bool = net.readBool()
local int = net.readInt()
local float = net.readFloat()
local double = net.readDouble()
local str = net.readString()
local r, g, b, a = net.readColour()
--custom format, quicker but more complicated
local fByte, fInt, fFloat, fDouble, fString, index = net.readFormat("Bifds") --last returned value is index in stream
net.seek(index) --must seek to index after reading custom format, otherwise wont be able to read other values after custom format
print("\tbyte:", byte)
print("\tbool:", bool)
print("\tint:", int)
print("\tfloat:", float)
print("\tdouble:", double)
print("\tstring:", str)
print("\tcolour:", r, g, b, a)
print("\tcustom:", fByte, fInt, fFloat, fDouble, fString)
net.start("pong")
net.send(address) --send message to address only
--net.send(address, flag, channel)
--net.broadcast() --send message to all connections
--net.broadcast(flag, channel)
-- flag: 0 = reliable, 1 = unreliable, 2 = unsequenced, defaults to reliable
-- "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.
-- channel: defaults to 0
-- The channel to send the packet on
end)