Skip to content

Instantly share code, notes, and snippets.

@legumbre
Last active August 29, 2015 13:56
Show Gist options
  • Save legumbre/9224909 to your computer and use it in GitHub Desktop.
Save legumbre/9224909 to your computer and use it in GitHub Desktop.
knetwork Server and Client | love
-- client --
-- - Launch the server.
-- - Fire up repl, then try:
-- a_client:send({a="foo", b="bar"})
-- - server responds with:
-- {a="foo", b="bar", ECHO="ECHO!"}
require "repl"
local serpent= require "serpent"
local knet= require "knetwork"
function love.load()
a_client= knet.new_client()
a_client:connect("127.0.0.1", 10000)
end
function love.update(dt)
repl_t()
a_client:update()
for i, msg in pairs(a_client:receive()) do
print("CLIENT got message: " .. serpent.block(msg))
end
end
local serpent= require "serpent"
local knet= require "knetwork"
function love.load()
the_server= knet.new_server()
the_server:listen("127.0.0.1", 10000)
end
function love.update(dt)
the_server:update()
for client_id, client in pairs(the_server:clients()) do
local messages= client:receive()
for i, msg in pairs(messages) do
print("SERVER got message: " .. serpent.block(msg))
if type(msg) == 'table' then
msg.ECHO="ECHO!"
the_server:send(msg)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment