Esempio n. 1
0
File: conn.go Progetto: nbusy/nbusy
// SendRequest sends a JSON-RPC request through the connection with an auto generated request ID.
// resHandler is called when a response is returned.
func (c *Conn) SendRequest(method string, params interface{}, resHandler func(res *ResCtx) error) (reqID string, err error) {
	id, err := shortid.UUID()
	if err != nil {
		return "", err
	}

	req := request{ID: id, Method: method, Params: params}
	if err = c.send(req); err != nil {
		return "", err
	}

	c.resRoutes.Set(req.ID, resHandler)
	return id, nil
}
Esempio n. 2
0
File: conn.go Progetto: nbusy/nbusy
// NewConn creates a new Conn object.
func NewConn() (*Conn, error) {
	id, err := shortid.UUID()
	if err != nil {
		return nil, err
	}

	c := &Conn{
		ID:             id,
		Session:        cmap.New(),
		resRoutes:      cmap.New(),
		deadline:       time.Second * time.Duration(300),
		disconnHandler: func(c *Conn) {},
	}
	c.connected.Store(false)
	return c, nil
}
Esempio n. 3
0
// SendRequest sends a JSON-RPC request through the connection denoted by the connection ID with an auto generated request ID.
// resHandler is called when a response is returned.
func (s *Sender) SendRequest(connID string, method string, params interface{}, resHandler func(ctx *ResCtx) error) (reqID string, err error) {
	s.lazyRegisterMiddleware()

	id, err := shortid.UUID()
	if err != nil {
		return "", err
	}

	req := Request{ID: id, Method: method, Params: params}
	if err = s.sendMsg(connID, req); err != nil {
		return "", err
	}

	s.resRoutes.Set(req.ID, resHandler)
	return id, nil
}