87 lines
2.2 KiB
Go
87 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"DT/core/gorm"
|
|
"DT/core/logger"
|
|
"DT/core/viper"
|
|
"DT/global"
|
|
"DT/httpserver"
|
|
"DT/tcpserver"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
)
|
|
|
|
func init() {
|
|
viper.InitViper()
|
|
logger.InitLogger()
|
|
gorm.InitGorm()
|
|
//gorm.AutoMigrate(global.Db)
|
|
}
|
|
|
|
func main() {
|
|
handler := &tcpserver.TCPHandler{}
|
|
|
|
tcpHost := global.AppConf.Service.Tcp.Host
|
|
tcpPort := global.AppConf.Service.Tcp.Port
|
|
tcpServer := tcpserver.NewServer(tcpHost+":"+tcpPort, handler.HandleClient)
|
|
handler.Server = tcpServer
|
|
|
|
httpHost := global.AppConf.Service.Http.Host
|
|
httpPort := global.AppConf.Service.Http.Port
|
|
httpServer := httpserver.NewServer(httpHost+":"+httpPort, tcpServer)
|
|
|
|
handler.Hub = httpServer.GetHub()
|
|
|
|
go func() {
|
|
if err := tcpServer.Start(); err != nil {
|
|
fmt.Printf("TCP server error: %v\n", err)
|
|
}
|
|
}()
|
|
|
|
go func() {
|
|
fmt.Printf("Listening and serving HTTP on %s:%s\r\n", httpHost, httpPort)
|
|
if err := httpServer.Start(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
fmt.Printf("HTTP server error: %v\n", err)
|
|
}
|
|
}()
|
|
|
|
quit := make(chan os.Signal, 1)
|
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
|
<-quit
|
|
|
|
fmt.Println("Shutting down servers...")
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
if err := httpServer.Stop(ctx); err != nil {
|
|
fmt.Printf("Error stopping HTTP server: %v\n", err)
|
|
}
|
|
tcpServer.Stop()
|
|
fmt.Println("All servers stopped")
|
|
}
|
|
|
|
// 客户端发送 {"Type":"reg","Imei":"8610123456","Pwd":"123456","Ver":"v1.0.0"}
|
|
// 服务器回复 {"Type":"reg","Time":1734948442}
|
|
|
|
// 心跳
|
|
// 客户端{"Type":"ping"}
|
|
// 服务端{"Type":"pong"}
|
|
|
|
// 服务端发 {"Type":"ota","Ip":"192.168.31.1:80","File":"/xxx/1.bin"}
|
|
// 服务端发 {\"Type\":\"ota\",\"Ip\":\"192.168.31.1:80\",\"File\":\"/xxx/1.bin\"}
|
|
// 客户端回复 {"Type":"ota","State":"1"}
|
|
|
|
// 开始实时上传数据
|
|
// 服务端发 {"Type":"start"}
|
|
// 客户端回复 {"Type":"start","Data":"{\"Type\":\"ota\",\"Ip\":\"192.168.31.1:80\",\"File\":\"/xxx/1.bin\"}"}
|
|
// 客户端回复 {"Type":"start","Data":"{\"Type\":\"ota\",\"Ip\":\"192.168.31.1:80\",\"File\":\"/xxx/1.bin\"}"}
|
|
|
|
// 停止实时上传数据
|
|
// 服务端发 {"Type":"stop"}
|
|
// 客户端回复 {"Type":"stop","State":"1"}
|