Difference between revisions of "Gideros Unite Framework"

From GiderosMobile
 
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
__NOTOC__
+
__TOC__
'''Supported platforms:''' [[File:Platform android.png]][[File:Platform ios.png]][[File:Platform mac.png]][[File:Platform pc.png]][[File:Platform html5.png]][[File:Platform winrt.png]][[File:Platform win32.png]][[File:Platform linux.png]]<br/>
+
=== Gideros Unite Framework ===
 +
The Gideros Unite framework allows a host (server) and clients to connect over a Local Area Network. Some demo code:
 +
 
 +
'''Get all connected devices'''
 +
<syntaxhighlight lang="lua">
 +
-- we can get all devices that are connected to our network
 +
local devices = {}
 +
serverlink:addEventListener("device", function(e)
 +
print(e.data.id, e.data.ip, e.data.host)
 +
devices[e.data.id] = {}
 +
devices[e.data.id].ip = e.data.ip
 +
devices[e.data.id].name = e.data.host
 +
end)
  
=== Gideros Unite Framework ===
+
serverlink:getDevices()
<youtube>https://www.youtube.com/watch?v=WBFLSz34I4I</youtube>
+
 
 +
-- add some methods, that could be called by other clients or server through network
 +
-- draw something
 +
serverlink:addMethod("draw", self.drawLine, self)
 +
-- end drawing
 +
serverlink:addMethod("end", self.stopDrawing, self)
 +
-- clear drawing
 +
serverlink:addMethod("clear", self.reset, self)
 +
 
 +
-- then you can call this methods when needed
 +
serverlink:callMethod("clear")
 +
serverlink:callMethod("draw", someX, someY)
 +
 
 +
-- or call method of specific device using its id
 +
serverlink:callMethodOf("clear", 112233)
 +
serverlink:callMethodOf("draw", someX, someY, 112233)
  
Gideros Unite framework provides a way to implement Multiplayer games. It uses LuaSocket to establish socket connections and even create server/client instances.
+
-- and when game is finished
 +
serverlink:close()
 +
</syntaxhighlight>
  
It provides the means of device discovery in Local Area Network, and allows to call methods of other devices through network.
+
'''Server example code'''
 +
<syntaxhighlight lang="lua">
 +
function onAccept(e)
 +
-- auto accept client with provided id
 +
serverlink:accept(e.data.id)
 +
end
  
About protocols, it is possible to use tcp, udp or both (by binding some method to tcp, if reliability is needed, and others to udp for faster data processing).
+
-- create a server instance
 +
serverlink = Server.new({username = 'myServer'})
 +
-- add event to monitor when new client wants to join
 +
serverlink:addEventListener('newClient', onAccept)
 +
-- start broadcasting to discover devices
 +
serverlink:startBroadcast()
  
==== Unite Package '''[[Media:Unite.zip|Unite.zip]]''' ====
+
-- and then before entering game logic
==== Gideros application project '''[[Media:DrawTogetherV2.zip|DrawTogetherV2.zip]]''' ====
+
-- if we are ready to play stop broadcasting
 +
serverlink:stopBroadcast()
 +
-- and start only listening to clients
 +
serverlink:startListening()
 +
</syntaxhighlight>
  
 +
'''Client example code'''
 
<syntaxhighlight lang="lua">
 
<syntaxhighlight lang="lua">
 +
function onJoin(e)
 +
-- auto connect to server with provided id
 +
serverlink:connect(e.data.id)
 +
end
 +
 +
-- create client instance
 +
serverlink = Client.new({username = 'IAmAClient'})
 +
-- create event to monitor when new server starts broadcasting
 +
serverlink:addEventListener('newServer', onJoin)
 +
 +
-- event to listen if server accepted our connection
 +
serverlink:addEventListener('onAccepted', function()
 +
print('server accepted our connection')
 +
end)
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
'''Game logic example''' (the game logic is the same for server and clients)
 +
<syntaxhighlight lang="lua">
 +
-- we can get all devices that are connected to our network
 +
local devices = {}
 +
serverlink:addEventListener('device', function(e)
 +
print(e.data.id, e.data.ip, e.data.host)
 +
devices[e.data.id] = {}
 +
devices[e.data.id].ip = e.data.ip
 +
devices[e.data.id].name = e.data.host
 +
end)
 +
serverlink:getDevices()
 +
 +
-- add some methods, that could be called by other clients or server through network
 +
-- draw something
 +
serverlink:addMethod('draw', self.drawLine, self)
 +
-- end drawing
 +
serverlink:addMethod('end', self.stopDrawing, self)
 +
-- clear drawing
 +
serverlink:addMethod('clear', self.reset, self)
 +
 +
-- then you can call these methods when needed
 +
serverlink:callMethod('clear')
 +
serverlink:callMethod('draw', someX, someY)
 +
 +
-- or call method of specific device using its id
 +
serverlink:callMethodOf('clear', 112233)
 +
serverlink:callMethodOf('draw', someX, someY, 112233)
 +
 +
-- when game is finished
 +
serverlink:close()
 +
</syntaxhighlight>
 +
 +
=== Using the Gideros Unite Framework ===
 +
ar2rsawseen 2012/07/25 Gideros Mobile, '''updated 2023/12/13 (V2)'''
 +
 +
<youtube>https://www.youtube.com/watch?v=WBFLSz34I4I</youtube>
 +
 +
Gideros Unite framework provides a way to implement Multiplayer games:
 +
* using LuaSocket to establish socket connections and create server/client instances
 +
* device discovery over Local Area Network
 +
* call methods on devices through the network
 +
* protocols: '''tcp''', '''udp''' or both. Binding some method to tcp if reliability is needed, and others to udp for faster data processing
 +
 +
You can download the Gideros Unite Framework here: '''[[Media:Unite.zip|Unite.zip]]'''
 +
 +
And a Gideros project: '''[[Media:DrawTogetherV2.zip|DrawTogetherV2.zip]]'''
 +
 +
=== Standard scenario ===
 +
This is a standard scenario that can be created using Gideros Unite framework:
 +
# Server starts broadcasting or skip to step 5, if all clients know server IP address
 +
# Client's start listening to servers
 +
# Client receives broadcast message from server, newServer event is initiated
 +
# Client autoconnects to server or user manually (by pushing button) connects to specific server
 +
# Server receives newClient event
 +
# Server accepts client automatically or user manually (by pushing button) accepts specific client
 +
# Client receives onAccept event
 +
# Implement your game logic here, where both clients and server can call methods on all devices or on one specific device in the network
 +
# When one of the clients becomes unreachable, all clients and server get onClientClose event
 +
# When server becomes unreachable, all clients get onServerClose event
 +
# When you are finished, close client or server using close method, which stops all timers, closes all connections and destroys instance
 +
 +
=== Framework ===
 +
*1 [[Unite Server Method list]]
 +
*2 [[Unite Client Method list]]
 +
*3 [[Unite Server Event list]]
 +
*4 [[Unite Client Event list]]
  
'''[[Multiplayer]]'''
+
{{Unite Framework}}
{{GIDEROS IMPORTANT LINKS}}
 

Latest revision as of 08:51, 26 August 2024

Gideros Unite Framework

The Gideros Unite framework allows a host (server) and clients to connect over a Local Area Network. Some demo code:

Get all connected devices

-- we can get all devices that are connected to our network
local devices = {}
serverlink:addEventListener("device", function(e)
	print(e.data.id, e.data.ip, e.data.host)
	devices[e.data.id] = {}
	devices[e.data.id].ip = e.data.ip
	devices[e.data.id].name = e.data.host
end)

serverlink:getDevices()

-- add some methods, that could be called by other clients or server through network
-- draw something
serverlink:addMethod("draw", self.drawLine, self)
-- end drawing
serverlink:addMethod("end", self.stopDrawing, self)
-- clear drawing
serverlink:addMethod("clear", self.reset, self)

-- then you can call this methods when needed
serverlink:callMethod("clear")
serverlink:callMethod("draw", someX, someY)

-- or call method of specific device using its id
serverlink:callMethodOf("clear", 112233)
serverlink:callMethodOf("draw", someX, someY, 112233)

-- and when game is finished
serverlink:close()

Server example code

function onAccept(e)
	-- auto accept client with provided id
	serverlink:accept(e.data.id)
end

-- create a server instance
serverlink = Server.new({username = 'myServer'})
-- add event to monitor when new client wants to join
serverlink:addEventListener('newClient', onAccept)
-- start broadcasting to discover devices
serverlink:startBroadcast()

-- and then before entering game logic
-- if we are ready to play stop broadcasting
serverlink:stopBroadcast()
-- and start only listening to clients
serverlink:startListening()

Client example code

function onJoin(e)
	-- auto connect to server with provided id
	serverlink:connect(e.data.id)
end

-- create client instance
serverlink = Client.new({username = 'IAmAClient'})
-- create event to monitor when new server starts broadcasting
serverlink:addEventListener('newServer', onJoin)

-- event to listen if server accepted our connection
serverlink:addEventListener('onAccepted', function()
	print('server accepted our connection')
end)

Game logic example (the game logic is the same for server and clients)

-- we can get all devices that are connected to our network
local devices = {}
serverlink:addEventListener('device', function(e)
	print(e.data.id, e.data.ip, e.data.host)
	devices[e.data.id] = {}
	devices[e.data.id].ip = e.data.ip
	devices[e.data.id].name = e.data.host
end)
serverlink:getDevices()

-- add some methods, that could be called by other clients or server through network
-- draw something
serverlink:addMethod('draw', self.drawLine, self)
-- end drawing
serverlink:addMethod('end', self.stopDrawing, self)
-- clear drawing
serverlink:addMethod('clear', self.reset, self)

-- then you can call these methods when needed
serverlink:callMethod('clear')
serverlink:callMethod('draw', someX, someY)

-- or call method of specific device using its id
serverlink:callMethodOf('clear', 112233)
serverlink:callMethodOf('draw', someX, someY, 112233)

-- when game is finished
serverlink:close()

Using the Gideros Unite Framework

ar2rsawseen 2012/07/25 Gideros Mobile, updated 2023/12/13 (V2)

Gideros Unite framework provides a way to implement Multiplayer games:

  • using LuaSocket to establish socket connections and create server/client instances
  • device discovery over Local Area Network
  • call methods on devices through the network
  • protocols: tcp, udp or both. Binding some method to tcp if reliability is needed, and others to udp for faster data processing

You can download the Gideros Unite Framework here: Unite.zip

And a Gideros project: DrawTogetherV2.zip

Standard scenario

This is a standard scenario that can be created using Gideros Unite framework:

  1. Server starts broadcasting or skip to step 5, if all clients know server IP address
  2. Client's start listening to servers
  3. Client receives broadcast message from server, newServer event is initiated
  4. Client autoconnects to server or user manually (by pushing button) connects to specific server
  5. Server receives newClient event
  6. Server accepts client automatically or user manually (by pushing button) accepts specific client
  7. Client receives onAccept event
  8. Implement your game logic here, where both clients and server can call methods on all devices or on one specific device in the network
  9. When one of the clients becomes unreachable, all clients and server get onClientClose event
  10. When server becomes unreachable, all clients get onServerClose event
  11. When you are finished, close client or server using close method, which stops all timers, closes all connections and destroys instance

Framework