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.

119 lines
2.2 KiB

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
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
  1. package melody
  2. import (
  3. "errors"
  4. "github.com/gorilla/websocket"
  5. "net/http"
  6. "time"
  7. )
  8. // A melody session.
  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.writeMessage(&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. return nil
  70. })
  71. for {
  72. t, message, err := s.conn.ReadMessage()
  73. if err != nil {
  74. s.melody.errorHandler(s, err)
  75. break
  76. }
  77. if t == websocket.TextMessage {
  78. s.melody.messageHandler(s, message)
  79. }
  80. if t == websocket.BinaryMessage {
  81. s.melody.messageHandlerBinary(s, message)
  82. }
  83. }
  84. }
  85. // Write message to session.
  86. func (s *Session) Write(msg []byte) {
  87. s.writeMessage(&envelope{t: websocket.TextMessage, msg: msg})
  88. }
  89. // Write binary message to session.
  90. func (s *Session) WriteBinary(msg []byte) {
  91. s.writeMessage(&envelope{t: websocket.BinaryMessage, msg: msg})
  92. }
  93. // Close session.
  94. func (s *Session) Close() {
  95. s.writeMessage(&envelope{t: websocket.CloseMessage, msg: []byte{}})
  96. }