118 lines
2.8 KiB
TypeScript
118 lines
2.8 KiB
TypeScript
|
import Singleton from '../Base/Singleton'
|
|||
|
|
|||
|
const TIMEOUT = 5000
|
|||
|
|
|||
|
export type IData = Record<string, any>
|
|||
|
|
|||
|
export interface ICallApiRet {
|
|||
|
success: boolean;
|
|||
|
error?: Error;
|
|||
|
res?: IData
|
|||
|
}
|
|||
|
|
|||
|
export enum ApiMsgEnum {
|
|||
|
ApiPlayerList = 'ApiPlayerList',
|
|||
|
ApiPlayerJoin = 'ApiPlayerJoin',
|
|||
|
ApiRoomList = 'ApiRoomList',
|
|||
|
ApiRoomCreate = 'ApiRoomCreate',
|
|||
|
ApiRoomJoin = 'ApiRoomJoin',
|
|||
|
ApiRoomLeave = 'ApiRoomLeave',
|
|||
|
MsgPlayerList = 'MsgPlayerList',
|
|||
|
MsgRoomList = 'MsgRoomList',
|
|||
|
MsgRoom = 'MsgRoom',
|
|||
|
}
|
|||
|
|
|||
|
export default class NetworkManager extends Singleton {
|
|||
|
static get Instance() {
|
|||
|
return super.GetInstance<NetworkManager>()
|
|||
|
}
|
|||
|
|
|||
|
ws: WebSocket
|
|||
|
port = 8888
|
|||
|
cbs: Map<string, Function[]> = new Map()
|
|||
|
isConnected = false
|
|||
|
|
|||
|
connect() {
|
|||
|
return new Promise((resolve, reject) => {
|
|||
|
this.ws = new WebSocket(`ws://localhost:${this.port}`)
|
|||
|
this.ws.onopen = () => {
|
|||
|
console.log("ws onopen")
|
|||
|
this.isConnected = true
|
|||
|
resolve(true)
|
|||
|
}
|
|||
|
|
|||
|
this.ws.onerror = () => {
|
|||
|
this.isConnected = false
|
|||
|
reject("ws onerror")
|
|||
|
}
|
|||
|
|
|||
|
this.ws.onclose = () => {
|
|||
|
this.isConnected = false
|
|||
|
reject("ws onclose")
|
|||
|
}
|
|||
|
|
|||
|
this.ws.onmessage = (e) => {
|
|||
|
try {
|
|||
|
const json = JSON.parse(e.data)
|
|||
|
const { name, data } = json
|
|||
|
try {
|
|||
|
if (this.cbs.has(name) && this.cbs.get(name).length) {
|
|||
|
console.log(json);
|
|||
|
this.cbs.get(name).forEach(cb => cb(data))
|
|||
|
}
|
|||
|
} catch (error) {
|
|||
|
console.log("this.cbs.get(name).forEach(cb => cb(restData))", error)
|
|||
|
}
|
|||
|
|
|||
|
} catch (error) {
|
|||
|
console.log('解析失败,不是合法的JSON格式', error)
|
|||
|
}
|
|||
|
}
|
|||
|
})
|
|||
|
}
|
|||
|
|
|||
|
sendMsg(name: string, data: IData) {
|
|||
|
this.ws.send(JSON.stringify({ name, data }))
|
|||
|
}
|
|||
|
|
|||
|
listenMsg(name: string, cb: Function) {
|
|||
|
if (this.cbs.has(name)) {
|
|||
|
this.cbs.get(name).push(cb)
|
|||
|
} else {
|
|||
|
this.cbs.set(name, [cb])
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
unlistenMsg(name: string, cb: Function) {
|
|||
|
if (this.cbs.has(name)) {
|
|||
|
const index = this.cbs.get(name).indexOf(cb)
|
|||
|
index > -1 && this.cbs.get(name).splice(index, 1)
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
callApi(name: string, data: IData) {
|
|||
|
return new Promise<ICallApiRet>((resolve) => {
|
|||
|
try {
|
|||
|
// 超时处理
|
|||
|
const timer = setTimeout(() => {
|
|||
|
resolve({ success: false, error: new Error('timeout') })
|
|||
|
this.unlistenMsg(name, cb)
|
|||
|
}, TIMEOUT)
|
|||
|
|
|||
|
// 回调处理
|
|||
|
const cb = (res: ICallApiRet) => {
|
|||
|
resolve(res)
|
|||
|
clearTimeout(timer)
|
|||
|
this.unlistenMsg(name, cb)
|
|||
|
}
|
|||
|
this.listenMsg(name, cb)
|
|||
|
|
|||
|
this.ws.send(JSON.stringify({ name, data }))
|
|||
|
} catch (error) {
|
|||
|
console.log(error)
|
|||
|
resolve({ success: false, error: error as Error })
|
|||
|
}
|
|||
|
})
|
|||
|
}
|
|||
|
}
|