tech/game

21/07/14 UDP 프로토콜을 사용하여 P2P Game Control

tech-lover 2021. 7. 14. 17:46

- Lua Socket 

 

```

local socket = require("socket")

local widget= require ("widget")

local client= socket.connect("www.google.com"80)

 

local ip, port = client:getsockname()

udp=socket.udp()

udp:settimeout(0)

udp:setsockname(ip, port)

 

local data

 

ipp = display.newText("IP = "..ip, display.contentCenterX80, native.systemFont60)

portt = display.newText("PORT = "..port, display.contentCenterX160, native.systemFont60)

 

sendIP=native.newTextField(display.contentCenterX25050080)

sendPORT=native.newTextField(display.contentCenterX35050080)

sendIP.text=ip

sendPORT.text=port

 

function ButEvent(e)

    if (e.phase=="began") and (sendIP.text~=""then

        udpp=socket.udp()

        udpp:settimeout(0)

        udpp:setpeername(sendIP.texttonumber(sendPORT.text))

 

        local dg = e.target.id

        local snd = udpp:send(dg)

    end

end

 

upBut=widget.newButton{

    defaultFile = "buttonHelp.png",

    left=display.contentCenterX-120,

    top=display.contentCenterY-200,

    width = 240,

    height = 120,

    label="Up",

    id="up",

    onEvent=ButEvent,

}

 

ball = widget.newButton{

    left=100,

    top=260,

    width=100,

    height=100,

    defaultFile = "football.png",

    id="ball"

}

 

downBut=widget.newButton{

    defaultFile = "buttonHelp.png",

    left=display.contentCenterX-120,

    top=display.contentCenterY+200,

    width = 240,

    height = 120,

    label="Down",

    id="down",

    onEvent=ButEvent,

}

 

leftBut=widget.newButton{

    defaultFile = "buttonHelp.png",

    left=display.contentCenterX-120*3,

    top=display.contentCenterY,

    width = 240,

    height = 120,

    label="left",

    id="left",

    onEvent=ButEvent,

}

 

rightBut=widget.newButton{

    defaultFile = "buttonHelp.png",

    left=display.contentCenterX+120,

    top=display.contentCenterY,

    width = 240,

    height = 120,

    label="Right",

    id="right",

    onEvent=ButEvent,

}

 

local function receiveUdpMsg(e)

    print("R")

    data = udp:receive()

    if data ~= nil then

        if (data=="up")then ball.y=ball.y-10

            print("up")

        elseif (data=="down"then ball.y=ball.y+10

            print("Down")

        elseif (data=="left"then ball.x=ball.x-10

            print("left")

        elseif (data=="right"then ball.x=ball.x+10

            print("Right")

        end

    end

    socket.sleep(0.01)

    recv= timer.performWithDelay(100, receiveUdpMsg)

end

recv= timer.performWithDelay(100, receiveUdpMsg)

```

 

 

 

References

- https://coronalabs.com/blog/2014/09/23/tutorial-local-multiplayer-with-udptcp/

 

Tutorial: Local multiplayer with UDP/TCP | Corona Labs

Today's guest tutorial comes to you courtesy of Mark Steelman, founder of Steelman Games LLC. Mark is currently working on a turn-based RPG incorporating local multiplayer called Legend of Us Roleplaying Game. Before becoming an indie developer, Mark worke

coronalabs.com

 

- 참고 동영상

 

https://youtu.be/qVWOaRYpli0

 

References

http://underpop.online.fr/l/lua/luasocket/

 

LuaSocket: Network support for the Lua language

What is LuaSocket? LuaSocket is a Lua extension library that is composed by two parts: a C core that provides support for the TCP and UDP transport layers, and a set of Lua modules that add support for functionality commonly needed by applications that dea

underpop.online.fr