main.lua
1local skynet = require "skynet" 2local snax = require "snax" 3 4 5local function main(...) 6 7 print("Skynet Server Starting....") 8 local gate = snax.newservice "gateway" 9 skynet.exit() 10 11end 12 13 14 15skynet.start(main)
gateway.lua
1local skynet = require "skynet" 2local socket = require "socket" 3local snax = "snax" 4 5 6local conf = { 7 ip = "192.168.2.5", 8 port = 80 9} 10 11 12function handle(id,addr) 13 14 -- 初始化套接字的相关信息 15 socket.start(id) 16 socket.write(id,"welcome....\r\n") 17 socket.write(id,"you:") 18 while id do 19 20 if socket.block(id) then -- 检查是否有数据 21 local buf = socket.readline(id) 22 if buf then 23 socket.write(id,"Server :" .. buf .. "\r\nyou:") 24 end 25 else 26 print "client close this socket fd.." 27 socket.close(id) 28 break 29 end 30 end 31end 32 33 34function init(...) 35 36 print "gateway will init..." 37 38 local id = socket.listen(conf.ip,conf.port) 39 socket.start(id,handle) 40 41end 42 43 44function exit(...) 45 46 print "gateway was exit..." 47 snax.exit() 48 49end