Skynet 初探(2) 之 snax 版的 echo 复读机

main.lua

local skynet = require "skynet"
local snax = require "snax"


local function main(...)

    print("Skynet Server Starting....")
    local gate = snax.newservice "gateway"
    skynet.exit()

end



skynet.start(main)

gateway.lua

local skynet = require "skynet"
local socket = require "socket"
local snax = "snax"


local conf = {
    ip = "192.168.2.5",
    port = 80
}


function handle(id,addr)

    -- 初始化套接字的相关信息
    socket.start(id)
    socket.write(id,"welcome....\r\n")
    socket.write(id,"you:")
    while id do

        if socket.block(id) then  -- 检查是否有数据
            local buf = socket.readline(id)
            if buf then 
                socket.write(id,"Server :" .. buf .. "\r\nyou:")
            end
        else
            print "client close this socket fd.."
            socket.close(id)
            break
        end
    end
end


function init(...)

    print "gateway will init..."

    local id = socket.listen(conf.ip,conf.port)
    socket.start(id,handle)

end


function exit(...)

    print "gateway was exit..."
    snax.exit()

end

评论

暂无评论