Hi,
I’m newbie in corona and i want to connect 2 or 3 devices in local network with 1 device is the server and sharing hotspot to another devices.
Can i do this?
I use udp socket and it work well in simulator but i can’t connect in android devices
Here is my code:
=====
server
=====
local socket = require “socket”
local widget= require (“widget”)
ball = widget.newButton{left=100,top=160,width=25,height=25, id=“ball”}
local udp = socket.udp()
udp:settimeout(0)
udp:setsockname("*", 12345)
socket.bind("*", 12345)
local data, msg_or_ip, port_or_nil
local function receiveUdpMsg(event)
data = udp:receive()
if data ~= nil then
if (data==“up”)then ball.y=ball.y-10
elseif (data==“down”) then ball.y=ball.y+10
elseif (data==“left”) then ball.x=ball.x-10
elseif (data==“right”) then ball.x=ball.x+10
end
end
socket.sleep(0.01)
recv= timer.performWithDelay(100, receiveUdpMsg)
end
recv= timer.performWithDelay(100, receiveUdpMsg)
=====
client
=====
local socket = require(“socket”)
local widget= require (“widget”)
local address, port = “localhost”, 12345
function sendUdpMsg(message)
udp = socket.udp()
udp:settimeout(0)
udp:setpeername(address, port)
udp:send(message)
end
local function ButEvent(self,event)
if event.phase == “ended” then
if self.id == “up” then
sendUdpMsg(“up”)
ball.y=ball.y-10
elseif self.id == “down” then
sendUdpMsg(“down”)
ball.y = ball.y+10
elseif self.id == “left” then
sendUdpMsg(“left”)
ball.x=ball.x-10
elseif self.id == “right” then
sendUdpMsg(“right”)
ball.x=ball.x+10
end
end
end
ball = widget.newButton{left=100,top=160,width=25,height=25, id=“ball”, }
upBut=widget.newButton{left=100,top=60,width=55,height=25,label=“Up”,id=“up”}
downBut=widget.newButton{left=100,top=260,width=55,height=25,label=“Down”,id=“down”}
leftBut=widget.newButton{left=10,top=160,width=55,height=25,label=“left”,id=“left”}
rightBut=widget.newButton{left=200,top=160,width=55,height=25,label=“Right”,id=“right”}
upBut.touch = ButEvent
downBut.touch = ButEvent
leftBut.touch = ButEvent
rightBut.touch = ButEvent