@wuilmerj24/signalr
by wuilmerj24 | v1.0.6
添加插件描述
npm i --save @wuilmerj24/signalr
npm

@wuilmerj24/signalr

npm install @wuilmerj24/signalr

API

方法 参数 返回
create OptionsConnect Promise
disconnect void
conectionId string
on eventName:string,callback: (...payload: any[]) void
off eventName:string void
emit eventName:string,callback: (...payload: any[]) void
isConnected EStateConn

接口

export interface OptionsConnect {
url: string;
accessToken?: string;
shouldSkipNegotiate?: boolean;
headers?: Array<IHeaders>;
handshakeResponseTimeout?: number;
keepAliveInterval?: number;
serverTimeout?: number;
transport?: TypeTransport;
reconnect?: boolean;
}

枚举

export enum TypeTransport {
ALL = 'ALL',
WEBSOCKETS = 'WEBSOCKETS',
LONG_POLLING = 'LONG_POLLING',
}

export enum EStateConn {
CONNECTED = 'CONNECTED',
CONNECTING = 'CONNECTING',
DISCONNECTED = 'DISCONNECTED',
}

示例

const signalr: Signalr = new Signalr();

export async function connect() {
await signalr
.create({
url: 'url',
shouldSkipNegotiate: false,
headers: [{ name: 'Content-Type', value: 'application/json' }],
transport: TypeTransport.WEBSOCKETS,
reconnect: true,
})
.then((res) => {
console.log('finish ', res);
})
.catch((err) => {
console.log('error ', err);
});
}

export async function desconectar() {
await signalr.disconnect();
}

export async function getStaateConn() {
alert(signalr.isConnected);
console.log("id",signalr.conectionId())
;
}

export async function invoke() {
signalr.emit('evento', null);
}

export async function ons() {
signalr.on('evento', (data) => {
console.log(data);
});
}

许可

Apache License Version 2.0