airfone-client-js
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -10,12 +10,20 @@ export declare type StatusEvent = 'connect' | 'disconnect' | 'online' | 'offline' | 'error'; | ||
} | ||
/** | ||
* 链接状态 0: 未连接 1:连接中 2:链接成功 3:链接失败 | ||
*/ | ||
export declare type ConnectState = 0 | 1 | 2 | 3; | ||
export default class AirfoneClient { | ||
readonly options: ClientOptions; | ||
private events; | ||
private listeners; | ||
private socket; | ||
private state; | ||
constructor(options: ClientOptions); | ||
connect(): AirfoneClient; | ||
disconnect(): void; | ||
chat(id: String, payload: any, receive?: Function): void; | ||
notice(role: String, payload: any): void; | ||
broadcast(payload: any): void; | ||
on(event: StatusEvent | MessageEvent, handle: Function): this; | ||
on(event: StatusEvent | MessageEvent | String, handle: Function): AirfoneClient; | ||
} | ||
export declare function getClient(options: ClientOptions): AirfoneClient; |
@@ -22,3 +22,9 @@ import { io } from 'socket.io-client'; | ||
this.events = new Map(); | ||
let { id, room, role, server } = this.options = options; | ||
this.listeners = new Map(); | ||
this.state = 0; | ||
this.options = options; | ||
} | ||
connect() { | ||
let { id, room, role, server } = this.options; | ||
this.state = 1; | ||
this.socket = io(`${win.AF_SERVER || server || '/'}`, { | ||
@@ -28,8 +34,9 @@ query: { id, room, role }, | ||
secure: true | ||
}); | ||
this.socket.on('connect', () => { | ||
}).compress(true).on('connect', () => { | ||
var _a; | ||
this.state = 2; | ||
(_a = this.events.get('connect')) === null || _a === void 0 ? void 0 : _a.call(this, { id, room, role }); | ||
}).on('disconnect', (reason) => { | ||
var _a; | ||
this.state = 0; | ||
(_a = this.events.get('disconnect')) === null || _a === void 0 ? void 0 : _a.call(this, { id, room, role, reason }); | ||
@@ -53,6 +60,22 @@ }).on(`${room}/${id}`, (data) => { | ||
var _a; | ||
this.state = 3; | ||
(_a = this.events.get('error')) === null || _a === void 0 ? void 0 : _a.call(this, error); | ||
}); | ||
this.listeners.forEach((handle, event) => { | ||
this.socket.on(`${room}/${event}`, (payload) => { | ||
handle.call(this, payload); | ||
}); | ||
}); | ||
return this; | ||
} | ||
disconnect() { | ||
if (this.state === 2) { | ||
this.socket.disconnect(); | ||
} | ||
this.state = 0; | ||
} | ||
chat(id, payload, receive) { | ||
if (this.state !== 2) { | ||
throw new Error('连接未建立,无法发送消息'); | ||
} | ||
if (receive) { | ||
@@ -70,2 +93,5 @@ let _uid = uuid.v4(); | ||
notice(role, payload) { | ||
if (this.state !== 2) { | ||
throw new Error('连接未建立,无法发送消息'); | ||
} | ||
this.socket.send(path, { | ||
@@ -78,2 +104,5 @@ to: role, | ||
broadcast(payload) { | ||
if (this.state !== 2) { | ||
throw new Error('连接未建立,无法发送消息'); | ||
} | ||
this.socket.send(path, { | ||
@@ -85,3 +114,12 @@ event: EventType.BROADCAST, | ||
on(event, handle) { | ||
this.events.set(event, handle); | ||
if (this.state != 0) { | ||
throw new Error('监听必须在调用connect方法之前添加'); | ||
} | ||
try { | ||
// @ts-ignore | ||
this.events.set(event, handle); | ||
} | ||
catch (e) { | ||
this.listeners.set(event, handle); | ||
} | ||
return this; | ||
@@ -88,0 +126,0 @@ } |
{ | ||
"name": "airfone-client-js", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6970
152