Re: sock.lua - A simple networking library for LÖVE
Posted: Thu Jul 26, 2018 1:18 pm
You didn't set the serialization and deserialization functions which is needed by the library to send data across network. Try using bitser, as exemplified in the documentation.Shadowblitz16 wrote: ↑Wed Jul 25, 2018 10:10 pm what am i doing wrong? I keep getting error during service and can;'t serialize message: serialize was not sentsimplified it and it still doesn't work..Code: Select all
net = require "lib.sock" local id = "" local joined = false; local server = net.newServer("localhost", 50301) local client = net.newClient("localhost", 50302) local messages = {} local message = "" function clientConnect(id) clientSendMessage(id, "has connected") joined = true end function clientSendMessage(id, message) client:send("message", {id, message}) end function serverGetMessage(id, message) server:sendToAll("message", {id, message}) end function clientGetMessage(id, message) message = "" table.insert(messages, message) end function love.load(dt) server:on("message", serverGetMessage(id, message)) client:on("message", clientGetMessage(id, message)) client:connect() end function love.keypressed(key, scancode, isrepeat) if joined == false then if key == "return" then clientConnect(id) elseif key == "space" then id = id .. " " else id = id .. key end else if key == "return" then clientSendMessage(id, message) elseif key == "space" then message = message .. " " else message = message .. key end end end function love.update() server:update() client:update() end function love.draw() love.graphics.clear(0,0,0,1) love.graphics.setColor(0.1,0.1,0.1,1) love.graphics.rectangle("fill",0,480-16,640,480) love.graphics.setColor(1,1,1,1) if joined == false then love.graphics.print(id, 16,480-16,0,1,1,0,0) elseif joined == true then love.graphics.print(message,16,480-16,0,1,1,0,0) end for i,v in ipairs(messages) do love.graphics.print(v, 16,480-(i*16),0,1,1,0,0) end end
Code: Select all
net = require "lib.sock" local str = "" local server = net.newServer("localhost", 50301) local client = net.newClient("localhost", 50302) server:on("connect", function(message) server:sendToAll("connect", message) end) client:on("connect", function(message) str = message end) client:connect() client:send("connect", "hello") function love.update() client:update() server:update() end function love.draw() love.graphics.print(str,320,240,0,1,1,0,0) end
Code: Select all
bitser = require "bitser" -- or any library you like
server = sock.newServer("localhost", 22122)
server:setSerialization(bitser.dumps, bitser.loads)