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

 

转载于:https://my.oschina.net/CandyMi/blog/831985

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值