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

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
Ikroth
Citizen
Posts: 79
Joined: Thu Jul 18, 2013 4:44 am

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

Post 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.
Last edited by Ikroth on Sat Dec 17, 2016 10:13 pm, edited 1 time in total.
User avatar
Jack5500
Party member
Posts: 149
Joined: Wed Dec 07, 2011 8:38 pm
Location: Hamburg, Germany

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

Post 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!
User avatar
Ortimh
Citizen
Posts: 90
Joined: Tue Sep 09, 2014 5:07 am
Location: Indonesia

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

Post 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!
User avatar
Ulydev
Party member
Posts: 445
Joined: Mon Nov 10, 2014 10:46 pm
Location: Paris
Contact:

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

Post by Ulydev »

Looks like an amazing lib, can't wait to try it!
User avatar
Ikroth
Citizen
Posts: 79
Joined: Thu Jul 18, 2013 4:44 am

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

Post 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 ;).
User avatar
whitebear
Citizen
Posts: 86
Joined: Sun Mar 15, 2009 1:50 am

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

Post by whitebear »

I am curious, what license is this under.
User avatar
Ikroth
Citizen
Posts: 79
Joined: Thu Jul 18, 2013 4:44 am

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

Post by Ikroth »

whitebear wrote:I am curious, what license is this under.
It is licensed under the MIT license.
User avatar
LordSeaworth
Prole
Posts: 22
Joined: Tue Jun 07, 2016 10:29 pm

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

Post 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
User avatar
Sulunia
Party member
Posts: 203
Joined: Tue Mar 22, 2016 1:10 pm
Location: SRS, Brazil

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

Post 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.
Don't check my github! It contains thousands of lines of spaghetti code in many different languages cool software! :neko:
https://github.com/Sulunia
User avatar
LordSeaworth
Prole
Posts: 22
Joined: Tue Jun 07, 2016 10:29 pm

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

Post by LordSeaworth »

*snip*
Silly me, found the answer to my question in the example.
Post Reply

Who is online

Users browsing this forum: No registered users and 37 guests