From 84414ad49908bbb85c7b54ad63938959d5f7f3f8 Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Fri, 14 Apr 2023 11:39:37 -0700 Subject: [PATCH] supporting previous event socket format --- net/server/internal/api_status.go | 77 +++++++++++++++++++--- net/web/src/context/useAppContext.hook.js | 7 +- net/web/src/context/useRingContext.hook.js | 2 +- 3 files changed, 74 insertions(+), 12 deletions(-) diff --git a/net/server/internal/api_status.go b/net/server/internal/api_status.go index 0c9e440b..2e154f9f 100644 --- a/net/server/internal/api_status.go +++ b/net/server/internal/api_status.go @@ -13,11 +13,15 @@ import ( var wsSync sync.Mutex var wsExit = make(chan bool, 1) var statusListener = make(map[uint][]chan<- []byte) +var revisionListener = make(map[uint][]chan<- []byte) var upgrader = websocket.Upgrader{} //Status handler for websocket connection func Status(w http.ResponseWriter, r *http.Request) { + // send ringing updates + ringMode := r.FormValue("mode") == "ring" + // accept websocket connection conn, err := upgrader.Upgrade(w, r, nil) if err != nil { @@ -74,9 +78,14 @@ func Status(w http.ResponseWriter, r *http.Request) { c := make(chan []byte) defer close(c) - // register channel for revisions - addStatusListener(session.Account.ID, c) - defer removeStatusListener(session.Account.ID, c) + // register channel for updates + if ringMode { + addStatusListener(session.Account.ID, c) + defer removeStatusListener(session.Account.ID, c) + } else { + addRevisionListener(session.Account.ID, c) + defer removeRevisionListener(session.Account.ID, c) + } // start ping pong ticker ticker := time.NewTicker(60 * time.Second) @@ -159,9 +168,14 @@ func SetStatus(account *store.Account) { // get revisions for the account rev := getRevision(account) - msg, err := json.Marshal(rev) - if err != nil { - ErrMsg(err) + full, errFull := json.Marshal(rev) + if errFull != nil { + ErrMsg(errFull) + return + } + base, errBase := json.Marshal(rev.Revision) + if errBase != nil { + ErrMsg(errBase) return } @@ -169,11 +183,19 @@ func SetStatus(account *store.Account) { wsSync.Lock() defer wsSync.Unlock() - // notify all listeners - chs, ok := statusListener[account.ID] + // notify all base listeners + chs, ok := revisionListener[account.ID] if ok { for _, ch := range chs { - ch <- msg + ch <- base + } + } + + // notify all full listeners + chs, ok = statusListener[account.ID] + if ok { + for _, ch := range chs { + ch <- full } } } @@ -214,3 +236,40 @@ func removeStatusListener(act uint, ch chan<- []byte) { } } } + +func addRevisionListener(act uint, ch chan<- []byte) { + + // lock access to revisionListener + wsSync.Lock() + defer wsSync.Unlock() + + // add new listener to map + chs, ok := revisionListener[act] + if ok { + revisionListener[act] = append(chs, ch) + } else { + revisionListener[act] = []chan<- []byte{ch} + } +} + +func removeRevisionListener(act uint, ch chan<- []byte) { + + // lock access to revisionListener + wsSync.Lock() + defer wsSync.Unlock() + + // remove channel from map + chs, ok := revisionListener[act] + if ok { + for i, c := range chs { + if ch == c { + if len(chs) == 1 { + delete(revisionListener, act) + } else { + chs[i] = chs[len(chs)-1] + revisionListener[act] = chs[:len(chs)-1] + } + } + } + } +} diff --git a/net/web/src/context/useAppContext.hook.js b/net/web/src/context/useAppContext.hook.js index c811e3ca..eec9b889 100644 --- a/net/web/src/context/useAppContext.hook.js +++ b/net/web/src/context/useAppContext.hook.js @@ -172,7 +172,7 @@ export function useAppContext(websocket) { } updateState({ status: 'connecting' }); - ws.current = createWebsocket(protocol + window.location.host + "/status"); + ws.current = createWebsocket(protocol + window.location.host + "/status?mode=ring"); ws.current.onmessage = (ev) => { try { let activity = JSON.parse(ev.data); @@ -180,10 +180,13 @@ export function useAppContext(websocket) { if (activity.revision) { setAppRevision(activity.revision); } - if (activity.ring) { + else if (activity.ring) { const { cardId, callId, calleeToken, iceUrl, iceUsername, icePassword } = activity.ring; ringContext.actions.ring(cardId, callId, calleeToken, iceUrl, iceUsername, icePassword); } + else { + setAppRevision(activity); + } } catch (err) { console.log(err); diff --git a/net/web/src/context/useRingContext.hook.js b/net/web/src/context/useRingContext.hook.js index c62785c0..7f8e17d6 100644 --- a/net/web/src/context/useRingContext.hook.js +++ b/net/web/src/context/useRingContext.hook.js @@ -196,7 +196,7 @@ export function useRingContext() { accessAudio.current = false; const protocol = window.location.protocol === 'http:' ? 'ws://' : 'wss://'; - ws.current = createWebsocket(`${protocol}${node}/signal`); + ws.current = createWebsocket(`${protocol}${node}/signal?mode=ring`); ws.current.onmessage = async (ev) => { // handle messages [impolite] try {