websocket.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "net/http"
  6. "time"
  7. "github.com/DeanThompson/ginpprof"
  8. "github.com/gin-gonic/gin"
  9. "github.com/google/uuid"
  10. "github.com/gorilla/websocket"
  11. "github.com/redis/go-redis/v9"
  12. "sikey.com/websocket/config"
  13. "sikey.com/websocket/repositories"
  14. "sikey.com/websocket/server"
  15. "sikey.com/websocket/utils/mysqlx"
  16. "sikey.com/websocket/utils/zlog"
  17. )
  18. var configFile = flag.String("f", "./etc/websocket.toml", "the config file")
  19. func main() {
  20. flag.Parse()
  21. config.MustLoadConfig(*configFile)
  22. // Zaplog init
  23. zlog.WithZapLogger(zlog.NewLogger(config.MustLoadLogger()))
  24. app := newApp()
  25. app.Run(fmt.Sprintf(":%d", config.Config.Port))
  26. }
  27. func newApp() *gin.Engine {
  28. app := gin.Default()
  29. ginpprof.Wrap(app)
  30. srv := &server.Server{
  31. Upgrader: websocket.Upgrader{
  32. ReadBufferSize: 1024,
  33. WriteBufferSize: 1024,
  34. CheckOrigin: func(r *http.Request) bool {
  35. return true
  36. },
  37. },
  38. WriteWait: 10 * time.Second,
  39. ReadWait: 10 * time.Second,
  40. PingWait: 120 * time.Second,
  41. Hub: server.NewHub(server.HubConfig{
  42. ServerId: uuid.NewString(),
  43. Rdb: redis.NewUniversalClient(&redis.UniversalOptions{
  44. Addrs: []string{"106.75.230.4:6379"},
  45. Password: "sikey!Q@W#E456",
  46. DB: 0,
  47. }),
  48. ConnectSize: 1024,
  49. DisconnectSize: 1024,
  50. MessageSize: 125,
  51. }),
  52. Repositories: repositories.NewRepositories(mysqlx.ConnectMysql()),
  53. }
  54. app.GET("/websocket/endpoint", func(ctx *gin.Context) { server.WebsocketHandler(ctx, srv) })
  55. return app
  56. }