@dave1707, I have programmed my code to have a UDP socket test how long it takes to get a message back from every server on the network and calculate all of the times into an average timeframe. So hopefully if this works, you now shouldn’t have to manually change the delay in the code to fit the iPad’s needs. If however the average is too small a time for your iPads to receive anything, you can also try multiplying the “send_average” variable by say 10, 100 or 1000. Here’s the code:
function setup()
socket = require("socket")
parameter.action("Create Room", createRoom)
parameter.action("Join Room", function()
if other_ip then
joinRoom(other_ip)
else
parameter.text("IP", "", function(t)
other_ip = t
end)
end
end)
parameter.action("Find Room", function()
packetTest()
end)
client = socket.udp()
client:settimeout(0)
-- We need a spare UDP socket to use for when we need to determine how long it takes to get a message back from every server on the network
listener = socket.udp()
listener:settimeout(0)
listen_time = nil
timeframes = {}
-- Our send average variable will store the average amount of time it takes to get a message back from every server on your router
send_average = 0
-- We need to create a table that can hold information about our server, if you end up making one
server_info = {players = 0, clients = {}}
other_ip = nil
current_time = nil
current_room_count = 1
tick = 0
finding_room = false
server = false
end
function createRoom()
-- We need to find your address, so we create a new UDP socket and assign it a random IP
local udp = socket.udp()
udp:setpeername("192.167.188.122", "14285")
local ip, port = udp:getsockname()
udp:close()
client:setsockname("*", "14285")
local ip2, port2 = client:getsockname()
client:setoption("broadcast", true)
your_ip = ip2
your_port = port2
print("Room created!")
print("Connect to "..ip)
server = true
end
function joinRoom(ip)
if server == false then
print("Attempting to join the room...")
-- The port for all the servers is the same, so we only need the IP address of the server
player:setpeername(ip, "14285")
player:send("connection confirmation")
current_time = os.time()
else
print("You are already hosting a server!")
end
end
function packetTest()
if server == false then
-- We need to send a broadcast message to every server on the network, but first we need to bind our socket to every address
listener:setsockname("*", "14285")
listener:setoption("broadcast", true)
-- Now we can send a packet to every server using the broadcast address "255.255.255.255"
listener:sendto("packet test", "255.255.255.255", "14285")
listen_time = tick
else
print("You are already hosting a server!")
end
end
function findRoom(count)
if count == 1 then
print("Attempting to find a room...")
print("This can take anywhere from 5-50secs")
end
finding_room = true
-- First, we need to find your IP
local udp = socket.udp()
udp:setpeername("192.167.188.122", "14285")
local ip, port = udp:getsockname()
udp:close()
local ip1, ip2 = string.match(ip, "(%d+.%d+.%d+.)(%d+)")
client:setpeername(ip1..count, "14285")
client:send("connection confirmation")
current_time = tick
end
function receiveData()
if server == true then
local msg, ip, port = client:receivefrom()
if msg ~= nil then
if msg == "connection confirmation" then
if server_info["players"] < 32 then
-- We can now add 1 more to our player count
server_info["players"] = server_info["players"] + 1
-- We now need to store the clients IP and port, so we can send them messages later
local uid = {ip = ip, port = port}
if not server_info["clients"][uid] then
table.insert(server_info["clients"], uid)
end
client:sendto("valid confirmation", ip, port)
-- We can now send everyone that has joined this room a packet telling them the amount of players in the room
for i,c in pairs(server_info["clients"]) do
client:sendto("player count"..tostring(server_info["players"]), c.ip, c.port)
end
else
-- We don't have enough room for another player, so we send the peer a message telling them to disconnect
local reason = "Max players inside the room!"
client:sendto("invalid confirmation"..reason, ip, port)
end
elseif msg == "packet test" then
client:sendto("packet test completed", ip, port)
end
end
else
local result = client:receive()
if result == "valid confirmation" then
print("Successfully joined the room!")
current_time = nil
elseif result and string.find(result, "player count") then
-- We now know the current amount of players in our room, for now at least
local count = string.sub(result, 13, #result)
print("Player count: "..count)
elseif result and string.find(result, "invalid confirmation") then
-- We can now see the reason for our invalid confirmation and display it
local reason = string.sub(result, 21, #result)
print("Could not join the room: "..reason)
current_time = nil
-- We can now also remove the address and port set for our peer
client:setpeername("*")
-- If there is still another room available on the network and we haven't looped through to 255 yet, we can still continue the search
current_room_count = current_room_count + 1
if current_room_count < 256 then
findRoom(current_room_count)
end
elseif result == nil then
if current_time ~= nil then
if finding_room == false then
if os.time() > current_time + 5 then
-- If we get no response back for 5 seconds or more, we know it didn't work
print("Failed to connect, please try again later")
current_time = nil
end
else
-- If the time limit has passed, we can check the next address for a room
if tick > current_time + send_average then
current_room_count = current_room_count + 1
if current_room_count < 256 then
findRoom(current_room_count)
end
end
end
end
end
local data, ip, port = listener:receivefrom()
if data == "packet test completed" then
local result = (tick - listen_time)
table.insert(timeframes, result)
listen_time = tick
else
if listen_time and tick > listen_time + 15 then
-- We can now assume that every server has sent the same message back to our listener, and now we can calculate an average
local average = 0
for i,t in pairs(timeframes) do
average = average + t
end
average = average / #timeframes
send_average = average
print("Average receive time: "..send_average)
if finding_room == false then
findRoom(current_room_count)
end
listen_time = nil
end
end
end
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
-- This sets the line thickness
strokeWidth(5)
-- Do your drawing here
receiveData()
tick = tick + 1.0
end