Пример #1
0
func (c *Client) dial(timeout time.Duration) (err error) {
	if c.ReadBufferSize == 0 {
		c.ReadBufferSize = 4096
	}

	if c.WriteBufferSize == 0 {
		c.WriteBufferSize = 4096
	}

	opts := &sockjsclient.DialOptions{
		BaseURL:         c.URL,
		ReadBufferSize:  c.ReadBufferSize,
		WriteBufferSize: c.WriteBufferSize,
		Timeout:         timeout,
	}

	transport := c.LocalKite.Config.Transport

	c.LocalKite.Log.Debug("Client transport is set to '%s'", transport)

	c.m.Lock()
	defer c.m.Unlock()

	switch transport {
	case config.WebSocket:
		c.session, err = sockjsclient.ConnectWebsocketSession(opts)
	case config.XHRPolling:
		c.session, err = sockjsclient.NewXHRSession(opts)
	default:
		return fmt.Errorf("Connection transport is not known '%v'", transport)
	}

	if err != nil {
		// explicitly set nil to avoid panicing when used the methods of that interface
		c.session = nil
		return err
	}

	go c.sendHub()
	c.wg.Add(1) // with sendHub we added a new listener

	// Reset the wait time.
	c.redialBackOff.Reset()

	// Must be run in a goroutine because a handler may wait a response from
	// server.
	go c.callOnConnectHandlers()

	return nil
}
Пример #2
0
func (c *Client) dial(timeout time.Duration) (err error) {
	if c.ReadBufferSize == 0 {
		c.ReadBufferSize = 4096
	}

	if c.WriteBufferSize == 0 {
		c.WriteBufferSize = 4096
	}

	opts := &sockjsclient.DialOptions{
		BaseURL:         c.URL,
		ReadBufferSize:  c.ReadBufferSize,
		WriteBufferSize: c.WriteBufferSize,
		ClientFunc:      c.ClientFunc,
		Timeout:         timeout,
	}

	transport := c.LocalKite.Config.Transport

	c.LocalKite.Log.Debug("Client transport is set to '%s'", transport)

	var session sockjs.Session

	switch transport {
	case config.WebSocket:
		session, err = sockjsclient.ConnectWebsocketSession(opts)
	case config.XHRPolling:
		session, err = sockjsclient.NewXHRSession(opts)
	default:
		return fmt.Errorf("Connection transport is not known '%v'", transport)
	}

	if err != nil {
		return err
	}

	c.setSession(session)
	c.wg.Add(1)
	go c.sendHub()

	// Reset the wait time.
	c.redialBackOff.Reset()

	// Must be run in a goroutine because a handler may wait a response from
	// server.
	go c.callOnConnectHandlers()

	return nil
}