go_dreamfactory/modules/gateway/wservice_comp.go
2022-06-28 14:51:58 +08:00

48 lines
1.1 KiB
Go

package gateway
import (
"net/http"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/core/cbase"
"go_dreamfactory/lego/sys/gin"
"go_dreamfactory/lego/sys/gin/engine"
"go_dreamfactory/lego/sys/log"
"github.com/gorilla/websocket"
)
type WSServiceComp struct {
cbase.ModuleCompBase
options *Options
module IGateway
gin gin.ISys
}
func (this *WSServiceComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
err = this.ModuleCompBase.Init(service, module, comp, options)
this.options = options.(*Options)
this.module = module.(IGateway)
this.gin, err = gin.NewSys(gin.SetListenPort(this.options.ListenPort))
this.gin.GET("/gateway", this.ws)
return
}
func (this *WSServiceComp) ws(c *engine.Context) {
upGrader := websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
if wsConn, err := upGrader.Upgrade(c.Writer, c.Request, nil); err != nil {
log.Errorf("accept faile client:%s err:%v", c.RemoteIP(), err)
return
} else {
agent := newAgent(this.module, wsConn)
this.module.Connect(agent)
}
}