func (c *Client) Connect() (err error) {
	c.Lock()
	defer c.Unlock()

	// check if web socket connection is already open
	if !c.Dead() {
		return errors.New("websocket connection already established, cannot open a new one")
	}

	// discord API sends a web socket url which should be used.
	// It's required to be cached, and only re-requested whenever disgord is unable
	// to reconnect to the API..
	if !c.Routed() {
		c.url, err = getGatewayRoute(c.conf.HTTPClient, c.conf.DAPIVersion, c.conf.DAPIEncoding)
		if err != nil {
			return
		}
	}

	defer func(err error) error {
		if err != nil {
			if c.conn != nil {
				c.conn.Close()
				c.conn = nil
				close(c.disconnected)
			}
			logrus.Error(err)
			return err
		}
		return nil
	}(err)

	// establish ws connection
	logrus.Debug("Connecting to url: " + c.url)
	c.conn, _, err = websocket.DefaultDialer.Dial(c.url, nil)
	if err != nil {
		return
	}
	logrus.Debug("established web socket connection")
	c.disconnected = make(chan struct{})

	// setup operation listeners
	// These handle sepecific "events" related to the socket connection
	go c.operationHandlers()

	// setup read and write goroutines
	go c.readPump()
	go c.writePump(c.conn)

	return
}
// Connect establishes a websocket connection to the discord API
// if Connect() fails, it closes channels and connections
Connect establishes a websocket connection to the discord API
if Connect() fails, it closes channels and connections
// Connect establishes a websocket connection to the discord API // if Connect () fails it closes channels and connections
func (c *Client) Connect() (err error) {
	c.Lock()
	defer c.Unlock()
	if !c.Dead() {
		return errors.New("websocket connection already established, cannot open a new one")
	}
	if !c.Routed() {
		c.url, err = getGatewayRoute(c.conf.HTTPClient, c.conf.DAPIVersion, c.conf.DAPIEncoding)
		if err != nil {
			return
		}
	}
	defer func(err error) error {
		if err != nil {
			if c.conn != nil {
				c.conn.Close()
				c.conn = nil
				close(c.disconnected)
			}
			logrus.Error(err)
			return err
		}
		return nil
	}(err)
	logrus.Debug("Connecting to url: " + c.url)
	c.conn, _, err = websocket.DefaultDialer.Dial(c.url, nil)
	if err != nil {
		return
	}
	logrus.Debug("established web socket connection")
	c.disconnected = make(chan struct{})
	go c.operationHandlers()
	go c.readPump()
	go c.writePump(c.conn)
	return
}
func ( c * Client ) Connect ( ) ( err error ) { c . Lock ( ) defer c . Unlock ( ) if ! c . Dead ( ) { return errors . New ( " " ) } if ! c . Routed ( ) { c . url , err = getGatewayRoute ( c . conf . HTTPClient , c . conf . DAPIVersion , c . conf . DAPIEncoding ) if err != nil { return } } defer func ( err error ) error { if err != nil { if c . conn != nil { c . conn . Close ( ) c . conn = nil close ( c . disconnected ) } logrus . Error ( err ) return err } return nil } ( err ) logrus . Debug ( " " + c . url ) c . conn , _ , err = websocket . DefaultDialer . Dial ( c . url , nil ) if err != nil { return } logrus . Debug ( " " ) c . disconnected = make ( chan struct { } ) go c . operationHandlers ( ) go c . readPump ( ) go c . writePump ( c . conn ) return }
Connect establishes a websocket connection to the discord API
if Connect() fails, it closes channels and connections
Connect establishes a websocket connection to the discord API if Connect () fails it closes channels and connections
// check if web socket connection is already open // discord API sends a web socket url which should be used. // It's required to be cached, and only re-requested whenever disgord is unable // to reconnect to the API.. // establish ws connection // setup operation listeners // These handle sepecific "events" related to the socket connection // setup read and write goroutines