Skip to content

Commit

Permalink
use channel to wait for sockets before looping send frame
Browse files Browse the repository at this point in the history
  • Loading branch information
gferraro committed Nov 8, 2021
1 parent f9bceb3 commit 729ecf4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
6 changes: 1 addition & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ script:
- go vet ./...
- go test ./...
- curl -sL https://git.io/goreleaser | bash -s check
- make install-typescript
- git diff
- git status

# Create a release using goreleaser when tests pass and revision is tagged.
before_deploy:
- git fetch --unshallow
deploy:
- provider: script
skip_cleanup: true
Expand Down
9 changes: 9 additions & 0 deletions cmd/managementd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
socketTimeout = 7 * time.Second
)

var haveClients = make(chan bool)
var version = "<not set>"
var sockets = make(map[int64]*WebsocketRegistration)
var socketsLock sync.RWMutex
Expand Down Expand Up @@ -175,12 +176,17 @@ func WebsocketServer(ws *websocket.Conn) {
// Occasionally go through the list and cull any that are no-longer sending heart-beats.
if message.Type == "Register" {
socketsLock.Lock()
firstSocket := len(sockets) == 0
sockets[message.Uuid] = &WebsocketRegistration{
Socket: ws,
LastHeartbeatAt: time.Now(),
AtomicLock: 0,
}
socketsLock.Unlock()
if firstSocket {
log.Print("Git new client register")
haveClients <- true
}
}
if message.Type == "Heartbeat" {
if socket, ok := sockets[message.Uuid]; ok {
Expand Down Expand Up @@ -281,6 +287,9 @@ func sendFrameToSockets() {
}
socketsLock.Unlock()
}
} else {
log.Print("Wait for new client camera register")
<-haveClients
}
}
}
Expand Down

0 comments on commit 729ecf4

Please sign in to comment.