Skip to content

Commit b758dc5

Browse files
committed
Fix send websocket msg when connecting
1 parent e285134 commit b758dc5

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

pkg/web/src/routes/+page.svelte

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -158,20 +158,28 @@ async function getPizza() {
158158
}
159159
160160
pizza = json;
161-
if (socket.readyState <= 1) {
162-
socket.send(
163-
JSON.stringify({
164-
// FIXME: The 'user' key is present in order not to break
165-
// existing examples using QP WS. Remove it at some point.
166-
// It has no connection to the user auth itself.
167-
user: wsVisitorID,
168-
ws_visitor_id: wsVisitorID,
169-
msg: 'new_pizza',
170-
}),
171-
);
161+
const wsMsg = JSON.stringify({
162+
// FIXME: The 'user' key is present in order not to break
163+
// existing examples using QP WS. Remove it at some point.
164+
// It has no connection to the user auth itself.
165+
user: wsVisitorID,
166+
ws_visitor_id: wsVisitorID,
167+
msg: 'new_pizza',
168+
});
169+
if (socket.readyState === WebSocket.OPEN) {
170+
socket.send(wsMsg);
171+
} else if (socket.readyState === WebSocket.CONNECTING) {
172+
const handleOpen = () => {
173+
socket.send(wsMsg);
174+
socket.removeEventListener('open', handleOpen);
175+
};
176+
socket.addEventListener('open', handleOpen);
177+
} else {
178+
faro.api.pushError(new Error('socket state error: ' + socket.readyState));
172179
}
180+
173181
if (pizza['pizza']['ingredients'].find((e) => e.name === 'Pineapple')) {
174-
faro.api.pushError(new Error('Bad Pizza Recommendation'));
182+
faro.api.pushError(new Error('Pizza Error: Pineapple detected! This is a violation of ancient pizza law. Proceed at your own risk!'));
175183
}
176184
}
177185

0 commit comments

Comments
 (0)