websocket 增加多分组 fork https://github.com/olahol/melody
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
2.3 KiB

10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
  1. package melody
  2. import (
  3. "errors"
  4. "github.com/gorilla/websocket"
  5. "net/http"
  6. "time"
  7. )
  8. // Session is wrapper around websocket connections.
  9. type Session struct {
  10. Request *http.Request
  11. conn *websocket.Conn
  12. output chan *envelope
  13. melody *Melody
  14. }
  15. func (s *Session) writeMessage(message *envelope) {
  16. select {
  17. case s.output <- message:
  18. default:
  19. s.melody.errorHandler(s, errors.New("Message buffer full"))
  20. }
  21. }
  22. func (s *Session) writeRaw(message *envelope) error {
  23. s.conn.SetWriteDeadline(time.Now().Add(s.melody.Config.WriteWait))
  24. err := s.conn.WriteMessage(message.t, message.msg)
  25. if err != nil {
  26. return err
  27. }
  28. if message.t == websocket.CloseMessage {
  29. err := s.conn.Close()
  30. if err != nil {
  31. return err
  32. }
  33. }
  34. return nil
  35. }
  36. func (s *Session) close() {
  37. s.writeRaw(&envelope{t: websocket.CloseMessage, msg: []byte{}})
  38. }
  39. func (s *Session) ping() {
  40. s.writeRaw(&envelope{t: websocket.PingMessage, msg: []byte{}})
  41. }
  42. func (s *Session) writePump() {
  43. defer s.conn.Close()
  44. ticker := time.NewTicker(s.melody.Config.PingPeriod)
  45. defer ticker.Stop()
  46. loop:
  47. for {
  48. select {
  49. case msg, ok := <-s.output:
  50. if !ok {
  51. s.close()
  52. break loop
  53. }
  54. if err := s.writeRaw(msg); err != nil {
  55. s.melody.errorHandler(s, err)
  56. break loop
  57. }
  58. case <-ticker.C:
  59. s.ping()
  60. }
  61. }
  62. }
  63. func (s *Session) readPump() {
  64. defer s.conn.Close()
  65. s.conn.SetReadLimit(s.melody.Config.MaxMessageSize)
  66. s.conn.SetReadDeadline(time.Now().Add(s.melody.Config.PongWait))
  67. s.conn.SetPongHandler(func(string) error {
  68. s.conn.SetReadDeadline(time.Now().Add(s.melody.Config.PongWait))
  69. s.melody.pongHandler(s)
  70. return nil
  71. })
  72. for {
  73. t, message, err := s.conn.ReadMessage()
  74. if err != nil {
  75. s.melody.errorHandler(s, err)
  76. break
  77. }
  78. if t == websocket.TextMessage {
  79. s.melody.messageHandler(s, message)
  80. }
  81. if t == websocket.BinaryMessage {
  82. s.melody.messageHandlerBinary(s, message)
  83. }
  84. }
  85. }
  86. // Write writes message to session.
  87. func (s *Session) Write(msg []byte) {
  88. s.writeMessage(&envelope{t: websocket.TextMessage, msg: msg})
  89. }
  90. // WriteBinary writes a binary message to session.
  91. func (s *Session) WriteBinary(msg []byte) {
  92. s.writeMessage(&envelope{t: websocket.BinaryMessage, msg: msg})
  93. }
  94. // Close closes a session.
  95. func (s *Session) Close() {
  96. s.writeMessage(&envelope{t: websocket.CloseMessage, msg: []byte{}})
  97. }