Golang Long Connection

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Design ideas: Each websocket allows the connection is time-limited, after timeout, the server will automatically disconnect, then the long connection on the service side after the disconnection information sent by the client to detect the disconnection information to initiate a connection request, and then through the handshake information to ensure that the client is connected with the servers.

Design structure:

[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. }


The long connection actively sends a message to the connecting party via the ' Writech ' channel, reading the information in the connection through the ' READCH ' channel, setting ' Shakehand ' to determine whether to send the handshake information, status to identify the connection status.

Through the Writeloop to send handshake information, while listening to the ' Writech ' channel, forward the message in the channel.

[Plain]View Plain Copy  
  1. Call Func with a gorouting, it'll send shake hands message to service to make sure
  2. If you want to send message call func ' Write ', and the case Writech'll be vaild
  3. Func (L *longsocket) Writeloop () {
  4. Defer func () {
  5. If err: = Recover (); Err! = Nil {
  6. Fmt. Println ("Writeloop", err)
  7. }
  8. }()
  9. for {
  10. ErrCount: = 0
  11. If l.status! = status_connect {
  12. Break
  13. }
  14. Select {
  15. Case <-time. After (time. Second * time. Duration (shake_hands_frequency)):
  16. If L.shakehand {
  17. _, Err: = L.ws.write ([]byte (SHAKE_HANDS_MSG))
  18. If err! = Nil {
  19. errcount++
  20. }
  21. }
  22. Case msg: = <-l.writech:
  23. _, Err: = L.ws.write (msg)
  24. If err! = Nil {
  25. errcount++
  26. }
  27. }
  28. If ErrCount! = 0 {
  29. Break
  30. }
  31. }
  32. L.close ()
  33. }


The message is received via Readloop and forwarded to the ' READCH ' channel.

[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. for {
  9. If l.status! = status_connect {
  10. Break
  11. }
  12. BUF: = Make ([]byte, L.buffersize)
  13. N, Err: = L.ws.read (BUF)
  14. If err! = Nil {
  15. Break
  16. }
  17. If n > 0 {
  18. L.READCH <-Buf[0:n]
  19. }
  20. }
  21. L.close ()
  22. }


The message can then be forwarded through the Read function to a shape such as

Type dealmsg func ([]byte, *longsocket) error

function to do the corresponding message processing, of course, you can also send the corresponding processing message through the Longsocket parameter.

Source has been uploaded githup as follows, including demo for reference.

Https://github.com/qianlnk/longsocket

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.