Home
Ratchet Library :: Error Handling Example
API  ·  Manual

Error Handling

require "ratchet"

function ctx1(host, port)
    local rec = ratchet.socket.prepare_tcp(host, port)
    local socket = ratchet.socket.new(rec.family, rec.socktype, rec.protocol)
    socket:set_timeout(5.0)

    local worked, err = pcall(socket.connect, socket, rec.addr)
    if err:is("ETIMEDOUT") then
        print("Log: connection to "..host..":"..port.." timed out.")
    elseif err:is("ECONNREFUSED") then
        print("Log: connection to "..host..":"..port.." refused.")
    elseif not worked then
        print("Log: connection failed: "..tostring(err))
    end
end

kernel = ratchet.new(function ()
    ratchet.thread.attach(ctx1, "localhost", 10025)
end)
kernel:loop()


 

require "ratchet"

function ctx1(host, port)
    local rec = ratchet.socket.prepare_tcp(host, port)
    local socket = ratchet.socket.new(rec.family, rec.socktype, rec.protocol)
    socket:set_timeout(5.0)

    socket:connect(rec.addr)
end

function error_handler(err, thread)
    if err.type == "ratchet_error" then
        if err:is("ETIMEDOUT") then
            print("Log: connection timed out.")
        elseif err:is("ECONNREFUSED") then
            print("Log: connection refused.")
        else
            print(debug.traceback(thread, tostring(err), 2))
            os.exit(1)
        end
    else
        print(debug.traceback(thread, err, 2))
        os.exit(1)
    end
end

kernel = ratchet.new(function ()
    ratchet.thread.attach(ctx1, "localhost", 10025)
end, error_handler)

kernel:loop()



Last modified:  Sun, 17 Aug 2014 09:32:32 -0400
Author:  icgood