big websocket change

This commit is contained in:
ntr committed 2022-09-16 19:21:11 +10:00
1 parent 0e3c272adc
commit 73248f7d9f
12 files changed
+126 -99

No files matched your search

+24 -1
View File
@@ -1,4 +1,4 @@
import { wantsState, wsState, userState } from './store'
import { wantsState, wsState, userState, offersState, acpOffersState } from './store'
const SOCKET_URL = `${window.location.protocol === 'https:' ? 'wss://' : 'ws://'}${window.location.host}/api/ws`;
@@ -34,6 +34,14 @@ export default function createSocket(dispatch) {
send(['WantRm', { id }]);
}
function sendAcpOffers() {
send(['AcpOffers', {}]);
}
function sendAcpOfferStatusSet(id, status) {
send(['AcpOfferStatusSet', { id, status }]);
}
let pongTimeout;
function onPong() {
pongTimeout = setTimeout(sendPing, 5000);
@@ -50,6 +58,14 @@ export default function createSocket(dispatch) {
dispatch(wantsState.actions.set(wants));
}
function onOffers(offers) {
dispatch(offersState.actions.set(offers));
}
function onAcpOffers(offers) {
dispatch(acpOffersState.actions.set(offers));
}
// -------------
// Setup
// -------------
@@ -57,8 +73,11 @@ export default function createSocket(dispatch) {
// this object wraps the reply types to a function
const handlers = {
Wants: onWants,
Offers: onOffers,
User: onUser,
AcpOffers: onAcpOffers,
Pong: onPong,
Error: errHandler,
};
@@ -84,6 +103,7 @@ export default function createSocket(dispatch) {
function onOpen() {
console.log('websocket connected', SOCKET_URL)
sendPing();
sendAcpOffers();
}
function onError(event) {
@@ -122,5 +142,8 @@ export default function createSocket(dispatch) {
sendOfferStatusSet,
sendWantRm,
sendAcpOffers,
sendAcpOfferStatusSet,
};
}