Skip to content

Commit

Permalink
Merge remote-tracking branch 'Parent/v1' into Current
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolaRHristov committed Jun 23, 2024
2 parents 65ebea9 + b8e7d61 commit 21c4622
Show file tree
Hide file tree
Showing 9 changed files with 450 additions and 39 deletions.
29 changes: 17 additions & 12 deletions examples/tauri-app/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
{
"name": "tauri-app",
"scripts": {
"build": "tsc && vite build",
"dev": "vite",
"preview": "vite preview"
},
"dependencies": {
"tauri-plugin-websocket-api": "link:..\\.."
},
"devDependencies": {
"@tauri-apps/cli": "1.5.14"
}
"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.14",
"typescript": "^5.3.3",
"vite": "^5.0.12"
},
"dependencies": {
"tauri-plugin-websocket-api": "link:..\\.."
}
}
13 changes: 13 additions & 0 deletions examples/tauri-app/src-tauri/rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
max_width = 100
hard_tabs = false
tab_spaces = 2
newline_style = "Auto"
use_small_heuristics = "Default"
reorder_imports = true
reorder_modules = true
remove_nested_parens = true
edition = "2018"
merge_derives = true
use_try_shorthand = false
use_field_init_shorthand = false
force_explicit_abi = true
38 changes: 38 additions & 0 deletions examples/tauri-app/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]

use futures_util::StreamExt;
use tokio::net::{TcpListener, TcpStream};

async fn start_server() {
let addr = "127.0.0.1:8080".to_string();

// Create the event loop and TCP listener we'll accept connections on.
let try_socket = TcpListener::bind(&addr).await;
let listener = try_socket.expect("Failed to bind");

while let Ok((stream, _)) = listener.accept().await {
tokio::spawn(accept_connection(stream));
}
}

async fn accept_connection(stream: TcpStream) {
let ws_stream = tokio_tungstenite::accept_async(stream)
.await
.expect("Error during the websocket handshake occurred");

let (write, read) = ws_stream.split();
if let Err(e) = read.forward(write).await {
eprintln!("Error: {}", e);
}
}

fn main() {
tauri::async_runtime::spawn(start_server());
tauri::Builder::default()
.plugin(tauri_plugin_websocket::init())
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
51 changes: 51 additions & 0 deletions examples/tauri-app/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* eslint-disable */
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>
`;
72 changes: 72 additions & 0 deletions examples/tauri-app/src/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
html,
body {
position: relative;
width: 100%;
height: 100%;
}

body {
color: #333;
margin: 0;
padding: 8px;
box-sizing: border-box;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
}

a {
color: rgb(0, 100, 200);
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

a:visited {
color: rgb(0, 80, 160);
}

label {
display: block;
}

input,
button,
select,
textarea {
font-family: inherit;
font-size: inherit;
-webkit-padding: 0.4em 0;
padding: 0.4em;
margin: 0 0 0.5em 0;
box-sizing: border-box;
border: 1px solid #ccc;
border-radius: 2px;
}

input:disabled {
color: #ccc;
}

button {
color: #333;
background-color: #f4f4f4;
outline: none;
}

button:disabled {
color: #999;
}

button:not(:disabled):active {
background-color: #ddd;
}

button:focus {
border-color: #666;
}

p {
margin: 3px 0;
}
1 change: 1 addition & 0 deletions 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 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" />
57 changes: 30 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
{
"name": "tauri-plugin-websocket-api",
"exports": {
"browser": "./dist-js/index.min.js",
"import": "./dist-js/index.mjs",
"types": "./dist-js/index.d.ts"
},
"module": "dist-js/index.mjs",
"browser": "dist-js/index.min.js",
"types": "dist-js/index.d.ts",
"files": [
"dist-js",
"!dist-js/**/*.map",
"README.md",
"LICENSE"
],
"scripts": {
"build": "rollup -c"
},
"dependencies": {
"@tauri-apps/api": "1.5.6"
},
"devDependencies": {
"tslib": "2.6.3"
},
"authors": [
"Tauri Programme within The Commons Conservancy"
]
"name": "tauri-plugin-websocket-api",
"version": "0.0.0",
"license": "MIT or APACHE-2.0",
"authors": [
"Tauri Programme within The Commons Conservancy"
],
"type": "module",
"browser": "dist-js/index.min.js",
"module": "dist-js/index.mjs",
"types": "dist-js/index.d.ts",
"exports": {
"import": "./dist-js/index.mjs",
"types": "./dist-js/index.d.ts",
"browser": "./dist-js/index.min.js"
},
"scripts": {
"build": "rollup -c"
},
"files": [
"dist-js",
"!dist-js/**/*.map",
"README.md",
"LICENSE"
],
"devDependencies": {
"tslib": "2.6.3"
},
"dependencies": {
"@tauri-apps/api": "1.5.6"
}
}
Loading

0 comments on commit 21c4622

Please sign in to comment.