feat: use message-processor library

pull/1/head
lambda 2 years ago
parent 3a8e7dd766
commit dcfc6b33da

@ -1,5 +1,9 @@
import { addPlayerInteractions, hideAudio } from "./interactions.js";
import { connectToServer } from "./websocket.js";
import { actions } from "./messagesProcessor.js";
import { messageProcessor } from "@radioiceberg/message-processor";
messageProcessor.actions = actions;
const main = () => {
hideAudio();

@ -1,3 +1,4 @@
import { messageProcessor } from "@radioiceberg/message-processor";
import { artistBlock, titleBlock, logoDot } from "./dom.js";
const changeTitles = ({ artist, title }) => {
@ -32,27 +33,14 @@ const liveEnded = () => {
};
const setState = ({ data }) => {
const { lastPlayed } = data;
changeTitles(lastPlayed);
const { lastPlayed, isOnline } = data;
if (lastPlayed) changeTitles(lastPlayed);
if (isOnline) liveStarted();
};
const actions = {
export const actions = {
metadataChange,
liveStarted,
liveEnded,
setState,
};
export const processMessage = ({ data }) => {
try {
const message = JSON.parse(data);
const action = actions[message.command];
if (action) {
action(message);
} else {
console.log("no action for", message);
}
} catch (error) {
console.error("Error while process websocket message", error);
}
};

@ -1,8 +1,8 @@
import { processMessage } from "./messagesProcessor.js";
import { messageProcessor } from "@radioiceberg/message-processor";
const socketProtocol = location.protocol === "http:" ? "ws:" : "wss:";
export async function connectToServer() {
const ws = new WebSocket(`${socketProtocol}//${location.host}/meta`);
ws.addEventListener("message", processMessage);
ws.addEventListener("message", messageProcessor.processMessage);
}