Skip to content

Example: Updating A Server Frequently

Seppi edited this page Apr 26, 2016 · 5 revisions

To ensure you are sending data to the server as quickly as possible, consider:

  • How often are you sending LoverNet's payload. (configure with :setClientTransmitRate())?
  • How much data is being sent. (Consider updating non-vital data less frequently.)?
  • How large are your function calls (e.g. a function named p will be smaller than a function named getNewPlayerPositions.)?
  • Are you sending data that can be easily compressed by the SerDes (default Bitser)?
  • How long does it take the server to process data? (Is there a timeout / validate to ensure that not too much information is processed. e.g., see: Example: Sending Latest Data To Server))

With these few things in mind, sending data can be very fast.

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

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{transmitRate=1/60}
lovernet:addOp('p')

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

Clone this wiki locally