45 lines
881 B
TypeScript
45 lines
881 B
TypeScript
import Singleton from '../Base/Singleton'
|
|
|
|
export default class NetworkManager extends Singleton {
|
|
static get Instance() {
|
|
return super.GetInstance<NetworkManager>()
|
|
}
|
|
|
|
ws: WebSocket
|
|
port = 8888
|
|
|
|
connect() {
|
|
return new Promise((resolve, reject) => {
|
|
this.ws = new WebSocket(`ws://localhost:${this.port}`)
|
|
this.ws.onopen = () => {
|
|
console.log("ws onopen")
|
|
resolve(null)
|
|
}
|
|
|
|
this.ws.onerror = () => {
|
|
console.log("ws onerror")
|
|
reject()
|
|
}
|
|
|
|
this.ws.onclose = () => {
|
|
console.log("ws onclose")
|
|
reject()
|
|
}
|
|
|
|
this.ws.onmessage = (data) => {
|
|
console.log("onmessage", data);
|
|
|
|
}
|
|
})
|
|
|
|
}
|
|
|
|
sendMsg(data) {
|
|
this.ws.send(data)
|
|
}
|
|
|
|
async callApi(path: string) {
|
|
return await fetch(`http://localhost:${this.port}${path}`).then((res) => res.json())
|
|
}
|
|
}
|