#tauri-plugin #tcp-socket #tauri #tcp #plugin

sys tauri-plugin-tcp

TCP Socket for Tauri App

2 releases

0.1.1 Oct 19, 2024
0.1.0 Oct 19, 2024

#1531 in Network programming

Download history 311/week @ 2024-10-15 39/week @ 2024-10-22

350 downloads per month

MIT license

16KB
370 lines

tauri-plugin-tcp

This plugin only works with Tauri 2.x only.

Install

cargo add tauri-plugin-tcp
npm i @kuyoonjo/tauri-plugin-tcp

Usage

rust


tauri::Builder::default()
    .plugin(tauri_plugin_tcp::init())
    ...

javascript

import { connect, disconnect, send, listen } from "@kuyoonjo/tauri-plugin-tcp";

// Server side
const sid = 'unique-server-id';
await bind(sid, '0.0.0.0:8080');
await send(sid, '192.168.1.2:9090', 'hello');
let clientAddr = '';
await listen((x) => {
  console.log(x.payload);
  if (x.payload.id === sid && x.payload.event.connect) {
    clientAddr = x.payload.event.connect;
    await send(sid, 'hello', clientAddr);
  }
});
await unbind(sid);

// Client side
const cid = 'unique-client-id';
await connect(cid, '0.0.0.0:8080');
await listen((x) => {
  console.log(x.payload);
  if (x.payload.id === cid && x.payload.event.message) {
    // npm i buffer
    // import { Buffer } from 'buffer';
    let str = Buffer.from(x.payload.event.message.data).toString();
    if (str === 'hello')
      await send(cid, 'world');
  }
});
await disconnect(cid);

Event Payload Interface

export interface Payload {
  id: string;
  event: {
    bind?: string;
    unbind?: [];
    connect?: string;
    disconnect?: string;
    message?: {
      addr: string;
      data: number[];
    };
  };
}

permissions

add "tcp:default" into "permissions" list of src-tauri\capabilities\default.json

{
  "$schema": "../gen/schemas/desktop-schema.json",
  ...
  "permissions": [
    "core:default",
    ...
    "tcp:default"
  ]
}

Support

MacOS Linux Windows

Dependencies

~18–58MB
~884K SLoC