Skip to content

Example: Queuing Data To Server

Seppi edited this page Apr 26, 2016 · 1 revision

LoverNet decides when data should be sent and received over the network, but as a user this is transparent. To queue data, you will treat the sent data as a stack.

In this example, we will inform the server of every mouse press.

For the server:

lovernetlib = require('lovernet')
lovernet = lovernetlib.new{type=lovernetlib.mode.server}
lovernet:addOp('c') -- (c)lick .. use single character to reduce bandwidth
lovernet:addValidateOnServer('c',{x='number',y='number',b='number'})
lovernet:addProcessOnServer('c',function(self,peer,arg,storage)
  local user = self:getUser(peer)
  user.clicks = user.clicks or {}
  table.insert(user.clicks,{
    x=arg.x,
    y=arg.y,
    button=arg.b,
  })
end)

function love.update(dt)
  lovernet:update(dt)
end

For the client:

lovernetlib = require('lovernet')
lovernet = lovernetlib.new()
lovernet:addOp('c')

function love.mousepressed(x,y,button)
  lovernet:pushData('c',{x=x,y=y,b=button})
end

function love.update(dt)
  lovernet:update(dt)
end