1 Star 1 Fork 1

龙先生/gameServer

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
client.lua 2.05 KB
一键复制 编辑 原始数据 按行查看 历史
龙先生 提交于 2020-09-27 17:35 . 初始化
package.cpath = "luaclib/?.so"
package.path = "lualib/?.lua;examples/?.lua"
if _VERSION ~= "Lua 5.3" then
error "Use lua 5.3"
end
local socket = require "client.socket"
local proto = require "proto"
local sproto = require "sproto"
local host = sproto.new(proto.s2c):host "package"
local request = host:attach(sproto.new(proto.c2s))
local fd = assert(socket.connect("127.0.0.1", 8010))
local function send_package(fd, pack)
local package = string.pack(">s2", pack)
socket.send(fd, package)
end
local function unpack_package(text)
local size = #text
if size < 2 then
return nil, text
end
local s = text:byte(1) * 256 + text:byte(2)
if size < s+2 then
return nil, text
end
return text:sub(3,2+s), text:sub(3+s)
end
local function recv_package(last)
local result
result, last = unpack_package(last)
if result then
return result, last
end
local r = socket.recv(fd)
if not r then
return nil, last
end
if r == "" then
error "Server closed"
end
return unpack_package(last .. r)
end
local session = 0
local function send_request(name, args)
session = session + 1
local str = request(name, args, session)
send_package(fd, str)
print("Request:", session)
end
local last = ""
local function print_request(name, args)
print("REQUEST", name)
if args then
for k,v in pairs(args) do
print(k,v)
end
end
end
local function print_response(session, args)
print("RESPONSE", session)
if args then
for k,v in pairs(args) do
print(k,v)
end
end
end
local function print_package(t, ...)
if t == "REQUEST" then
print_request(...)
else
assert(t == "RESPONSE")
print_response(...)
end
end
local function dispatch_package()
while true do
local v
v, last = recv_package(last)
if not v then
break
end
print_package(host:dispatch(v))
end
end
send_request("handshake")
send_request("set", { what = "hello", value = "world" })
while true do
dispatch_package()
local cmd = socket.readstdin()
if cmd then
if cmd == "quit" then
send_request("quit")
else
send_request("get", { what = cmd })
end
else
socket.usleep(100)
end
end
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Lua
1
https://gitee.com/DragonSir/game-server.git
git@gitee.com:DragonSir/game-server.git
DragonSir
game-server
gameServer
master

搜索帮助