实现一个简单的留言板

#examples/Pconfig

include "config.path"
 
-- preload = "./examples/preload.lua"	-- run preload.lua before every lua service run
-- 必须配置
thread = 8                              -- 启用多少个工作线程
cpath = "./cservice/?.so"               -- 用C编写的服务模块的位置
bootstrap = "snlua bootstrap"           -- (固定)启动的第一个服务
 
-- bootstrap 配置项
start = "Pmain"                         -- 主服务入口
harbor = 0                              -- (固定)不适应主从节点
 
-- lua 配置项(暂时固定)
lualoader = "./lualib/loader.lua"
luaservice = "./service/?.lua;".."./test/?.lua;".."./examples/?.lua;".."./test/?/init.lua"
lua_path = "./lualib/?.lua;".."./lualib/?/init.lua"
lua_cpath = "./luaclib/?.so"
 
-- 后台模式
-- deamon = "./skynet.pid"
-- logger = "./userlog"

#examples/Pmain

local skynet = require "skynet"
local socket = require "skynet.socket"
local mysql  = require "skynet.db.mysql"

local db = nil

local clients = {}

skynet.start(function()
    -- 监听网络
    local listenfd = socket.listen("0.0.0.0",9001)
    socket.start(listenfd, connect)

    -- 连接
    db = mysql.connect({
        host            = "127.0.0.1",
        port            = 3306,
        database        = "skynet",
        user            = "skynet",
        password        = "skynet",
        max_packet_size = 1024 * 1024,
        on_connect      = nil
    })
end)

function connect(fd, addr)
    -- 启动连接
    print(fd.." connected addr:"..addr)
    socket.start(fd)
    clients[fd] = {}

    socket.write(fd, "Welcome to Skynet..\r\n")

    -- 消息处理
    while true do
        local readdata = socket.read(fd)
        -- 正常接受
        if readdata ~= nil then
            -- 返回留言板内容
            elseif readdata == "get\r\n" then
                -- 查询
                res = db:query("select * from msgs")
                -- 打印
                for i,v in pairs(res) do
                    socket.write(fd, v.id.."\t"..v.text.."\r\n")
                end
            --留言
            else
                -- 插入
                local data = string.match( readdata, "set (.-)\r\n" )
                db:query("insert into msgs (text) values (\'"..data.."\')")
            end
        -- 断开连接
        else
            print(fd.."close ")
            socket.close(fd)
            clients[fd] = nil
        end
    end
end

Q.E.D.