This commit is contained in:
aozhiwei 2020-10-22 14:57:07 +08:00
parent b018d8712e
commit 0913b3f3e6
3 changed files with 80 additions and 11 deletions

View File

@ -1,9 +1,7 @@
package main
import "q5"
import "f5"
import "fmt"
import "time"
type App_ struct {
f5.App_
@ -13,17 +11,16 @@ var App *App_
func (this *App_) Init() {
f5.App = &this.App_
f5.App.SetPkgName("analyseapi")
this.App_.Init()
G.MetaMgr = &MetaMgr{}
G.MetaMgr = new(MetaMgr)
G.MetaMgr.Init()
f5.Timer().AddRepeatTimer(
1000 * 5,
func (params *q5.XParams) {
},
func (params *q5.XParams) {
fmt.Println("hello", time.Now())
})
G.MetaMgr.Load()
G.GCListener = new(GCListener)
G.GCListener.Init()
fmt.Println(
G.MetaMgr.GetConf().GetGameHeartbeat()[0],
G.MetaMgr.GetServer(1))
}
func (this *App_) UnInit() {

View File

@ -2,6 +2,7 @@ package main
type GlobalVar struct {
MetaMgr *MetaMgr
GCListener *GCListener
}
var G *GlobalVar = &GlobalVar{}

View File

@ -0,0 +1,71 @@
package main
import (
"net/http"
"fmt"
"sync"
"q5"
"f5"
)
type GCListener struct {
handlersMutex sync.RWMutex
handlers map[string]func(http.ResponseWriter, *http.Request)
}
func (this *GCListener) Init() {
this.handlers = make(map[string]func(http.ResponseWriter, *http.Request))
fmt.Println("GCListener.Init\n")
http.HandleFunc("/webapp/index.php", this.dispatchRequest)
this.RegisterHandle("Ops", "selfChecking", func(w http.ResponseWriter,r *http.Request) {
w.Write([]byte(`{"errcode":0, "errmsg":"", "health":1, "max_rundelay": 10}`))
})
this.RegisterHandle("Stat", "updateSession", func(w http.ResponseWriter,r *http.Request) {
f5.App.AddIMMsg(IM_UPDATE_SESSION, new(q5.XParams).Init(func (params *q5.XParams){
params.Sender.SetString(q5.Request(r, "account_id").GetString())
params.Param1.SetString(q5.Request(r, "session_id").GetString())
params.Param2.SetString(q5.GetRequestRemoteAddr(r))
params.Param3.SetString(q5.Request(r, "count").GetString())
}))
w.Write([]byte(`{"errcode":0, "errmsg":""`))
})
this.RegisterHandle("Stat", "getRealTimeOnline", func(w http.ResponseWriter,r *http.Request) {
onlineNum := G.StatMgr.GetRealTimeOnline(
q5.Request(r, "game_id").GetInt32(),
q5.Request(r, "channel").GetInt32())
w.Write([]byte(fmt.Sprintf(`{"errcode":0, "errmsg":"", "num":%d}`, onlineNum)))
})
go this.listenAndServe(G.MetaMgr.GetServer(1).GetListenPort())
}
func (this *GCListener) UnInit() {
fmt.Println("GCListener.UnInit\n")
}
func (this *GCListener) listenAndServe(listen_port int32) {
http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", listen_port), nil)
}
func (this *GCListener) dispatchRequest(w http.ResponseWriter, r *http.Request) {
handleName := q5.Request(r, "c").GetString() + "$" + q5.Request(r, "a").GetString()
handle := this.getHandle(handleName)
if handle != nil {
handle(w, r)
} else {
w.Write([]byte(`{"errcode":404, "errmsg":"接口不存在"}`))
}
}
func (this *GCListener) getHandle(handleName string) (func(http.ResponseWriter, *http.Request)) {
this.handlersMutex.Lock()
defer this.handlersMutex.Unlock()
handle, _ := this.handlers[handleName]
return handle
}
func (this *GCListener) RegisterHandle(c string, a string, handle func(http.ResponseWriter, *http.Request)) {
this.handlersMutex.Lock()
defer this.handlersMutex.Unlock()
handleName := c + "$" + a
this.handlers[handleName] = handle
}