示例#1
0
// Create a golfstream remote service implementation that doesn't itseld do anything except sending commands to remote server
// wia HTTP and a websoket.
func NewHttp(baseUrl string, p poster.Poster, errorCb func(error)) (Service, error) {
	if errorCb == nil {
		errorCb = func(error) {}
	}
	if p == nil {
		p = poster.Http()
	}

	url := baseUrl
	if strings.HasPrefix(url, "http://") {
		url = url[len("http://"):]
	}

	ws, _, err := websocket.DefaultDialer.Dial(fmt.Sprintf("ws://%s/events", url), nil)
	if err != nil {
		return nil, err
	}

	self := &remoteService{
		baseUrl, true, p,
		0,
		sync.Mutex{}, map[string]*remoteServiceBackend{},
		sync.Mutex{}, ws,
		sync.Mutex{}, map[uint32]chan json.RawMessage{},
	}

	go func() {
		if err := self.run(ws); err != nil {
			errorCb(err)
		}
	}()

	return self, nil
}
示例#2
0
/*
Create a remote http backend/ It doesn't store anything and just send commants to a remote server via HTTP.
*/
func NewHttp(baseUrl string, p poster.Poster) Backend {
	if p == nil {
		p = poster.Http()
	}
	return &httpBackend{
		baseUrl,
		fmt.Sprintf("%s/config", baseUrl),
		fmt.Sprintf("%s/streams", baseUrl),
		fmt.Sprintf("%s/drop", baseUrl),
		fmt.Sprintf("%s/streams/%%s", baseUrl),
		p,
		sync.Mutex{},
		map[string]*httpBackendStream{},
	}
}