tauri-plugin-tcpclient-api 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +136 -0
- package/dist-js/index.cjs +86 -0
- package/dist-js/index.d.ts +94 -0
- package/dist-js/index.js +76 -0
- package/package.json +45 -0
package/README.md
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# tauri-plugin-tcpclient
|
|
2
|
+
|
|
3
|
+
[](https://opensource.org/licenses/MIT)
|
|
4
|
+
[](https://tauri.app)
|
|
5
|
+
|
|
6
|
+
A Tauri plugin for TCP client connections. Send and receive TCP data directly from JavaScript.
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- Connect to TCP servers from JavaScript
|
|
11
|
+
- Send and receive data asynchronously
|
|
12
|
+
- Event-based notifications for connection state changes
|
|
13
|
+
- Support for binding to specific local addresses
|
|
14
|
+
- Proper error handling including Windows disconnect detection
|
|
15
|
+
|
|
16
|
+
## Platform Support
|
|
17
|
+
|
|
18
|
+
| Platform | Supported |
|
|
19
|
+
| -------- | --------- |
|
|
20
|
+
| Linux | ✓ |
|
|
21
|
+
| Windows | ✓ |
|
|
22
|
+
| macOS | ✓ |
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
### Rust
|
|
27
|
+
|
|
28
|
+
Add to your `src-tauri/Cargo.toml`:
|
|
29
|
+
|
|
30
|
+
```toml
|
|
31
|
+
[dependencies]
|
|
32
|
+
tauri-plugin-tcpclient = "0.1"
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### JavaScript
|
|
36
|
+
|
|
37
|
+
```sh
|
|
38
|
+
npm install @x93008/plugin-tcpclient
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
### Register Plugin
|
|
44
|
+
|
|
45
|
+
```rust
|
|
46
|
+
// src-tauri/src/lib.rs
|
|
47
|
+
fn main() {
|
|
48
|
+
tauri::Builder::default()
|
|
49
|
+
.plugin(tauri_plugin_tcpclient::init())
|
|
50
|
+
.run(tauri::generate_context!())
|
|
51
|
+
.expect("error while running tauri application");
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### JavaScript API
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
import {
|
|
59
|
+
connect,
|
|
60
|
+
send,
|
|
61
|
+
sendString,
|
|
62
|
+
disconnect,
|
|
63
|
+
listenEvents,
|
|
64
|
+
isConnected,
|
|
65
|
+
getConnections,
|
|
66
|
+
decodeMessage,
|
|
67
|
+
} from '@x93008/plugin-tcpclient';
|
|
68
|
+
|
|
69
|
+
// Listen for events
|
|
70
|
+
const unlisten = await listenEvents((event) => {
|
|
71
|
+
const { id, event: e } = event.payload;
|
|
72
|
+
|
|
73
|
+
switch (e.type) {
|
|
74
|
+
case 'Connect':
|
|
75
|
+
console.log(`[${id}] Connected to ${e.data.endpoint}`);
|
|
76
|
+
break;
|
|
77
|
+
case 'Message':
|
|
78
|
+
const text = decodeMessage(e.data.data);
|
|
79
|
+
console.log(`[${id}] Received: ${text}`);
|
|
80
|
+
break;
|
|
81
|
+
case 'Disconnect':
|
|
82
|
+
console.log(`[${id}] Disconnected: ${e.data.reason.type}`);
|
|
83
|
+
break;
|
|
84
|
+
case 'Error':
|
|
85
|
+
console.error(`[${id}] Error: ${e.data.message}`);
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
// Connect to server
|
|
91
|
+
await connect('my-conn', '192.168.1.100:8080');
|
|
92
|
+
|
|
93
|
+
// Check connection
|
|
94
|
+
console.log('Connected:', await isConnected('my-conn'));
|
|
95
|
+
|
|
96
|
+
// Send string
|
|
97
|
+
await sendString('my-conn', 'Hello World');
|
|
98
|
+
|
|
99
|
+
// Send raw bytes
|
|
100
|
+
await send('my-conn', [0x01, 0x02, 0x03]);
|
|
101
|
+
|
|
102
|
+
// Get all connections
|
|
103
|
+
console.log('Active connections:', await getConnections());
|
|
104
|
+
|
|
105
|
+
// Disconnect
|
|
106
|
+
await disconnect('my-conn');
|
|
107
|
+
|
|
108
|
+
// Stop listening
|
|
109
|
+
unlisten();
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## API Reference
|
|
113
|
+
|
|
114
|
+
| Function | Description |
|
|
115
|
+
|----------|-------------|
|
|
116
|
+
| `connect(id, endpoint)` | Connect to TCP server |
|
|
117
|
+
| `connectWithBind(id, localAddr, endpoint)` | Connect with specific local address |
|
|
118
|
+
| `send(id, data)` | Send bytes to server |
|
|
119
|
+
| `sendString(id, text)` | Send string to server |
|
|
120
|
+
| `disconnect(id)` | Close connection |
|
|
121
|
+
| `isConnected(id)` | Check if connection exists |
|
|
122
|
+
| `getConnections()` | Get all active connection IDs |
|
|
123
|
+
| `listenEvents(callback)` | Listen for TCP events |
|
|
124
|
+
|
|
125
|
+
## Events
|
|
126
|
+
|
|
127
|
+
| Event | Description |
|
|
128
|
+
|-------|-------------|
|
|
129
|
+
| `Connect` | Connection established |
|
|
130
|
+
| `Message` | Data received from server |
|
|
131
|
+
| `Disconnect` | Connection closed (with reason) |
|
|
132
|
+
| `Error` | Error occurred |
|
|
133
|
+
|
|
134
|
+
## License
|
|
135
|
+
|
|
136
|
+
MIT License
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@tauri-apps/api/core');
|
|
4
|
+
var event = require('@tauri-apps/api/event');
|
|
5
|
+
|
|
6
|
+
// ============================================================================
|
|
7
|
+
// API Functions
|
|
8
|
+
// ============================================================================
|
|
9
|
+
/**
|
|
10
|
+
* Connect to a TCP server
|
|
11
|
+
* @param id - Unique identifier for this connection
|
|
12
|
+
* @param endpoint - Server address (e.g., "192.168.1.100:8080")
|
|
13
|
+
*/
|
|
14
|
+
async function connect(id, endpoint) {
|
|
15
|
+
await core.invoke('plugin:tcpclient|connect', { id, endpoint });
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Connect to a TCP server with a specific local address
|
|
19
|
+
* @param id - Unique identifier for this connection
|
|
20
|
+
* @param localAddr - Local address to bind (e.g., "192.168.1.100:0")
|
|
21
|
+
* @param endpoint - Server address (e.g., "192.168.1.100:8080")
|
|
22
|
+
*/
|
|
23
|
+
async function connectWithBind(id, localAddr, endpoint) {
|
|
24
|
+
await core.invoke('plugin:tcpclient|connect_with_bind', { id, localAddr, endpoint });
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Disconnect from a TCP server
|
|
28
|
+
* @param id - Connection identifier
|
|
29
|
+
*/
|
|
30
|
+
async function disconnect(id) {
|
|
31
|
+
await core.invoke('plugin:tcpclient|disconnect', { id });
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Send data to the TCP server
|
|
35
|
+
* @param id - Connection identifier
|
|
36
|
+
* @param data - Data to send (as byte array or Uint8Array)
|
|
37
|
+
*/
|
|
38
|
+
async function send(id, data) {
|
|
39
|
+
const dataArray = data instanceof Uint8Array ? Array.from(data) : data;
|
|
40
|
+
await core.invoke('plugin:tcpclient|send', { id, data: dataArray });
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Send a string to the TCP server (UTF-8 encoded)
|
|
44
|
+
* @param id - Connection identifier
|
|
45
|
+
* @param text - Text to send
|
|
46
|
+
*/
|
|
47
|
+
async function sendString(id, text) {
|
|
48
|
+
const encoder = new TextEncoder();
|
|
49
|
+
await send(id, encoder.encode(text));
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Check if a connection exists
|
|
53
|
+
* @param id - Connection identifier
|
|
54
|
+
*/
|
|
55
|
+
async function isConnected(id) {
|
|
56
|
+
return await core.invoke('plugin:tcpclient|is_connected', { id });
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Get all active connection IDs
|
|
60
|
+
*/
|
|
61
|
+
async function getConnections() {
|
|
62
|
+
return await core.invoke('plugin:tcpclient|get_connections');
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Listen for TCP client events
|
|
66
|
+
* @param callback - Event callback function
|
|
67
|
+
*/
|
|
68
|
+
async function listenEvents(callback) {
|
|
69
|
+
return await event.listen('plugin://tcpclient', callback);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Decode message data to string
|
|
73
|
+
*/
|
|
74
|
+
function decodeMessage(data) {
|
|
75
|
+
return new TextDecoder().decode(new Uint8Array(data));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
exports.connect = connect;
|
|
79
|
+
exports.connectWithBind = connectWithBind;
|
|
80
|
+
exports.decodeMessage = decodeMessage;
|
|
81
|
+
exports.disconnect = disconnect;
|
|
82
|
+
exports.getConnections = getConnections;
|
|
83
|
+
exports.isConnected = isConnected;
|
|
84
|
+
exports.listenEvents = listenEvents;
|
|
85
|
+
exports.send = send;
|
|
86
|
+
exports.sendString = sendString;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { UnlistenFn } from '@tauri-apps/api/event';
|
|
2
|
+
/** Disconnect reason */
|
|
3
|
+
export type DisconnectReason = {
|
|
4
|
+
type: 'Normal';
|
|
5
|
+
} | {
|
|
6
|
+
type: 'Reset';
|
|
7
|
+
} | {
|
|
8
|
+
type: 'Timeout';
|
|
9
|
+
} | {
|
|
10
|
+
type: 'Error';
|
|
11
|
+
data: string;
|
|
12
|
+
};
|
|
13
|
+
/** Event types */
|
|
14
|
+
export type EventType = {
|
|
15
|
+
type: 'Connect';
|
|
16
|
+
data: {
|
|
17
|
+
endpoint: string;
|
|
18
|
+
};
|
|
19
|
+
} | {
|
|
20
|
+
type: 'Disconnect';
|
|
21
|
+
data: {
|
|
22
|
+
endpoint: string;
|
|
23
|
+
reason: DisconnectReason;
|
|
24
|
+
};
|
|
25
|
+
} | {
|
|
26
|
+
type: 'Message';
|
|
27
|
+
data: {
|
|
28
|
+
endpoint: string;
|
|
29
|
+
data: number[];
|
|
30
|
+
};
|
|
31
|
+
} | {
|
|
32
|
+
type: 'Error';
|
|
33
|
+
data: {
|
|
34
|
+
endpoint: string;
|
|
35
|
+
message: string;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
/** Event payload */
|
|
39
|
+
export interface EventPayload {
|
|
40
|
+
id: string;
|
|
41
|
+
event: EventType;
|
|
42
|
+
}
|
|
43
|
+
/** Event callback type */
|
|
44
|
+
export type EventCallback = (event: {
|
|
45
|
+
payload: EventPayload;
|
|
46
|
+
}) => void;
|
|
47
|
+
/**
|
|
48
|
+
* Connect to a TCP server
|
|
49
|
+
* @param id - Unique identifier for this connection
|
|
50
|
+
* @param endpoint - Server address (e.g., "192.168.1.100:8080")
|
|
51
|
+
*/
|
|
52
|
+
export declare function connect(id: string, endpoint: string): Promise<void>;
|
|
53
|
+
/**
|
|
54
|
+
* Connect to a TCP server with a specific local address
|
|
55
|
+
* @param id - Unique identifier for this connection
|
|
56
|
+
* @param localAddr - Local address to bind (e.g., "192.168.1.100:0")
|
|
57
|
+
* @param endpoint - Server address (e.g., "192.168.1.100:8080")
|
|
58
|
+
*/
|
|
59
|
+
export declare function connectWithBind(id: string, localAddr: string, endpoint: string): Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Disconnect from a TCP server
|
|
62
|
+
* @param id - Connection identifier
|
|
63
|
+
*/
|
|
64
|
+
export declare function disconnect(id: string): Promise<void>;
|
|
65
|
+
/**
|
|
66
|
+
* Send data to the TCP server
|
|
67
|
+
* @param id - Connection identifier
|
|
68
|
+
* @param data - Data to send (as byte array or Uint8Array)
|
|
69
|
+
*/
|
|
70
|
+
export declare function send(id: string, data: Uint8Array | number[]): Promise<void>;
|
|
71
|
+
/**
|
|
72
|
+
* Send a string to the TCP server (UTF-8 encoded)
|
|
73
|
+
* @param id - Connection identifier
|
|
74
|
+
* @param text - Text to send
|
|
75
|
+
*/
|
|
76
|
+
export declare function sendString(id: string, text: string): Promise<void>;
|
|
77
|
+
/**
|
|
78
|
+
* Check if a connection exists
|
|
79
|
+
* @param id - Connection identifier
|
|
80
|
+
*/
|
|
81
|
+
export declare function isConnected(id: string): Promise<boolean>;
|
|
82
|
+
/**
|
|
83
|
+
* Get all active connection IDs
|
|
84
|
+
*/
|
|
85
|
+
export declare function getConnections(): Promise<string[]>;
|
|
86
|
+
/**
|
|
87
|
+
* Listen for TCP client events
|
|
88
|
+
* @param callback - Event callback function
|
|
89
|
+
*/
|
|
90
|
+
export declare function listenEvents(callback: EventCallback): Promise<UnlistenFn>;
|
|
91
|
+
/**
|
|
92
|
+
* Decode message data to string
|
|
93
|
+
*/
|
|
94
|
+
export declare function decodeMessage(data: number[]): string;
|
package/dist-js/index.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { invoke } from '@tauri-apps/api/core';
|
|
2
|
+
import { listen } from '@tauri-apps/api/event';
|
|
3
|
+
|
|
4
|
+
// ============================================================================
|
|
5
|
+
// API Functions
|
|
6
|
+
// ============================================================================
|
|
7
|
+
/**
|
|
8
|
+
* Connect to a TCP server
|
|
9
|
+
* @param id - Unique identifier for this connection
|
|
10
|
+
* @param endpoint - Server address (e.g., "192.168.1.100:8080")
|
|
11
|
+
*/
|
|
12
|
+
async function connect(id, endpoint) {
|
|
13
|
+
await invoke('plugin:tcpclient|connect', { id, endpoint });
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Connect to a TCP server with a specific local address
|
|
17
|
+
* @param id - Unique identifier for this connection
|
|
18
|
+
* @param localAddr - Local address to bind (e.g., "192.168.1.100:0")
|
|
19
|
+
* @param endpoint - Server address (e.g., "192.168.1.100:8080")
|
|
20
|
+
*/
|
|
21
|
+
async function connectWithBind(id, localAddr, endpoint) {
|
|
22
|
+
await invoke('plugin:tcpclient|connect_with_bind', { id, localAddr, endpoint });
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Disconnect from a TCP server
|
|
26
|
+
* @param id - Connection identifier
|
|
27
|
+
*/
|
|
28
|
+
async function disconnect(id) {
|
|
29
|
+
await invoke('plugin:tcpclient|disconnect', { id });
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Send data to the TCP server
|
|
33
|
+
* @param id - Connection identifier
|
|
34
|
+
* @param data - Data to send (as byte array or Uint8Array)
|
|
35
|
+
*/
|
|
36
|
+
async function send(id, data) {
|
|
37
|
+
const dataArray = data instanceof Uint8Array ? Array.from(data) : data;
|
|
38
|
+
await invoke('plugin:tcpclient|send', { id, data: dataArray });
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Send a string to the TCP server (UTF-8 encoded)
|
|
42
|
+
* @param id - Connection identifier
|
|
43
|
+
* @param text - Text to send
|
|
44
|
+
*/
|
|
45
|
+
async function sendString(id, text) {
|
|
46
|
+
const encoder = new TextEncoder();
|
|
47
|
+
await send(id, encoder.encode(text));
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Check if a connection exists
|
|
51
|
+
* @param id - Connection identifier
|
|
52
|
+
*/
|
|
53
|
+
async function isConnected(id) {
|
|
54
|
+
return await invoke('plugin:tcpclient|is_connected', { id });
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Get all active connection IDs
|
|
58
|
+
*/
|
|
59
|
+
async function getConnections() {
|
|
60
|
+
return await invoke('plugin:tcpclient|get_connections');
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Listen for TCP client events
|
|
64
|
+
* @param callback - Event callback function
|
|
65
|
+
*/
|
|
66
|
+
async function listenEvents(callback) {
|
|
67
|
+
return await listen('plugin://tcpclient', callback);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Decode message data to string
|
|
71
|
+
*/
|
|
72
|
+
function decodeMessage(data) {
|
|
73
|
+
return new TextDecoder().decode(new Uint8Array(data));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export { connect, connectWithBind, decodeMessage, disconnect, getConnections, isConnected, listenEvents, send, sendString };
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "tauri-plugin-tcpclient-api",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"author": "x93008",
|
|
5
|
+
"description": "A Tauri plugin for TCP client connections",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/x93008/tauri-plugin-tcpclient"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"types": "./dist-js/index.d.ts",
|
|
13
|
+
"main": "./dist-js/index.cjs",
|
|
14
|
+
"module": "./dist-js/index.js",
|
|
15
|
+
"exports": {
|
|
16
|
+
"types": "./dist-js/index.d.ts",
|
|
17
|
+
"import": "./dist-js/index.js",
|
|
18
|
+
"require": "./dist-js/index.cjs"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist-js",
|
|
22
|
+
"README.md"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "rollup -c",
|
|
26
|
+
"prepublishOnly": "npm run build",
|
|
27
|
+
"pretest": "npm run build"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@tauri-apps/api": "^2.0.0"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@rollup/plugin-typescript": "^12.0.0",
|
|
34
|
+
"rollup": "^4.9.6",
|
|
35
|
+
"typescript": "^5.3.3",
|
|
36
|
+
"tslib": "^2.6.2"
|
|
37
|
+
},
|
|
38
|
+
"keywords": [
|
|
39
|
+
"tauri",
|
|
40
|
+
"tcp",
|
|
41
|
+
"client",
|
|
42
|
+
"network",
|
|
43
|
+
"plugin"
|
|
44
|
+
]
|
|
45
|
+
}
|