mlp-server-tools/tools/test-draft/wsplayer.mjs

38 lines
848 B
JavaScript
Raw Normal View History

2019-07-04 14:49:14 +00:00
import WebSocket from "ws";
import { resolve } from "dns";
import { rejects } from "assert";
export default class Player {
2019-07-04 14:49:14 +00:00
constructor(wsURL, authToken) {
this.wsURL = wsURL;
this.authToken = authToken;
}
async connect() {
this.ws = new WebSocket(this.wsURL);
return new Promise((resolve, reject) => {
this.ws.on("message", data => {
this.ws.removeAllListeners("message");
this.ws.on("message", this._onMessage);
resolve(JSON.parse(data));
});
this.ws.on("open", () => {
this.send({
token: this.authToken
});
});
this.ws.on("close", () => reject("connection closed"));
});
}
send(data) {
const jsonData = JSON.stringify(data);
this.ws.send(jsonData);
}
_onMessage(msg) {
const data = JSON.parse(msg);
//TODO
}
}