Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(websocket): Convert websocket example to vanilla ts #805

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions plugins/websocket/examples/svelte-app/.gitignore

This file was deleted.

27 changes: 0 additions & 27 deletions plugins/websocket/examples/svelte-app/package.json

This file was deleted.

9 changes: 0 additions & 9 deletions plugins/websocket/examples/svelte-app/src/app.d.ts

This file was deleted.

13 changes: 0 additions & 13 deletions plugins/websocket/examples/svelte-app/src/app.html

This file was deleted.

35 changes: 0 additions & 35 deletions plugins/websocket/examples/svelte-app/src/routes/+page.svelte

This file was deleted.

Binary file not shown.
15 changes: 0 additions & 15 deletions plugins/websocket/examples/svelte-app/svelte.config.js

This file was deleted.

17 changes: 0 additions & 17 deletions plugins/websocket/examples/svelte-app/tsconfig.json

This file was deleted.

8 changes: 0 additions & 8 deletions plugins/websocket/examples/svelte-app/vite.config.ts

This file was deleted.

25 changes: 25 additions & 0 deletions plugins/websocket/examples/tauri-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist/**
!dist/.gitkeep
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
20 changes: 20 additions & 0 deletions plugins/websocket/examples/tauri-app/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>WebSocket Plugin Example</title>
</head>
<body>
<div id="app">
<div>
<input bind:value="{message}" />
<button on:click="{send}">Send</button>
<button on:click="{disconnect}">Disconnect</button>
</div>
<div id="response-container"></div>
</div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
19 changes: 19 additions & 0 deletions plugins/websocket/examples/tauri-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "tauri-app",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview"
},
"devDependencies": {
"@tauri-apps/cli": "1.5.7",
"typescript": "^5.3.3",
"vite": "^5.0.6"
},
"dependencies": {
"tauri-plugin-websocket-api": "link:..\\.."
}
}
1 change: 1 addition & 0 deletions plugins/websocket/examples/tauri-app/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions plugins/websocket/examples/tauri-app/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import WebSocket from "tauri-plugin-websocket-api";
import "./style.css";

let ws: WebSocket;

document.addEventListener("DOMContentLoaded", async () => {
document.querySelector("#send")?.addEventListener("click", send);
document.querySelector("#disconnect")?.addEventListener("click", disconnect);
await connect();
});

function _updateResponse(returnValue: unknown) {
const msg = document.createElement("p");
msg.textContent =
typeof returnValue === "string" ? returnValue : JSON.stringify(returnValue);
document.querySelector("#response-container")?.appendChild(msg);
}

async function connect() {
try {
ws = await WebSocket.connect("ws://127.0.0.1:8080").then((r) => {
_updateResponse("Connected");
return r;
});
} catch (e) {
_updateResponse(e);
}
ws.addListener(_updateResponse);
}

function send() {
ws.send(document.querySelector("#msg-input")?.textContent || "")
.then(() => _updateResponse("Message sent"))
.catch(_updateResponse);
}

function disconnect() {
ws.disconnect()
.then(() => _updateResponse("Disconnected"))
.catch(_updateResponse);
}

document.querySelector<HTMLDivElement>("#app")!.innerHTML = `
<div>
<input type="text" />
<button id="send">send</button>
<button id="disconnect">disconnect</button>
<div id="response-container"></div>
</div>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,7 @@ button:not(:disabled):active {
button:focus {
border-color: #666;
}

p {
margin: 3px 0;
}
1 change: 1 addition & 0 deletions plugins/websocket/examples/tauri-app/src/typescript.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions plugins/websocket/examples/tauri-app/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
23 changes: 23 additions & 0 deletions plugins/websocket/examples/tauri-app/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"module": "ESNext",
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"]
}
Loading
Loading