import { defineNuxtPlugin } from "#app";
export default defineNuxtPlugin(() => {
const ws = new WebSocket("ws://localhost:3000/websocket");
ws.onopen = () => console.log("Connected to WS");
ws.onmessage = (message) => {
const parsedData = JSON.parse(message.data);
console.log("Received data:", parsedData);
};
ws.onerror = (error) => console.log(`Error: ${error}`);
ws.onclose = () => console.log("Disconnected from WS");
function send(data: string | object) {
ws.send(typeof data === "string" ? data : JSON.stringify(data));
}
return {
provide: {
$ws: { send }
}
};
});
import { defineNuxtPlugin } from "#app";
export default defineNuxtPlugin(() => {
const ws = new WebSocket("ws://localhost:3000/websocket");
ws.onopen = () => console.log("Connected to WS");
ws.onmessage = (message) => {
const parsedData = JSON.parse(message.data);
console.log("Received data:", parsedData);
};
ws.onerror = (error) => console.log(`Error: ${error}`);
ws.onclose = () => console.log("Disconnected from WS");
function send(data: string | object) {
ws.send(typeof data === "string" ? data : JSON.stringify(data));
}
return {
provide: {
$ws: { send }
}
};
});