<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] ## 概述 引用云風博客的話:"skynet 是一個為網絡游戲服務器設計的輕量框架。但它本身并沒有任何為網絡游戲業務而特別設計的部分,所以盡可以把它用于其它領域。skynet 并不是一個開箱即用的引擎,使用它需要先對框架本身的結構有所了解,理解框架到底幫助開發者解決怎樣的問題。如果你希望使用這個框架來開發網絡游戲服務器,你將發現,skynet 并不會引導你把服務器搭建起來。它更像是一套工具,只有你知道你想做什么,它才會幫助你更有效率的完成。" 詳情請參考:https://github.com/cloudwu/skynet/wiki/GettingStarted ## 下載 & 編譯 安裝環境: * ubuntu16.04 * gcc5.4 * make 從[github](https://github.com/cloudwu/skynet)上下載 ```shell $ git clone https://github.com.cnpmjs.org/cloudwu/skynet.git $ cd skynet && make linux ``` ## 解析示例 啟動一個服務端和客戶端示例: ```shell ./skynet examples/config # Launch first skynet node (Gate server) and a skynet-master (see config for standalone option) ./3rd/lua/lua examples/client.lua # Launch a client, and try to input hello. ``` 我是一個skynet小白,在我第一次看到官方示例的時候,只能看懂語法,但是不知道其中的邏輯關系。根據以往學習項目的經驗,我先研究的怎么使用,然后再看源代碼。在網上找到這個教程:https://blog.csdn.net/119365374/article/details/77790653, 寫一個`echo`,可以很好的理解skynet的使用。 寫`echo`服務的腳本 ```lua -- echo.lua local skynet = require("skynet") require ("skynet.manager") local command = {} function command.HELLO(what) return "I am echo server: ".. what end skynet.start(function() skynet.dispatch("lua", function(session, address, cmd, ...) cmd = cmd:upper() if cmd == "HELLO" then local f = command[cmd] assert(f) skynet.ret(skynet.pack(f(...))) end end) -- skynet.register("echo") --這句可以不要,已經驗證過 end) ``` `skynet.dispatch("lua", function(session, address, cmd, ...) end)`三個參數解釋 * **sesson**: 請求序列號,是一個自增的id,溢出了又從1開始 * **address**:是skynet中服務的地址,這個地址在運行時是唯一的。在上面的代碼中就是代表echo服務自已的地址。它實際上也是一個數字。 * **cmd**: 請求命令 調用`echo`服務的test_echo.lua腳本 ```lua -- test_echo.lua local skynet = require "skynet" skynet.start(function () local echo = skynet.newservice("echo") print(skynet.call(echo, "lua", "HELLO", "WORLD")) end) ``` 調用函數`skynet.call(echo, "lua", "HELLO", "WORLD")`中四個參數的解釋, * **echo**: 調用服務的地址,與上面的**address**對應; * **HELLO**: 請求命令,與上面的**cmd**對應; * **WORLD**:請求參數; * call是阻塞式調用。 寫配置文件 `examples/config.echo` ```config -- config.echo include "config.path" -- preload = "./examples/preload.lua" -- run preload.lua before every lua service run thread = 8 logger = nil logpath = "." harbor = 1 address = "127.0.0.1:2526" master = "127.0.0.1:2013" start = "test_echo" -- 指定啟動的腳本名字 bootstrap = "snlua bootstrap" -- The service for bootstrap standalone = "0.0.0.0:2013" -- snax_interface_g = "snax_g" cpath = root.."cservice/?.so" -- daemon = "./skynet.pid" ``` 運行: ``` ./skynet examples/config.echo ``` 這個例子讓我們學習了`skynet.start`、`skynet.dispatch`、`skynet.register`、skynet.call、skynet.newservice這幾個重要的方法 ## 改造成C/S架構的echo 了解了一下skynet的網絡模型是actor模型,這樣寫起服務來更加得心應手了。 一個`echo_server.lua` 監聽服務 ```lua local skynet = require "skynet" local socket = require("skynet.socket") local echo function accept(id, addr) print("accept connect from addr: ".. addr .. "; id: ".. id) print(skynet.call(echo, "lua", "RWING", id)) end skynet.start(function() -- 讀寫的服務 echo = skynet.newservice("echo_rw") -- 監聽8883端口 local listen_id = socket.listen("0.0.0.0", 8883) -- 開始監聽 socket.start(listen_id, accept) end) ``` 讀寫服務 `echo_rw.lua` ```lua local skynet = require("skynet") local socket = require("skynet.socket") require ("skynet.manager") local command = {} -- 這個函數才是 echo函數 接收客戶端的數據,并且把數據原封不動的發回給客戶端 function command.RWING(id) print("start id: " .. id) socket.start(id) while true do -- 讀取客戶端的數據 local msg = socket.read(id, nil) if msg then print("recv msg: ".. msg) socket.write(id, msg) else socket.close(id) return end end end skynet.start(function() skynet.dispatch("lua", function(session, address, cmd, ...) cmd = cmd:upper() if cmd == "RWING" then local f = command[cmd] assert(f) skynet.ret(skynet.pack(f(...))) --這個是模仿skynet例子里寫的 end end) skynet.register("echo_rw") end) ``` 配置文件 `config.echo` ```config include "config.path" -- preload = "./examples/preload.lua" -- run preload.lua before every lua service run thread = 8 logger = nil logpath = "." harbor = 1 address = "127.0.0.1:2526" master = "127.0.0.1:2013" start = "echo_server" -- echo_server script 主要是改 腳本名字 bootstrap = "snlua bootstrap" -- The service for bootstrap standalone = "0.0.0.0:2013" -- snax_interface_g = "snax_g" cpath = root.."cservice/?.so" -- daemon = "./skynet.pid" ``` 啟動 ```shell ./skynet examples/config.echo ``` 附上一個客戶端測試一下服務器 ```lua package.cpath = "luaclib/?.so" package.path = "lualib/?.lua;examples/?.lua" if _VERSION ~= "Lua 5.4" then error "Use lua 5.4" end local socket = require("client.socket") local fd = assert(socket.connect("127.0.0.1", 8883)) local function sendpkg(fd, pack) local package = string.pack(">s2", pack) print("send msg: " .. package) socket.send(fd, package) end local function recvpkg(fd) local msg = socket.recv(fd) if not msg then return end if msg == "" then return end print("recv from server msg: " .. msg) end function main() sendpkg(fd, "hello world I'm Allen!!!") --發送消息 while true do recvpkg(fd) --接收消息,寫在循環里 end end main() ``` 這是我入門skynet框架寫的一個小demo,到這里就結束了,希望能對剛接觸skynet的同學有所幫助。對了,skynet是使用actor作為通信模型,了解了actor之后可以更好的理解skynet。 ## 學習項目 https://github.com/liuhaopen/SkynetMMO https://github.com/cloudwu/skynet/wiki/GettingStarted
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看