supporting previous event socket format

This commit is contained in:
Roland Osborne 2023-04-14 11:39:37 -07:00
parent a95f7323e0
commit 84414ad499
3 changed files with 74 additions and 12 deletions

View File

@ -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
// 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]
}
}
}
}
}

View File

@ -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);

View File

@ -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 {