Implemented chatting room (#17)
This commit is contained in:
@@ -45,6 +45,13 @@ function TASK.removeTask_data(data)
|
||||
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()
|
||||
local i=#tasks
|
||||
while i>0 do
|
||||
|
||||
@@ -166,44 +166,6 @@ do--dumpTable
|
||||
return s..tabs[t-1].."}"
|
||||
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
|
||||
--
|
||||
-- json.lua
|
||||
@@ -564,7 +526,6 @@ do--json
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
do--urlencode
|
||||
local rshift=bit.rshift
|
||||
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
|
||||
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)
|
||||
local L={}
|
||||
for i=1,#org do
|
||||
|
||||
@@ -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_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_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={
|
||||
__index=function(L,k)
|
||||
|
||||
@@ -311,11 +311,13 @@ ACCOUNT={
|
||||
|
||||
PATH={
|
||||
api="/tech/api/v1",
|
||||
socket="/tech/socket/v1",
|
||||
appInfo="/app/info",
|
||||
users="/users",
|
||||
auth="/auth",
|
||||
access="/auth/access",
|
||||
versus="/online/versus"
|
||||
versus="/online/versus",
|
||||
chat="/chat",
|
||||
}
|
||||
|
||||
keyMap={
|
||||
|
||||
@@ -81,6 +81,8 @@ return{
|
||||
registerFailed="Registration failed",
|
||||
loginSuccessed="Successfully logged in!",
|
||||
loginFailed="Login failed",
|
||||
accessSuccessed="Successfully authorized!",
|
||||
accessFailed="Authorization failed",
|
||||
wsSuccessed="Websocket connected!",
|
||||
wsFailed="Websocket failed to connect",
|
||||
|
||||
|
||||
@@ -84,6 +84,8 @@ return{
|
||||
registerFailed="Erreur d'enregistrement",
|
||||
-- loginSuccessed="Successfully logged in!",
|
||||
-- loginFailed="Login failed",
|
||||
-- accessSuccessed="Successfully authorized!",
|
||||
-- accessFailed="Authorization failed",
|
||||
-- wsSuccessed="Websocket connected!",
|
||||
-- wsFailed="Websocket failed to connect",
|
||||
|
||||
|
||||
@@ -85,6 +85,8 @@ return{
|
||||
registerFailed="Registro fallido.",
|
||||
-- loginSuccessed="Successfully logged in!",
|
||||
-- loginFailed="Login failed",
|
||||
-- accessSuccessed="Successfully authorized!",
|
||||
-- accessFailed="Authorization failed",
|
||||
-- wsSuccessed="Websocket connected!",
|
||||
-- wsFailed="Websocket failed to connect",
|
||||
|
||||
|
||||
@@ -82,6 +82,8 @@ return{
|
||||
registerFailed="注册失败",
|
||||
loginSuccessed="登录成功!",
|
||||
loginFailed="登录失败",
|
||||
accessSuccessed="授权成功!",
|
||||
accessFailed="授权失败",
|
||||
wsSuccessed="连接成功!",
|
||||
wsFailed="连接失败",
|
||||
|
||||
|
||||
@@ -1,27 +1,8 @@
|
||||
local function send()
|
||||
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()
|
||||
local function socketWrite(message)
|
||||
if not WSCONN then
|
||||
LOG.print("尚未连接到服务器","warn")
|
||||
return
|
||||
end
|
||||
local message = WIDGET.active.message.value
|
||||
print("TextBox: "..message)
|
||||
local writeErr = client.write(WSCONN, message)
|
||||
if writeErr then
|
||||
print(writeErr, "warn")
|
||||
@@ -29,6 +10,21 @@ local function socketWrite()
|
||||
return true
|
||||
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.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},
|
||||
|
||||
@@ -35,7 +35,7 @@ end
|
||||
|
||||
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="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="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")},
|
||||
|
||||
@@ -145,8 +145,18 @@ function Tick.httpREQ_newLogin(data)
|
||||
ACCOUNT.email=res.email
|
||||
ACCOUNT.auth_token=res.auth_token
|
||||
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
|
||||
LOG.print(text.netErrorCode..response.code..": "..res.message,"warn")
|
||||
end
|
||||
@@ -169,6 +179,38 @@ function Tick.httpREQ_autoLogin(data)
|
||||
end
|
||||
else
|
||||
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)
|
||||
if err then
|
||||
LOG.print(text.netErrorCode..response.code..": "..err.message,"warn")
|
||||
@@ -181,8 +223,43 @@ function Tick.httpREQ_autoLogin(data)
|
||||
end
|
||||
return checkTimeout(data,360)
|
||||
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)
|
||||
print("Running wsconntask...")
|
||||
if data.wsconntask then
|
||||
local wsconn,connErr=client.poll(data.wsconntask)
|
||||
if wsconn then
|
||||
@@ -203,8 +280,7 @@ function Tick.wsCONN_read(data)
|
||||
local messages,readErr=client.read(WSCONN)
|
||||
if messages then
|
||||
if messages[1] then
|
||||
print(messages[1])
|
||||
LOG.print("Message: "..messages[1])
|
||||
LOG.print(messages[1])
|
||||
end
|
||||
elseif readErr then
|
||||
print("Read error: "..readErr)
|
||||
|
||||
Reference in New Issue
Block a user