Implemented chatting room (#17)

This commit is contained in:
Particle_G
2020-12-03 16:09:25 +08:00
committed by MrZ626
parent 325c544e17
commit 37d7dd5fe0
11 changed files with 189 additions and 66 deletions

View File

@@ -45,6 +45,13 @@ function TASK.removeTask_data(data)
end end
end end
end end
function TASK.checkTask_code(code)
for i=#tasks,1,-1 do
if tasks[i].code==code then
return true
end
end
end
function TASK.clear() function TASK.clear()
local i=#tasks local i=#tasks
while i>0 do while i>0 do

View File

@@ -166,44 +166,6 @@ do--dumpTable
return s..tabs[t-1].."}" return s..tabs[t-1].."}"
end end
end end
do--httpRequest
client=LOADLIB("NETlib")
httpRequest=
client and function(tick,path,method,header,body)
local task,err=client.httpraw{
url="http://krakens.tpddns.cn:10026"..path,
method=method or"GET",
header=header,
body=body,
}
if task then
TASK.new(tick,{task=task,time=0,net=true})
else
LOG.print("NETlib error: "..err,"warn")
end
TASK.netTaskCount=TASK.netTaskCount+1
end or
function()
LOG.print("[NO NETlib]",5,COLOR.yellow)
end
wsConnect=
client and function(tick,path,header)
local task,err=client.wsraw{
url="http://krakens.tpddns.cn:10026"..path,
header=header,
}
if task then
TASK.new(tick,{wsconntask=task,time=0,net=true})
else
LOG.print("NETlib error: "..err,"warn")
end
TASK.netTaskCount=TASK.netTaskCount+1
end or
function()
LOG.print("[NO NETlib]",5,COLOR.yellow)
end
end
do--json do--json
-- --
-- json.lua -- json.lua
@@ -564,7 +526,6 @@ do--json
end end
end end
end end
do--urlencode do--urlencode
local rshift=bit.rshift local rshift=bit.rshift
local b16={[0]="0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"} local b16={[0]="0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"}
@@ -581,6 +542,48 @@ do--urlencode
return out return out
end end
end end
do--httpRequest
client=LOADLIB("NETlib")
httpRequest=
client and function(tick,path,method,header,body)
local task,err=client.httpraw{
url="http://krakens.tpddns.cn:10026"..path,
-- url="http://127.0.0.1:10026"..path,
method=method or"GET",
header=header,
body=body,
}
if task then
TASK.new(tick,{task=task,time=0,net=true})
else
LOG.print("NETlib error: "..err,"warn")
end
TASK.netTaskCount=TASK.netTaskCount+1
end or
function()
LOG.print("[NO NETlib]",5,COLOR.yellow)
end
wsConnect=
client and function(tick,path,header)
local task,err=client.wsraw{
url="ws://krakens.tpddns.cn:10026"..path,
origin="krakens.tpddns.cn",
-- url="ws://127.0.0.1:10026"..path,
-- origin="127.0.0.1",
header=header,
}
if task then
TASK.new(tick,{wsconntask=task,time=0,net=true})
else
LOG.print("NETlib error: "..err,"warn")
end
TASK.netTaskCount=TASK.netTaskCount+1
end or
function()
LOG.print("[NO NETlib]",5,COLOR.yellow)
end
end
function copyList(org) function copyList(org)
local L={} local L={}
for i=1,#org do for i=1,#org do

View File

@@ -806,6 +806,37 @@ function WIDGET.lnk_STPeq(k,v) return function() return sceneTemp[k]==v end e
function WIDGET.lnk_pressKey(k) return function() love.keypressed(k) end end function WIDGET.lnk_pressKey(k) return function() love.keypressed(k) end end
function WIDGET.lnk_goScene(t,s) return function() SCN.go(t,s) end end function WIDGET.lnk_goScene(t,s) return function() SCN.go(t,s) end end
function WIDGET.lnk_swapScene(t,s) return function() SCN.swapTo(t,s) end end function WIDGET.lnk_swapScene(t,s) return function() SCN.swapTo(t,s) end end
function WIDGET.lnk_goNetgame()
if LOGIN then
if ACCOUNT.access_token then
local res=json.encode{
email=ACCOUNT.email,
access_token=ACCOUNT.access_token,
}
httpRequest(
TICK.httpREQ_checkAccessToken,
PATH.api..PATH.access,
"GET",
{["Content-Type"]="application/json"},
res
)
else
local payload=json.encode{
email=ACCOUNT.email,
auth_token=ACCOUNT.auth_token,
}
httpRequest(
TICK.httpREQ_getAccessToken,
PATH.api..PATH.access,
"POST",
{["Content-Type"]="application/json"},
payload
)
end
else
SCN.go("login")
end
end
local indexMeta={ local indexMeta={
__index=function(L,k) __index=function(L,k)

View File

@@ -311,11 +311,13 @@ ACCOUNT={
PATH={ PATH={
api="/tech/api/v1", api="/tech/api/v1",
socket="/tech/socket/v1",
appInfo="/app/info", appInfo="/app/info",
users="/users", users="/users",
auth="/auth", auth="/auth",
access="/auth/access", access="/auth/access",
versus="/online/versus" versus="/online/versus",
chat="/chat",
} }
keyMap={ keyMap={

View File

@@ -81,6 +81,8 @@ return{
registerFailed="Registration failed", registerFailed="Registration failed",
loginSuccessed="Successfully logged in!", loginSuccessed="Successfully logged in!",
loginFailed="Login failed", loginFailed="Login failed",
accessSuccessed="Successfully authorized!",
accessFailed="Authorization failed",
wsSuccessed="Websocket connected!", wsSuccessed="Websocket connected!",
wsFailed="Websocket failed to connect", wsFailed="Websocket failed to connect",

View File

@@ -84,6 +84,8 @@ return{
registerFailed="Erreur d'enregistrement", registerFailed="Erreur d'enregistrement",
-- loginSuccessed="Successfully logged in!", -- loginSuccessed="Successfully logged in!",
-- loginFailed="Login failed", -- loginFailed="Login failed",
-- accessSuccessed="Successfully authorized!",
-- accessFailed="Authorization failed",
-- wsSuccessed="Websocket connected!", -- wsSuccessed="Websocket connected!",
-- wsFailed="Websocket failed to connect", -- wsFailed="Websocket failed to connect",

View File

@@ -85,6 +85,8 @@ return{
registerFailed="Registro fallido.", registerFailed="Registro fallido.",
-- loginSuccessed="Successfully logged in!", -- loginSuccessed="Successfully logged in!",
-- loginFailed="Login failed", -- loginFailed="Login failed",
-- accessSuccessed="Successfully authorized!",
-- accessFailed="Authorization failed",
-- wsSuccessed="Websocket connected!", -- wsSuccessed="Websocket connected!",
-- wsFailed="Websocket failed to connect", -- wsFailed="Websocket failed to connect",

View File

@@ -82,6 +82,8 @@ return{
registerFailed="注册失败", registerFailed="注册失败",
loginSuccessed="登录成功!", loginSuccessed="登录成功!",
loginFailed="登录失败", loginFailed="登录失败",
accessSuccessed="授权成功!",
accessFailed="授权失败",
wsSuccessed="连接成功!", wsSuccessed="连接成功!",
wsFailed="连接失败", wsFailed="连接失败",

View File

@@ -1,27 +1,8 @@
local function send() local function socketWrite(message)
local W=WIDGET.active.text
--sendMessage(W.value)
W.value=""
end
function sceneInit.chat()
BG.set("none")
end
local function socketConnect()
wsConnect(
TICK.wsCONN_connect,
"/solo?room_id=114",
{}
)
end
local function socketWrite()
if not WSCONN then if not WSCONN then
LOG.print("尚未连接到服务器","warn") LOG.print("尚未连接到服务器","warn")
return return
end end
local message = WIDGET.active.message.value
print("TextBox: "..message)
local writeErr = client.write(WSCONN, message) local writeErr = client.write(WSCONN, message)
if writeErr then if writeErr then
print(writeErr, "warn") print(writeErr, "warn")
@@ -29,6 +10,21 @@ local function socketWrite()
return true return true
end end
local function send()
local W=WIDGET.active.text
socketWrite(W.value)
W.value=""
end
function sceneInit.chat()
BG.set("none")
wsConnect(
TICK.wsCONN_connect,
PATH.socket..PATH.chat.."?email="..urlEncode(ACCOUNT.email).."&access_token="..urlEncode(ACCOUNT.access_token),
{}
)
end
WIDGET.init("chat",{ WIDGET.init("chat",{
WIDGET.newTextBox{name="text", x=40, y=500,w=980,h=180,font=40}, WIDGET.newTextBox{name="text", x=40, y=500,w=980,h=180,font=40},
WIDGET.newButton{name="send", x=1140, y=540,w=170,h=80,font=40,code=send}, WIDGET.newButton{name="send", x=1140, y=540,w=170,h=80,font=40,code=send},

View File

@@ -35,7 +35,7 @@ end
WIDGET.init("main",{ WIDGET.init("main",{
WIDGET.newButton{name="offline",x=150,y=220,w=200,h=140,color="lRed", font=40,code=WIDGET.lnk_goScene("mode")}, WIDGET.newButton{name="offline",x=150,y=220,w=200,h=140,color="lRed", font=40,code=WIDGET.lnk_goScene("mode")},
WIDGET.newButton{name="online", x=370,y=220,w=200,h=140,color="lCyan", font=40,code=function()SCN.go(LOGIN and"netgame"or"login")end}, WIDGET.newButton{name="online", x=370,y=220,w=200,h=140,color="lCyan", font=40,code=WIDGET.lnk_goNetgame},
WIDGET.newButton{name="custom", x=590,y=220,w=200,h=140,color="lBlue", font=40,code=WIDGET.lnk_goScene("customGame")}, WIDGET.newButton{name="custom", x=590,y=220,w=200,h=140,color="lBlue", font=40,code=WIDGET.lnk_goScene("customGame")},
WIDGET.newButton{name="setting",x=150,y=380,w=200,h=140,color="lOrange",font=40,code=WIDGET.lnk_goScene("setting_game")}, WIDGET.newButton{name="setting",x=150,y=380,w=200,h=140,color="lOrange",font=40,code=WIDGET.lnk_goScene("setting_game")},
WIDGET.newButton{name="stat", x=370,y=380,w=200,h=140,color="lGreen", font=40,code=WIDGET.lnk_goScene("stat")}, WIDGET.newButton{name="stat", x=370,y=380,w=200,h=140,color="lGreen", font=40,code=WIDGET.lnk_goScene("stat")},

View File

@@ -145,8 +145,18 @@ function Tick.httpREQ_newLogin(data)
ACCOUNT.email=res.email ACCOUNT.email=res.email
ACCOUNT.auth_token=res.auth_token ACCOUNT.auth_token=res.auth_token
FILE.save(ACCOUNT,"account","") FILE.save(ACCOUNT,"account","")
SCN.pop()
SCN.go("netgame") local payload=json.encode{
email=ACCOUNT.email,
auth_token=ACCOUNT.auth_token,
}
httpRequest(
TICK.httpREQ_getAccessToken,
PATH.api..PATH.access,
"POST",
{["Content-Type"]="application/json"},
payload
)
else else
LOG.print(text.netErrorCode..response.code..": "..res.message,"warn") LOG.print(text.netErrorCode..response.code..": "..res.message,"warn")
end end
@@ -169,6 +179,38 @@ function Tick.httpREQ_autoLogin(data)
end end
else else
LOGIN=false LOGIN=false
local err=json.decode(response.body)
if err then
LOG.print(text.loginFailed..": "..text.netErrorCode..response.code.."-"..err.message,"warn")
end
end
return true
elseif request_error then
LOG.print(text.loginFailed..": "..request_error,"warn")
return true
end
return checkTimeout(data,360)
end
function Tick.httpREQ_checkAccessToken(data)
local response,request_error=client.poll(data.task)
if response then
if response.code==200 then
LOG.print(text.accessSuccessed)
SCN.pop()
SCN.go("netgame")
elseif response.code==403 or response.code==401 then
local payload=json.encode{
email=ACCOUNT.email,
auth_token=ACCOUNT.auth_token,
}
httpRequest(
TICK.httpREQ_getAccessToken,
PATH.api..PATH.access,
"POST",
{["Content-Type"]="application/json"},
payload
)
else
local err=json.decode(response.body) local err=json.decode(response.body)
if err then if err then
LOG.print(text.netErrorCode..response.code..": "..err.message,"warn") LOG.print(text.netErrorCode..response.code..": "..err.message,"warn")
@@ -181,8 +223,43 @@ function Tick.httpREQ_autoLogin(data)
end end
return checkTimeout(data,360) return checkTimeout(data,360)
end end
function Tick.httpREQ_getAccessToken(data)
local response,request_error=client.poll(data.task)
if response then
if response.code==200 then
local res=json.decode(response.body)
if res then
LOG.print(text.accessSuccessed)
ACCOUNT.access_token=res.access_token
FILE.save(ACCOUNT,"account","")
SCN.pop()
SCN.go("netgame")
else
LOG.print(text.netErrorCode..response.code..": "..res.message,"warn")
SCN.pop()
SCN.go("main")
end
else
LOGIN=false
ACCOUNT.access_token=nil
ACCOUNT.auth_token=nil
local err=json.decode(response.body)
if err then
LOG.print(text.loginFailed..": "..text.netErrorCode..response.code.."-"..err.message,"warn")
else
LOG.print(text.loginFailed..": "..text.netErrorCode,"warn")
end
SCN.pop()
SCN.go("main")
end
return true
elseif request_error then
LOG.print(text.loginFailed..": "..request_error,"warn")
return true
end
return checkTimeout(data,360)
end
function Tick.wsCONN_connect(data) function Tick.wsCONN_connect(data)
print("Running wsconntask...")
if data.wsconntask then if data.wsconntask then
local wsconn,connErr=client.poll(data.wsconntask) local wsconn,connErr=client.poll(data.wsconntask)
if wsconn then if wsconn then
@@ -203,8 +280,7 @@ function Tick.wsCONN_read(data)
local messages,readErr=client.read(WSCONN) local messages,readErr=client.read(WSCONN)
if messages then if messages then
if messages[1] then if messages[1] then
print(messages[1]) LOG.print(messages[1])
LOG.print("Message: "..messages[1])
end end
elseif readErr then elseif readErr then
print("Read error: "..readErr) print("Read error: "..readErr)