client.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package neffos
  2. import (
  3. "context"
  4. "strings"
  5. )
  6. // Client is the neffos client. Contains the neffos client-side connection
  7. // and the ID came from server on acknowledgement process of the `Dial` function.
  8. // Use its `Connect` to connect to a namespace or
  9. // `WaitServerConnect` to wait for server to force-connect this client to a namespace.
  10. type Client struct {
  11. conn *Conn
  12. // ID comes from server, local changes are not reflected,
  13. // use the `Server#IDGenerator` if you want to set a custom logic for ID set.
  14. ID string
  15. // NotifyClose can be optionally registered to notify about the client's disconnect.
  16. // This callback is for the entire client side connection,
  17. // the channel is notified after namespace disconnected and any room left events.
  18. // Don't confuse it with the `OnNamespaceDisconnect` event.
  19. // Usage:
  20. // <- client.NotifyClose // blocks until local `Close` or remote close of connection.
  21. NotifyClose <-chan struct{}
  22. }
  23. // Close method terminates the client-side connection.
  24. // Forces the client to disconnect from all connected namespaces and leave from all joined rooms,
  25. // server gets notified.
  26. func (c *Client) Close() {
  27. if c == nil || c.conn == nil {
  28. return
  29. }
  30. c.conn.Close()
  31. }
  32. // WaitServerConnect method blocks until server manually calls the connection's `Connect`
  33. // on the `Server#OnConnected` event.
  34. //
  35. // See `Conn#WaitConnect` for more details.
  36. func (c *Client) WaitServerConnect(ctx context.Context, namespace string) (*NSConn, error) {
  37. return c.conn.WaitConnect(ctx, namespace)
  38. }
  39. // Connect method returns a new connected to the specific "namespace" `NSConn` value.
  40. // The "namespace" should be declared in the `connHandler` of both server and client sides.
  41. // Returns error if server-side's `OnNamespaceConnect` event callback returns an error.
  42. //
  43. // See `Conn#Connect` for more details.
  44. func (c *Client) Connect(ctx context.Context, namespace string) (*NSConn, error) {
  45. return c.conn.Connect(ctx, namespace)
  46. }
  47. // Dialer is the definition type of a dialer, gorilla or gobwas or custom.
  48. // It is the second parameter of the `Dial` function.
  49. type Dialer func(ctx context.Context, url string) (Socket, error)
  50. // Dial establishes a new neffos client connection.
  51. // Context "ctx" is used for handshake timeout.
  52. // Dialer "dial" can be either `gobwas.Dialer/DefaultDialer` or `gorilla.Dialer/DefaultDialer`,
  53. // custom dialers can be used as well when complete the `Socket` and `Dialer` interfaces for valid client.
  54. // URL "url" is the endpoint of the neffos server, i.e "ws://localhost:8080/echo".
  55. // The last parameter, and the most important one is the "connHandler", it can be
  56. // filled as `Namespaces`, `Events` or `WithTimeout`, same namespaces and events can be used on the server-side as well.
  57. //
  58. // See examples for more.
  59. func Dial(ctx context.Context, dial Dialer, url string, connHandler ConnHandler) (*Client, error) {
  60. if ctx == nil {
  61. ctx = context.Background()
  62. }
  63. if !strings.HasPrefix(url, "ws://") && !strings.HasPrefix(url, "wss://") {
  64. url = "ws://" + url
  65. }
  66. underline, err := dial(ctx, url)
  67. if err != nil {
  68. return nil, err
  69. }
  70. if connHandler == nil {
  71. connHandler = Namespaces{}
  72. }
  73. c := newConn(underline, connHandler.GetNamespaces())
  74. readTimeout, writeTimeout := getTimeouts(connHandler)
  75. c.readTimeout = readTimeout
  76. c.writeTimeout = writeTimeout
  77. go c.startReader()
  78. if err = c.sendClientACK(); err != nil {
  79. return nil, err
  80. }
  81. return &Client{conn: c, ID: c.id, NotifyClose: c.closeCh}, nil
  82. }