golang-im聊天
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ws_server.go 1.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package connect
  2. import (
  3. "gim/pkg/logger"
  4. "gim/pkg/util"
  5. "io"
  6. "net/http"
  7. "strings"
  8. "time"
  9. "go.uber.org/zap"
  10. "github.com/gorilla/websocket"
  11. )
  12. var upgrader = websocket.Upgrader{
  13. ReadBufferSize: 1024,
  14. WriteBufferSize: 65536,
  15. CheckOrigin: func(r *http.Request) bool {
  16. return true
  17. },
  18. }
  19. // StartWSServer 启动WebSocket服务器
  20. func StartWSServer(address string) {
  21. http.HandleFunc("/ws", wsHandler)
  22. logger.Logger.Info("websocket server start")
  23. err := http.ListenAndServe(address, nil)
  24. if err != nil {
  25. panic(err)
  26. }
  27. }
  28. func wsHandler(w http.ResponseWriter, r *http.Request) {
  29. wsConn, err := upgrader.Upgrade(w, r, nil)
  30. if err != nil {
  31. logger.Sugar.Error(err)
  32. return
  33. }
  34. conn := &Conn{
  35. CoonType: ConnTypeWS,
  36. WS: wsConn,
  37. }
  38. DoConn(conn)
  39. }
  40. // DoConn 处理连接
  41. func DoConn(conn *Conn) {
  42. defer util.RecoverPanic()
  43. for {
  44. err := conn.WS.SetReadDeadline(time.Now().Add(12 * time.Minute))
  45. if err != nil {
  46. HandleReadErr(conn, err)
  47. return
  48. }
  49. _, data, err := conn.WS.ReadMessage()
  50. if err != nil {
  51. HandleReadErr(conn, err)
  52. return
  53. }
  54. conn.HandleMessage(data)
  55. }
  56. }
  57. // HandleReadErr 读取conn错误
  58. func HandleReadErr(conn *Conn, err error) {
  59. logger.Logger.Debug("read tcp error:", zap.Int64("user_id", conn.UserId),
  60. zap.Int64("device_id", conn.DeviceId), zap.Error(err))
  61. str := err.Error()
  62. // 服务器主动关闭连接
  63. if strings.HasSuffix(str, "use of closed network connection") {
  64. return
  65. }
  66. conn.Close()
  67. // 客户端主动关闭连接或者异常程序退出
  68. if err == io.EOF {
  69. return
  70. }
  71. // SetReadDeadline 之后,超时返回的错误
  72. if strings.HasSuffix(str, "i/o timeout") {
  73. return
  74. }
  75. }