1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- package server
- import (
- "context"
- "fmt"
- "github.com/gogf/gf/net/gtcp"
- "github.com/gogf/gf/os/glog"
- gatewayV2 "sparrow-sdk/v2"
- )
- type Server struct {
- closeChan chan struct{}
- srv *gtcp.Server
- ctx context.Context
- addr string
- port int
- gateWay *gatewayV2.Gateway
- }
- func NewServer(ctx context.Context, addr string, port int, gw *gatewayV2.Gateway) *Server {
- return &Server{
- closeChan: make(chan struct{}),
- ctx: ctx,
- addr: addr,
- port: port,
- gateWay: gw,
- }
- }
- func (s *Server) Start() error {
- glog.Printf("服务端启动[%s:%d]", s.addr, s.port)
- srv := gtcp.NewServer(fmt.Sprintf("%s:%d", s.addr, s.port), s.onClientConnect)
- s.srv = srv
- return s.srv.Run()
- }
- func (s *Server) Stop() {
- s.srv.Close()
- }
- func (s *Server) onClientConnect(conn *gtcp.Conn) {
- glog.Debugf("新的设备接入:%s", conn.RemoteAddr())
- client := NewClient(s, conn)
- client.closeHandler = func(id string, c *Client) {
- glog.Debugf("客户端断开:%s", id)
- }
- go client.SendLoop()
- go client.GetSendByte()
- }
- func (s *Server) ReportStatus(subId string, data interface{}) error {
- return s.gateWay.ReportStatus(subId, "status", data)
- }
|