Hello,
I’ve been working on a local-multiplayer game using socket which uses a server browser to look for any server up and running on your local network. I was just wondering if anybody has two iPads, I would love if you could kindly test my code and see if the whole multiplayer thing works, as I don’t have a second iPad like some.
Here is the code for testing:
function setup()
scene = craft.scene()
craft.scene.main = scene
socket = require("socket")
messenger = socket.udp()
messenger:setoption("broadcast", true)
messenger:settimeout(0)
parameter.action("Host", createServer)
client_data = {}
clients = {}
entities = {}
servers = {}
my_client = nil
my_entity = nil
my_server = nil
-- Something to land on so the clients dont fall endlessly into the void
ground = scene:entity()
ground.model = craft.model(asset.builtin.Primitives.RoundedCube)
ground.position = vec3(0, -2, 10)
ground.scale = vec3(10, 0.1, 10)
gbody = ground:add(craft.rigidbody, STATIC)
ground:add(craft.shape.model, ground.model)
-- Setup camera
scene.camera.position = vec3(0, 8, -10)
scene.camera.rotation = quat.eulerAngles(25, 0, 0)
end
function joinServer(ip, port)
if my_client == nil then
local client = socket.udp()
client:setpeername(ip, port)
client:settimeout(0)
table.insert(clients, client)
updateEntities(client)
client:send("join")
my_client = client
end
end
function createServer()
if my_server == nil then
server = socket.udp()
server:setsockname("*", 14285)
server:setoption("broadcast", true)
server:settimeout(0)
my_server = server
end
end
function updateEntities(client)
if not entities[entity] then
entity = scene:entity()
entity.model = craft.model(asset.builtin.Primitives.Sphere)
entity.position = vec3(0, 0, 10)
body = entity:add(craft.rigidbody, DYNAMIC)
entity:add(craft.shape.sphere, 1)
entity.material = craft.material(asset.builtin.Materials.Specular)
entity.material.diffuse = color(math.random(0, 255), math.random(0, 255), math.random(0, 255))
entity.master = client
body.linearDamping = 0.8
my_entity = entity
table.insert(entities, entity)
end
end
function touched(touch)
if touch.state == MOVING then
if touch.deltaX > 0 then
for i,e in pairs(entities) do
if e == my_entity then
e:get(craft.rigidbody):applyForce(vec3(-30, 0, 0))
end
end
elseif touch.deltaX < 0 then
for i,e in pairs(entities) do
if e == my_entity then
e:get(craft.rigidbody):applyForce(vec3(30, 0, 0))
end
end
elseif touch.deltaY > 0 then
for i,e in pairs(entities) do
if e == my_entity then
e:get(craft.rigidbody):applyForce(vec3(0, 0, 30))
end
end
elseif touch.deltaY < 0 then
for i,e in pairs(entities) do
if e == my_entity then
e:get(craft.rigidbody):applyForce(vec3(0, 0, -30))
end
end
end
end
end
function draw()
-- Check if any "server messengers" have broadcasted anything
if my_server ~= nil then
local data_received, msgOrIp, portOrNil = my_server:receivefrom()
if data_received ~= nil then
if data_received == "checking for available servers" then
-- If yes, we know there is another server and we can send something back
my_server:sendto("server available", msgOrIp, portOrNil)
elseif data_received == "join" then
my_server:sendto("server entities list: "..#entities, msgOrIp, portOrNil)
end
end
end
-- Here, we check if our messenger has received anything back from any server on the network
local data, ip, port = messenger:receivefrom()
if data ~= nil then
if data == "server available" then
local uid = {ip = ip, port = port}
if not servers[uid] then
table.insert(servers, uid)
end
end
end
-- Here, we loop through every set of server information our messenger has received, and we use that information to create buttons that will create a client connected to that server if pressed
for i,s in pairs(servers) do
parameter.action("Join: "..tostring(s.ip).." - "..tostring(s.port), function() joinServer(s.ip, s.port) end)
end
-- We can loop through every client currently connected to the server, and see if the server has sent the amount of entities (this will only happen if you have just joined the server)
for i,c in pairs(clients) do
local data = c:receive()
if data ~= nil then
if string.find(data, "server entities list") then
local amount = string.sub(data, 22, #data)
for i = 1, tonumber(amount) do
if not entities[i].master == c then
updateEntities(c)
end
end
end
end
end
-- Continuously send UDP broadcasts across the whole network
messenger:sendto("checking for available servers", "255.255.255.255", "14285")
end
Thanks in advance,
Creator27