2019-01-28 21:08:37 +00:00
|
|
|
class Connection {
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.ws = null;
|
|
|
|
this.isConnected = false;
|
|
|
|
this.onopen = () => {};
|
|
|
|
this.onclose = () => {};
|
|
|
|
this.onmessage = () => {};
|
|
|
|
}
|
|
|
|
|
|
|
|
connectTo(url) {
|
|
|
|
if (this.ws) { this.close(); }
|
|
|
|
|
2019-02-13 20:09:43 +00:00
|
|
|
this.ws = new WebSocket(url, "fs");
|
2019-01-28 21:08:37 +00:00
|
|
|
this.ws.onopen = () => {
|
|
|
|
this.isConnected = true;
|
2019-02-13 20:09:43 +00:00
|
|
|
this.onopen();
|
2019-01-28 21:08:37 +00:00
|
|
|
};
|
|
|
|
this.ws.onclose = () => {
|
|
|
|
this.isConnected = false;
|
|
|
|
this.onclose();
|
|
|
|
};
|
|
|
|
this.ws.onmessage = (message) => {
|
|
|
|
this.onmessage(message);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
send(message) {
|
|
|
|
this.ws.send(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
|
|
|
this.ws.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|