server.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package socketio
  2. import (
  3. "github.com/googollee/go-engine.io"
  4. "net/http"
  5. "time"
  6. )
  7. // Server is the server of socket.io.
  8. type Server struct {
  9. *namespace
  10. broadcast BroadcastAdaptor
  11. eio *engineio.Server
  12. }
  13. // NewServer returns the server supported given transports. If transports is nil, the server will use ["polling", "websocket"] as default.
  14. func NewServer(transportNames []string) (*Server, error) {
  15. eio, err := engineio.NewServer(transportNames)
  16. if err != nil {
  17. return nil, err
  18. }
  19. ret := &Server{
  20. namespace: newNamespace(newBroadcastDefault()),
  21. eio: eio,
  22. }
  23. go ret.loop()
  24. return ret, nil
  25. }
  26. // SetPingTimeout sets the timeout of a connection ping. When it times out, the server will close the connection with the client. Default is 60s.
  27. func (s *Server) SetPingTimeout(t time.Duration) {
  28. s.eio.SetPingTimeout(t)
  29. }
  30. // SetPingInterval sets the interval of pings. Default is 25s.
  31. func (s *Server) SetPingInterval(t time.Duration) {
  32. s.eio.SetPingInterval(t)
  33. }
  34. // SetMaxConnection sets the maximum number of connections with clients. Default is 1000.
  35. func (s *Server) SetMaxConnection(n int) {
  36. s.eio.SetMaxConnection(n)
  37. }
  38. // GetMaxConnection returns the current max connection
  39. func (s *Server) GetMaxConnection() int {
  40. return s.eio.GetMaxConnection()
  41. }
  42. // Count returns the current number of connected clients in session
  43. func (s *Server) Count() int {
  44. return s.eio.Count()
  45. }
  46. // LenRoom returns the current number of connected clients in room
  47. func (s *Server) LenRoom(room string) int {
  48. return s.namespace.broadcast.Len(room)
  49. }
  50. // SetAllowRequest sets the middleware function when a connection is established. If a non-nil value is returned, the connection won't be established. Default will allow all connections.
  51. func (s *Server) SetAllowRequest(f func(*http.Request) error) {
  52. s.eio.SetAllowRequest(f)
  53. }
  54. // SetAllowUpgrades sets whether server allows transport upgrades. Default is true.
  55. func (s *Server) SetAllowUpgrades(allow bool) {
  56. s.eio.SetAllowUpgrades(allow)
  57. }
  58. // SetCookie sets the name of the cookie used by engine.io. Default is "io".
  59. func (s *Server) SetCookie(prefix string) {
  60. s.eio.SetCookie(prefix)
  61. }
  62. // SetNewId sets the callback func to generate new connection id. By default, id is generated from remote address + current time stamp
  63. func (s *Server) SetNewId(f func(*http.Request) string) {
  64. s.eio.SetNewId(f)
  65. }
  66. // SetSessionsManager sets the sessions as server's session manager. Default sessions is a single process manager. You can customize it as a load balancer.
  67. func (s *Server) SetSessionManager(sessions engineio.Sessions) {
  68. s.eio.SetSessionManager(sessions)
  69. }
  70. // SetAdaptor sets the adaptor of broadcast. Default is an in-process broadcast implementation.
  71. func (s *Server) SetAdaptor(adaptor BroadcastAdaptor) {
  72. s.namespace = newNamespace(adaptor)
  73. }
  74. // ServeHTTP handles http requests.
  75. func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  76. s.eio.ServeHTTP(w, r)
  77. }
  78. // BroadcastTo is a server level broadcast function.
  79. func (s *Server) BroadcastTo(room, event string, args ...interface{}) {
  80. s.namespace.BroadcastTo(room, event, args...)
  81. }
  82. func (s *Server) loop() {
  83. for {
  84. conn, err := s.eio.Accept()
  85. if err != nil {
  86. return
  87. }
  88. s := newSocket(conn, s.baseHandler)
  89. go func(s *socket) {
  90. s.loop()
  91. }(s)
  92. }
  93. }