示例#1
0
文件: echo.go 项目: currantlabs/ble
func (e *echoChar) echo(req ble.Request, n ble.Notifier) {
	ch := make(chan []byte)
	e.Lock()
	e.m[req.Conn().RemoteAddr().String()] = ch
	e.Unlock()
	log.Printf("echo: Notification subscribed")
	defer func() {
		e.Lock()
		delete(e.m, req.Conn().RemoteAddr().String())
		e.Unlock()
	}()
	for {
		select {
		case <-n.Context().Done():
			log.Printf("echo: Notification unsubscribed")
			return
		case <-time.After(time.Second * 20):
			log.Printf("echo: timeout")
			return
		case msg := <-ch:
			if _, err := n.Write(msg); err != nil {
				log.Printf("echo: can't indicate: %s", err)
				return
			}
		}
	}
}
示例#2
0
文件: echo.go 项目: currantlabs/ble
func (e *echoChar) written(req ble.Request, rsp ble.ResponseWriter) {
	e.Lock()
	e.m[req.Conn().RemoteAddr().String()] <- req.Data()
	e.Unlock()
}