Skip to content

Example: Sending Latest Data To Server

Seppi edited this page Apr 26, 2016 · 2 revisions

To send the server the latest data that is available from a user, we send the data (thus clearing the stack) so that only the latest entry is sent when lovernet updates.

In this example, we will inform the server of the current mouse position.

For the server:

lovernetlib = require('lovernet')
lovernet = lovernetlib.new{type=lovernetlib.mode.server}
lovernet:addOp('p') -- (p)oint .. use single character to reduce bandwidth
lovernet:addValidateOnServer('p',{x='number',y='number'})
lovernet:addProcessOnServer('p',function(self,peer,arg,storage)
  local user = self:getUser(peer)
  user.x, user.y = arg.x, arg.y
end)

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

For the client:

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

function love.update(dt)
  lovernet:sendData('p',{
    x=love.mouse.getX(),
    y=love.mouse.getY(),
  })
  lovernet:update(dt)
end