更新记录
1.0.1(2024-12-07)
readme.md
1.0.0(2024-12-07)
1.0.0
平台兼容性
App |
快应用 |
微信小程序 |
支付宝小程序 |
百度小程序 |
字节小程序 |
QQ小程序 |
HBuilderX 4.29,Android:支持,iOS:不支持,HarmonyNext:不支持 |
× |
× |
× |
× |
× |
× |
钉钉小程序 |
快手小程序 |
飞书小程序 |
京东小程序 |
× |
× |
× |
× |
H5-Safari |
Android Browser |
微信浏览器(Android) |
QQ浏览器(Android) |
Chrome |
IE |
Edge |
Firefox |
PC-Safari |
× |
× |
× |
× |
× |
× |
× |
× |
× |
xn-socket
开发文档
UTS 语法
UTS API插件
UTS uni-app兼容模式组件
UTS 标准模式组件
Hello UTS
import { TCPSocket } from '@/uni_modules/xn-socket';
export class TCPClient {
private TCPSocket: {connect: Function, disconnect: Function, listener: Function, isClosed: Function, sendBytes: Function, sendStr: Function};
isConnect: boolean | null = null;
channel: string;
connectCallback: Function = () => {};
constructor({ip, port, channel, timeout, onMessage}: {ip: string, port: number, channel?: string, timeout?: number, onMessage?: Function}) {
this.channel = channel ?? Date.now().toString(36) + Math.random().toString(36).substr(2, 9);
this.TCPSocket = new TCPSocket({
host: ip,
port: port,
timeout: timeout ?? 5000,
});
this.TCPSocket.listener({
onConnect: (data: {key: string, code: number, messages: string}) => {
console.error("TCP", "onConnect", data.messages, data.code);
if (data.code == 1){
this.isConnect = true;
this.connectCallback(true);
} else {
this.isConnect = false;
this.connectCallback(false);
}
},
onMessage: (result: {data: string, dataBytes: Array<number>}) => {
// console.error("TCP", "onMessage", result.data);
const str = ab2str(result.dataBytes.splice(4, result.dataBytes.length));
if (typeof onMessage == 'function') {
onMessage(str, this.channel);
}
},
onSendSuccess: (data: Array<number>) => {
// const str = ab2str(data.splice(4, data.length));
// console.error("TCP", "onSendSuccess", str);
}
})
}
connect() {
return new Promise((resolve, reject) => {
this.isConnect = null;
this.connectCallback = (value: boolean) => {
resolve(value);
}
this.TCPSocket.connect();
})
}
disconnect() {
console.error("TCP", `disconnect`);
this.TCPSocket.disconnect();
}
isConnected() {
let isClosed = this.TCPSocket.isClosed();
// console.error("TCP", `${isClosed} ${this.isConnect}`, `isConnected ${this.isConnect != null && !isClosed}`);
if (this.isConnect != null && !isClosed) {
return true;
}
return false;
}
// 发送字符串消息
sendStr = (values: {message: string}) => {
this.TCPSocket.sendStr(values);
}
// 发送Hex字符串消息
sendBytes = (buffer: Uint8Array) => {
let data = Array.from(buffer);
let bytesLength: Array<number> = int4byte(data.length);
data.unshift(...bytesLength);
// console.error("TCP", "sendBytes", data);
this.TCPSocket.sendBytes(data);
}
}
const ab2str = function (buf: Array<number>) {
return String.fromCharCode.apply(null, Array.from(new Uint8Array(buf)));
}
export const int4byte = (value: number): Array<number> => {
let bytes = [];
for (let i = 0; i < 4; i++) {
bytes[i] = (value >> 8 * (3 - i)) & 0xFF;
}
return bytes;
}
export const byte4Int = (bytes: Array<number>) => {
let value = 0;
for (let i = 0; i < 4; i++) {
value |= (bytes[i] & 0xFF) << (8 * (3 - i));
}
return value;
}