You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
746 B
36 lines
746 B
class Connection {
|
|
|
|
constructor() {
|
|
this.ws = null;
|
|
this.isConnected = false;
|
|
this.onopen = () => {};
|
|
this.onclose = () => {};
|
|
this.onmessage = () => {};
|
|
}
|
|
|
|
connectTo(url) {
|
|
if (this.ws) { this.close(); }
|
|
|
|
this.ws = new WebSocket(url, 'fs');
|
|
this.ws.onopen = () => {
|
|
this.isConnected = true;
|
|
this.onopen()
|
|
};
|
|
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();
|
|
}
|
|
|
|
} |