websocket.go 1.5 KB

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