Page 1 of 9

sock.lua - A simple networking library for LÖVE

Posted: Sat Aug 06, 2016 11:48 pm
by Ikroth
sock.lua

sock.lua is a networking library for LÖVE games. Its goal is to make getting started with networking as easy as possible.

Source Code: https://github.com/camchenry/sock.lua
Documentation: http://camchenry.com/sock.lua/
Examples: https://github.com/camchenry/sock.lua/t ... r/examples

Features
  • Event trigger system makes it easy to add behavior to network events.
  • Can send images and files over the network.
  • Can use a custom serialization library.
  • Logs events, errors, and warnings that occur.
Example

Code: Select all

-- client.lua
sock = require "sock"

function love.load()
    -- Creating a new client on localhost:22122
    client = sock.newClient("localhost", 22122)

    -- Creating a client to connect to some ip address
    client = sock.newClient("198.51.100.0", 22122)

    -- Called when a connection is made to the server
    client:on("connect", function(data)
        print("Client connected to the server.")
    end)

    -- Called when the client disconnects from the server
    client:on("disconnect", function(data)
        print("Client disconnected from the server.")
    end)

    -- Custom callback, called whenever you send the event from the server
    client:on("hello", function(msg)
        print("The server replied: " .. msg)
    end)

    client:connect()

    --  You can send different types of data
    client:send("greeting", "Hello, my name is Inigo Montoya.")
    client:send("isShooting", true)
    client:send("bulletsLeft", 1)
    client:send("position", {
        x = 465.3,
        y = 50,
    })
end

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

Code: Select all

-- server.lua
sock = require "sock"

function love.load()
    -- Creating a server on any IP, port 22122
    server = sock.newServer("*", 22122)

    -- Called when someone connects to the server
    server:on("connect", function(data, client)
        -- Send a message back to the connected client
        local msg = "Hello from the server!"
        client:send("hello", msg)
    end)
end

function love.update(dt)
    server:update()
end
Motivation
Writing code in lua-enet introduces a lot of boilerplate code that obfuscates your game code. Your game code becomes tied to your networking code, and it becomes difficult to reuse code between the client and server. sock tries to remedy this by taking on the task of serializing and sending data for you. It enables you to focus on the network code that matters.

Feedback needed
sock.lua is still a work in progress, but is already able to be used in games. However, there is a long way to go before it could be considered "complete."

  • How can this library be made more useful to you?
  • How can the documentation be improved?
[/b]
If you have comments, or need help, leave a reply here or send me a message on #love. Thanks.

Re: sock.lua - A simple networking library for LÖVE

Posted: Thu Aug 11, 2016 11:46 am
by Jack5500
Wow, this looks really well put together and like a valid alternative to lube.
Shame that I can't give any deep feedback, but from what I see on the surface and the documentation you did a really good job!

Re: sock.lua - A simple networking library for LÖVE

Posted: Thu Aug 11, 2016 2:36 pm
by Ortimh
Hat's off. Definitely a use. This could make my networking problem solved. It follows my naming conventions and tidy as heck (especially the doc), just the way I like it. No feedback from a newbie of networking stuff, but surely it's a great job from you!

Re: sock.lua - A simple networking library for LÖVE

Posted: Thu Aug 11, 2016 4:57 pm
by Ulydev
Looks like an amazing lib, can't wait to try it!

Re: sock.lua - A simple networking library for LÖVE

Posted: Thu Aug 11, 2016 6:25 pm
by Ikroth
Thanks for the feedback.

Right now, I'm working on support for custom serialization functions, so you're not forced to use bitser if you don't want to. You can probably expect to see it in the docs within a week or so.

The documentation still lacks some information, i.e. examples, usage info. There's also no information on how to use "data formats" (my own term, might change.) It's a cool feature that potentially saves a lot of bandwidth. Now, I just need to write my own Lua doc tool so I don't have to use LDoc anymore ;).

Re: sock.lua - A simple networking library for LÖVE

Posted: Fri Aug 12, 2016 2:16 pm
by whitebear
I am curious, what license is this under.

Re: sock.lua - A simple networking library for LÖVE

Posted: Fri Aug 12, 2016 7:06 pm
by Ikroth
whitebear wrote:I am curious, what license is this under.
It is licensed under the MIT license.

Re: sock.lua - A simple networking library for LÖVE

Posted: Tue Aug 16, 2016 1:11 am
by LordSeaworth
Hmm seems very usefull.
Was first thinking on using enet but if you keep this library alive with any upcoming love updates i'll deff gonne use this.

Keep up the good work

Re: sock.lua - A simple networking library for LÖVE

Posted: Wed Aug 17, 2016 4:45 am
by Sulunia
So, i just took this awesome library out for a spin.
One thing that i didn't know and took me some time to figure out: the server always receives an event, the data AND the peer that sent it.
So, if you want to send information specifically to a peer, you must use the peer the server receives on event trigger. It is pretty damn obvious now, but complete beginners to networking may have troubles.

I'll see if i can fix the ugly code around my simple "box moving" example with this lib and then share it here if people so wish.

Other than that, i suppose all "security" checks are done by the lib already no? So we only have to determine if the data received is valid or not to prevent cheating and so on...

Not sure if i was clear, say so if not. Otherwise, amazing library! Will definitely use it.

Re: sock.lua - A simple networking library for LÖVE

Posted: Wed Aug 17, 2016 5:27 am
by LordSeaworth
*snip*
Silly me, found the answer to my question in the example.