pj-ws-api-client
v1.0.6
Published
品杰WS API 客户端
Downloads
81
Readme
定义API(可通过工具生成)
// @ts-nocheck
import {BaseServerApi} from "pj-ws-api-client";
export class Account extends BaseServerApi {
constructor() {
super("Account");
this
._RegMethod("login", ["userName", "password", "verifyCode"])
._RegMethod("logout", ["token"])
._RegMethod("verifyToken", ["token"])
}
login(name: string, pwd: string, verifyCode: string): Promise<any> {
return Promise.reject(new Error("Method not implemented."));
}
logout(token: string): Promise<boolean> {
return Promise.reject(new Error("Method not implemented."));
}
verifyToken(token: string): Promise<any> {
return Promise.reject(new Error("Method not implemented."));
}
}
使用
<script setup lang="ts">
import {onMounted, ref} from "vue";
import {ApiClient} from "pj-ws-api-client";
import {Account} from "./Api/Account.ts";
import {Echo} from "./Api/Echo.ts";
const client = new ApiClient("ws://192.168.0.84:8604");
const myInfo = ref<string>("");
function hello(name: string): string {
console.log("收到服务器调用");
return `hello ${name}, i'm js`;
}
function helloAsync(name: string): Promise<string> {
return new Promise<string>((acc, rej) => {
console.log("收到服务器调用Async");
acc(`hello ${name}, i'm js via promise`);
})
}
onMounted(async () => {
client.start();
try {
client.register(hello, this);
client.register(helloAsync, this);
client.addEventHandler("onpush", (sender: any, args: any[]) => {
console.log("收到服务器消息", args);
});
client.addEventHandler("connected", (sender: any) => {
console.log("已经登录成功");
})
await client.waitConnected();
let a = await client.getProxy<Account>(Account).login("admin", "e10adc3949ba59abbe56e057f20f883e", "xxx");
console.log(`login result`, a);
client.addHeader("A-Token", a.AccessToken);
myInfo.value = await client.getProxy<Echo>(Echo).myInfo();
let c = await client.getProxy<Account>(Account).verifyToken(a.AccessToken);
console.log(`verify token result`, c);
let b = await client.getProxy<Account>(Account).logout(a.AccessToken);
console.log(`logout result`, b);
} catch (e) {
console.warn(e);
}
})
</script>