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

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
Мэтю
Prole
Posts: 32
Joined: Mon Jan 06, 2014 1:24 pm
Location: Espírito Santo, Brazil
Contact:

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

Post by Мэтю »

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 sent

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
simplified it and it still doesn't work..

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
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.

Code: Select all

bitser = require "bitser" -- or any library you like
server = sock.newServer("localhost", 22122)
server:setSerialization(bitser.dumps, bitser.loads)
World needs love.
Post Reply

Who is online

Users browsing this forum: No registered users and 44 guests