37 lines
848 B
JavaScript
37 lines
848 B
JavaScript
import WebSocket from "ws";
|
|
import { resolve } from "dns";
|
|
import { rejects } from "assert";
|
|
|
|
export default class Player {
|
|
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
|
|
}
|
|
}
|