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.
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
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?
If you have comments, or need help, leave a reply here or send me a message on #love. Thanks.