go_dreamfactory/modules/gateway/wservice_comp.go
2022-05-31 16:35:44 +08:00

47 lines
1.2 KiB
Go

package gateway
import (
"net/http"
"github.com/gorilla/websocket"
"github.com/liwei1dao/lego/core"
"github.com/liwei1dao/lego/core/cbase"
"github.com/liwei1dao/lego/sys/gin"
"github.com/liwei1dao/lego/sys/gin/engine"
"github.com/liwei1dao/lego/sys/log"
)
type WSService_Comp struct {
cbase.ModuleCompBase
options *Options
module IGateway
gin gin.ISys
}
func (this *WSService_Comp) 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 *WSService_Comp) 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)
}
}