Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Feb 25, 2024
0 parents commit 2cab284
Show file tree
Hide file tree
Showing 10 changed files with 6,384 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Nuxt dev/build outputs
.output
.data
.nuxt
.nitro
.cache
dist

# Node dependencies
node_modules

# Logs
logs
*.log

# Misc
.DS_Store
.fleet
.idea

# Local env files
.env
.env.*
!.env.example
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Nuxt + WS Demo

Demo for [Nitro WebSocket API](https://nitro.unjs.io/guide/websocket) using [Nuxt](https://nuxt.com)

> [!NOTE]
> This demo is using [Nuxt nightly](https://nuxt.com/docs/guide/going-further/nightly-release-channel)
92 changes: 92 additions & 0 deletions app.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<script setup lang="ts">
const logs = ref<string[]>([])
const log = (...args: any[]) => {
console.log("[ws]", ...args);
logs.value.push(args.join(" "));
};
let ws: WebSocket | undefined
const connect = async () => {
const isSecure = location.protocol === "https:";
const url = (isSecure ? "wss:https://" : "ws:https://") + location.host + "/_ws";
if (ws) {
log("Closing...");
ws.close();
}
log("Connecting to", url, "...");
ws = new WebSocket(url);
ws.addEventListener("close", () => {
log("Connection closed");
});
ws.addEventListener("error", (event) => {
log("Error:", event);
});
ws.addEventListener("message", (event) => {
log("Message from server:", event.data);
});
log("Waiting for connection...");
await new Promise((resolve) => ws!.addEventListener("open", resolve));
};
const clearLogs = () => {
logs.value = []
};
const sendPing = () => {
log("Sending ping...");
ws?.send("ping");
};
const message = ref<string>("ping")
const sendMessage = () => {
ws?.send(message.value);
};
onMounted(async () => {
await connect();
sendPing();
})
</script>

<template>
<div class="ms-m-5" data-theme="dark">
<h3>Nuxt ❤️ WS</h3>

<div class="ms-btn-group">
<button @click="sendPing">Send Ping</button>
<button @click="connect">Reconnect</button>
<button @click="clearLogs">Clear</button>
</div>

<div class="ms-form-group ms-mt-2">
<div class="row">
<div class="col-sm-6">
<input type="email" class="ms-secondary ms-small" id="message" placeholder="Message..." v-model="message"
@keydown.enter="sendMessage" />
</div>
<div class="col-sm-1">
<button class="ms-btn ms-secondary ms-small" @click="sendMessage">
Send
</button>
</div>
</div>
<br />
</div>
<pre id="logs">
<div v-for="log in logs" :key="log">{{ log }}</div>
</pre>
</div>
</template>

<style>
@import url('https://cdn.jsdelivr.net/npm/[email protected]/dist/css/minstyle.io.min.css');
</style>
8 changes: 8 additions & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
nitro: {
experimental: {
websocket: true
}
}
})
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "nuxt-app",
"private": true,
"type": "module",
"scripts": {
"build": "nuxt build",
"dev": "nuxt dev --no-fork",
"generate": "nuxt generate",
"preview": "nuxt preview",
"postinstall": "nuxt prepare"
},
"dependencies": {
"nuxt": "npm:nuxt-nightly@latest",
"nitropack": "npm:nitropack-nightly@latest",
"nuxi": "npm:nuxi-nightly@latest",
"vue": "^3.4.19",
"vue-router": "^4.3.0"
}
}
Loading

0 comments on commit 2cab284

Please sign in to comment.