I guess I should reword that correctly. The iPad gives me an IP address.
Here’s the code:
-- Chat Room
-- Use this function to perform your initial setup
function setup()
msgs = {}
msg = ""
scrolling = false
parameter.text("Username", "Guest")
supportedOrientations(LANDSCAPE_ANY)
local connectionMade = function()
output.clear()
parameter.clear()
print("Connected!")
end
multihandler = Multiuser(receiveData, connectionMade)
parameter.action("Host Room", function()
multihandler:hostRoom()
end)
parameter.action("Search for and Join Room", function()
multihandler:findRoom()
end)
parameter.action("Join Room", function()
if other_ip then
multihandler:joinRoom(other_ip, other_port)
else
parameter.text("other_ip", "")
parameter.text("other_port", "")
print("Fill in the host's ip and port, then click Join Room again")
end
end)
end
-- This function gets called once every frame
function draw()
multihandler:update()
background(52, 52, 52, 255)
if multihandler.connected then
fill(255, 255, 255, 255)
rect(0, 0, WIDTH, HEIGHT/2+36)
fill(0, 0, 0, 255)
text(msg, WIDTH/2, HEIGHT/2+25)
showKeyboard()
if (#msgs > 10) then
for i=1, 10 do
msgs[i] = msgs[i+1]
end
msgs[11] = nil
end
fill(255, 255, 255, 255)
if (#msgs < 11) then
for i = 1, #msgs do
text(msgs[i], WIDTH/2, HEIGHT-(20*i))
end
end
else
fill(255, 94, 0, 255)
text("RULES OF THE CHAT ROOM", WIDTH/2, 500)
text("1.) Be nice. Cyberbullying is a crime.", WIDTH/2, 450)
text("2.) Spamming can hurt the socket connection. You will be kicked from the chat if you do so.", WIDTH/2, 400)
text("3.) Sending nil (blank) messages can also hurt the socket connection", WIDTH/2, 350)
end
end
function receiveData(d)
msgs[#msgs+1] = d
sound(DATA, "ZgJAOgAzQFRrUD9AMgRrPcgXCT4Ps4A+OgBAaEBAQEJCQC9d")
end
function keyboard(key)
if (key == BACKSPACE) then
msg = string.sub(msg, 1, -2)
elseif (key == RETURN) then
sendMsg(msg, Username)
msg = ""
else
msg = msg .. key
end
end
function sendMsg(toSend, tag)
msgs[#msgs+1] = tag .. ": " .. toSend
multihandler:sendData(tag .. ": " .. toSend)
sound(DATA, "ZgJAMABMQFRrUD9AMgRrPcgXCT4Ps4A+OgBAaEBAQEJCQC9d")
end
local socket = require("socket")
Multiuser = class()
function Multiuser:init(dcb, ccb)
self.my_ip, self.my_port = self:getLocalIP(), 5400
self.peer_ip, self.peer_port = nil, self.my_port
self.client = socket.udp()
self.client:settimeout(0)
self.connected = false
self.is_host = false
self.searching = false
self.dataCallback = dcb or function() end
self.connectedCallback = ccb or function() end
end
-- Returns this iPad's local ip
function Multiuser:getLocalIP()
local randomIP = "192.167.188.122"
local randomPort = "3102"
local randomSocket = socket.udp()
randomSocket:setpeername(randomIP,randomPort)
local localIP, somePort = randomSocket:getsockname()
randomSocket:close()
randomSocket = nil
return localIP
end
-- Set the connected status and call the connection callback if needed
function Multiuser:setConnectedVal(bool)
self.connected = bool
if self.connected then
self.connectedCallback()
end
end
function Multiuser:setHostVal(bool)
self.is_host = bool
end
-- Prepare to be the host
function Multiuser:hostRoom()
print("Connect to " .. self.my_ip .. ":" .. self.my_port)
self.client:setsockname(self.my_ip, self.my_port)
self:setConnectedVal(false)
self.is_host = true
self.searching = false
end
-- Find a host
function Multiuser:findRoom()
print("Searching for rooms...")
self.searching = true
local ip_start, ip_end = self.my_ip:match("(%d+.%d+.%d+.)(%d+)")
for i = 1, 255 do
if i ~= tonumber(ip_end) then
tween.delay(0.01 * i, function()
self.client:setsockname(ip_start .. i, self.my_port)
self.client:sendto("connection_confirmation", ip_start .. i, self.my_port)
end)
end
end
end
-- Prepare to join a host
function Multiuser:joinRoom(ip, port)
self.peer_ip, self.peer_port = ip, port
self.client:setsockname(ip, port)
self.is_host = false
self.searching = false
self:sendData("connection_confirmation")
end
-- Send data to the other client
function Multiuser:sendData(msg_to_send)
if self.peer_ip then
self.client:sendto(msg_to_send, self.peer_ip, self.peer_port)
end
end
-- Check for data received from the other client
function Multiuser:checkForReceivedData()
local data, msg_or_ip, port_or_nil = self.client:receivefrom()
if data then
-- Store the ip of this new client so you can send data back
self.peer_ip, self.peer_port = msg_or_ip, port_or_nil
if not self.connected and data == "connection_confirmation" then
self:sendData("connection_confirmation")
self:setConnectedVal(true)
end
-- Call callback with received data
if data ~= "connection_confirmation" then
self.dataCallback(data)
end
end
end
function Multiuser:update()
self:checkForReceivedData()
end