feat: 基本的websocket echo服务

This commit is contained in:
2025-12-17 12:03:06 +08:00
parent b824dc3792
commit 1bc9c6a924
11 changed files with 242 additions and 29 deletions

View File

@ -10,20 +10,24 @@ import (
"time"
"git.jinshen.cn/remilia/push-server/interval/api"
"git.jinshen.cn/remilia/push-server/interval/hub"
"git.jinshen.cn/remilia/push-server/interval/server"
"git.jinshen.cn/remilia/push-server/interval/ws"
)
func main() {
serverCtx, serverCancel := context.WithCancel(context.Background())
serverCtx, stop := signal.NotifyContext(
context.Background(),
os.Interrupt,
syscall.SIGTERM,
)
defer func() {
serverCancel()
stop()
}()
h := hub.NewHub()
h := ws.NewHub()
go h.Run(serverCtx)
httpServer := server.NewHTTPServer(":8080", api.NewRouter(h))
httpServer := server.NewHTTPServer(":8080", api.NewRouter(h, serverCtx))
go func() {
log.Println("Starting HTTP server on :8080")
@ -32,16 +36,14 @@ func main() {
}
}()
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
<-sig
<-serverCtx.Done()
log.Println("Shutting down server...")
serverCancel()
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), time.Second*10)
defer shutdownCancel()
httpServer.Shutdown(shutdownCtx)
if err := httpServer.Shutdown(shutdownCtx); err != nil {
log.Printf("HTTP server shutdown error: %v", err)
}
}