golang長串連

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

設計思路:每個websocket允許的串連都是有時間限制的,逾時後服務端會自動中斷連線,那麼長串連就在服務端發出中斷連線資訊後用戶端檢測斷開資訊再次發起串連請求,中間再通過握手資訊確保用戶端同伺服器處於串連狀態。

設計結構:

 

[plain] view plain copy  
  1. type Longsocket struct {  
  2.     Ws         *websocket.Conn  
  3.     writeCh    chan []byte  
  4.     readCh     chan []byte  
  5.     ShakeHand  bool  
  6.     Url        string  
  7.     Protocol   string  
  8.     Origin     string  
  9.     BufferSize int  
  10.     Status     int  
  11.     mu         sync.Mutex  
  12. }  


長串連通過`writeCh`通道主動向串連方發送訊息,通過`ReadCh`通道讀取串連中的資訊,設定`ShakeHand`來確定是否要發送握手資訊,Status用以標識串連狀態。

 

 

通過WriteLoop來發送握手資訊,同時監聽`WriteCh`通道,轉寄通道裡的訊息。

 

[plain] view plain copy  
  1. //call func with a gorouting, it will send shake hands message to service to make sure self is ok  
  2. //if you want to send message call func 'Write', and the case writeCh will be vaild  
  3. func (l *Longsocket) WriteLoop() {  
  4.     defer func() {  
  5.         if err := recover(); err != nil {  
  6.             //fmt.Println("writeloop", err)  
  7.         }  
  8.     }()  
  9.   
  10.     for {  
  11.         errCount := 0  
  12.         if l.Status != STATUS_CONNECT {  
  13.             break  
  14.         }  
  15.         select {  
  16.         case <-time.After(time.Second * time.Duration(SHAKE_HANDS_FREQUENCY)):  
  17.             if l.ShakeHand {  
  18.                 _, err := l.Ws.Write([]byte(SHAKE_HANDS_MSG))  
  19.                 if err != nil {  
  20.                     errCount++  
  21.                 }  
  22.             }  
  23.         case msg := <-l.writeCh:  
  24.             _, err := l.Ws.Write(msg)  
  25.             if err != nil {  
  26.                 errCount++  
  27.             }  
  28.         }  
  29.   
  30.         if errCount != 0 {  
  31.             break  
  32.         }  
  33.     }  
  34.     l.Close()  
  35. }  


通過ReadLoop來接受資訊,同時將訊息轉寄到`ReadCh`通道內。

 

 

[plain] view plain copy  
  1. //read message form socket and write them to readCh  
  2. func (l *Longsocket) ReadLoop() {  
  3.     defer func() {  
  4.         if err := recover(); err != nil {  
  5.             //fmt.Println("readloop", err)  
  6.         }  
  7.     }()  
  8.   
  9.     for {  
  10.         if l.Status != STATUS_CONNECT {  
  11.             break  
  12.         }  
  13.         buf := make([]byte, l.BufferSize)  
  14.         n, err := l.Ws.Read(buf)  
  15.         if err != nil {  
  16.             break  
  17.         }  
  18.   
  19.         if n > 0 {  
  20.             l.readCh <- buf[0:n]  
  21.         }  
  22.     }  
  23.     l.Close()  
  24. }  


然後可以通過Read函數將訊息轉寄到形如

 

type dealmsg func([]byte, *Longsocket) error

 

的函數中去做相應的訊息處理,當然你也可以通過Longsocket參數發送相應的處理訊息。

 

源碼已上傳githup如下,其中有demo供參考。

https://github.com/qianlnk/longsocket

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.