Skynet基礎入門例子詳解(6)

把socket控制權交給其他服務

socket.abandon(id) 清除 socket id 在本服務內的數據結構,但並不關閉這個 socket 。這可以用於你把 id 發送給其它服務,以轉交 socket 的控制權。

在同一個目錄建立7個文件(config,proto.lua,main.lua,socket1.lua,client1.lua,agent1.lua,agent2.lua)

本節例子與上一節例子主要區別是: 
socket1服務不處理接收到的消息,交給了agent處理

socket1.lua代碼:

local skynet = require "skynet"
require "skynet.manager"    -- import skynet.register
local socket = require "socket"

local function accept1(id)
    socket.start(id)
    socket.write(id, "Hello Skynet\n")
    skynet.newservice("agent1", id)
    -- notice: Some data on this connection(id) may lost before new service start.
    -- So, be careful when you want to use start / abandon / start .
    socket.abandon(id)
end


local function accept2(id)
    socket.start(id)
    local agent2 = skynet.newservice("agent2")
    skynet.call(agent2,"lua",id)

    -- socket.abandon(id)清除 socket id 在本服務內的數據結構,但並不關閉這個 socket 。這可以用於你把 id 發送給其它服務,以轉交 socket 的控制權。
    socket.abandon(id)
end

skynet.start(function()
    print("==========Socket Start=========")
    local id = socket.listen("127.0.0.1", 8888)
    print("Listen socket :", "127.0.0.1", 8888)

    socket.start(id , function(id, addr)
            -- 接收到客戶端連接或發送消息()
            print("connect from " .. addr .. " " .. id)

            -- 處理接收到的消息(交給angent1或agent2處理)
            -- accept1(id)
            accept2(id)
        end)
    --可以爲自己註冊一個別名。(別名必須在 32 個字符以內)
    skynet.register "SOCKET1"
end)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

agent1.lua代碼:

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

local fd = ...

fd = tonumber(fd)

local function echo(id)
    socket.start(id)

    while true do
        local str = socket.read(id)
        if str then
            print("client say:"..str)
            -- socket.write(id, str)
        else
            socket.close(id)
            return
        end
    end
end

skynet.start(function()
    skynet.fork(function()
        echo(fd)
        skynet.exit()
    end)
end)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

agent2.lua代碼:

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

local proto = require "proto"
local sproto = require "sproto"

local host

local REQUEST = {}

function REQUEST:say()
    print("say", self.name, self.msg)
end

function REQUEST:handshake()
    print("handshake")
end

function REQUEST:quit()
    print("quit")
end

local function request(name, args, response)
    local f = assert(REQUEST[name])
    local r = f(args)
    if response then
        -- 生成迴應包(response是一個用於生成迴應包的函數。)
        -- 處理session對應問題
        -- return response(r)
    end
end

local function echo(id)
    socket.start(id)

    host = sproto.new(proto.c2s):host "package"

    while true do
        local str = socket.read(id)
        if str then
            local type,str2,str3,str4 = host:dispatch(str)

            if type=="REQUEST" then
                local ok, result  = pcall(request, str2,str3,str4)
                -- print("client say:"..str)
            end

            -- socket.write(id, str)
        else
            socket.close(id)
            return
        end
    end
end

skynet.start(function()
    skynet.dispatch("lua", function(session, address, fd, ...)

        skynet.fork(function()
            echo(fd)
            skynet.exit()
        end)

    end)    
end)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章