mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-21 04:04:43 +08:00
添加底层函数
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package GateService
|
package GateService
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duanhf2012/origin/event"
|
"github.com/duanhf2012/origin/event"
|
||||||
"github.com/duanhf2012/origin/network"
|
"github.com/duanhf2012/origin/network"
|
||||||
@@ -25,7 +26,8 @@ func (slf *GateService) OnInit() error{
|
|||||||
slf.httpRouter = sysservice.NewHttpHttpRouter(slf)
|
slf.httpRouter = sysservice.NewHttpHttpRouter(slf)
|
||||||
httpervice.SetHttpRouter(slf.httpRouter)
|
httpervice.SetHttpRouter(slf.httpRouter)
|
||||||
|
|
||||||
slf.httpRouter.RegRouter(sysservice.METHOD_GET,"/get/query",slf.HttpTest)
|
slf.httpRouter.GET("/get/query", slf.HttpTest)
|
||||||
|
slf.httpRouter.POST("/post/query", slf.HttpTestPost)
|
||||||
slf.httpRouter.SetServeFile(sysservice.METHOD_GET,"/img/head/","d:/img")
|
slf.httpRouter.SetServeFile(sysservice.METHOD_GET,"/img/head/","d:/img")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -38,6 +40,26 @@ func (slf *GateService) HttpTest(session *sysservice.HttpSession) {
|
|||||||
fmt.Print(string(session.GetBody()),"\n",v,"\n",v2)
|
fmt.Print(string(session.GetBody()),"\n",v,"\n",v2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (slf *GateService) HttpTestPost(session *sysservice.HttpSession) {
|
||||||
|
session.SetHeader("a","b")
|
||||||
|
v,_:=session.Query("a")
|
||||||
|
v2,_:=session.Query("b")
|
||||||
|
|
||||||
|
byteBody := session.GetBody()
|
||||||
|
fmt.Print(string(session.GetBody()),"\n",v,"\n",v2)
|
||||||
|
|
||||||
|
testa := struct {
|
||||||
|
AA int `json:"aa"`
|
||||||
|
BB string `json:"bb"`
|
||||||
|
}{}
|
||||||
|
json.Unmarshal(byteBody, &testa)
|
||||||
|
fmt.Println(testa)
|
||||||
|
|
||||||
|
testa.AA = 100
|
||||||
|
testa.BB = "this is a test"
|
||||||
|
session.WriteJson("asdasda")
|
||||||
|
}
|
||||||
|
|
||||||
func (slf *GateService) OnEventHandler(ev *event.Event) error{
|
func (slf *GateService) OnEventHandler(ev *event.Event) error{
|
||||||
if ev.Type == event.Sys_Event_Tcp_RecvPack {
|
if ev.Type == event.Sys_Event_Tcp_RecvPack {
|
||||||
pPack := ev.Data.(*sysservice.TcpPack)
|
pPack := ev.Data.(*sysservice.TcpPack)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package sysservice
|
package sysservice
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duanhf2012/origin/event"
|
"github.com/duanhf2012/origin/event"
|
||||||
"github.com/duanhf2012/origin/network"
|
"github.com/duanhf2012/origin/network"
|
||||||
@@ -51,7 +52,9 @@ type routerServeFileData struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type IHttpRouter interface {
|
type IHttpRouter interface {
|
||||||
RegRouter(method HTTP_METHOD, url string, handle HttpHandle) bool
|
//RegRouter(method HTTP_METHOD, url string, handle HttpHandle) bool
|
||||||
|
GET(url string, handle HttpHandle) bool
|
||||||
|
POST(url string, handle HttpHandle) bool
|
||||||
Router(session *HttpSession)
|
Router(session *HttpSession)
|
||||||
|
|
||||||
PutHttpSession(httpSession *HttpSession)
|
PutHttpSession(httpSession *HttpSession)
|
||||||
@@ -163,6 +166,15 @@ func (slf *HttpSession) Write(msg []byte) {
|
|||||||
slf.msg = msg
|
slf.msg = msg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (slf *HttpSession) WriteJson(msgJson interface{}) error {
|
||||||
|
msg, err := json.Marshal(msgJson)
|
||||||
|
if err == nil {
|
||||||
|
slf.Write(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func (slf *HttpSession) flush() {
|
func (slf *HttpSession) flush() {
|
||||||
slf.w.WriteHeader(slf.statusCode)
|
slf.w.WriteHeader(slf.statusCode)
|
||||||
if slf.msg!=nil {
|
if slf.msg!=nil {
|
||||||
@@ -215,7 +227,15 @@ func (slf *HttpRouter) PutHttpSession(httpSession *HttpSession){
|
|||||||
slf.eventReciver.NotifyEvent(&event.Event{Type:event.Sys_Event_Http_Event,Data:httpSession})
|
slf.eventReciver.NotifyEvent(&event.Event{Type:event.Sys_Event_Http_Event,Data:httpSession})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *HttpRouter) RegRouter(method HTTP_METHOD, url string, handle HttpHandle) bool{
|
func (slf *HttpRouter) GET(url string, handle HttpHandle) bool {
|
||||||
|
return slf.regRouter(METHOD_GET, url, handle)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (slf *HttpRouter) POST(url string, handle HttpHandle) bool {
|
||||||
|
return slf.regRouter(METHOD_POST, url, handle)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (slf *HttpRouter) regRouter(method HTTP_METHOD, url string, handle HttpHandle) bool{
|
||||||
mapRouter,ok := slf.pathRouter[method]
|
mapRouter,ok := slf.pathRouter[method]
|
||||||
if ok == false{
|
if ok == false{
|
||||||
return false
|
return false
|
||||||
|
|||||||
Reference in New Issue
Block a user