2019-01-28 21:08:37 +00:00
|
|
|
class ConnectionView {
|
|
|
|
constructor(connection) {
|
|
|
|
this.connection = connection;
|
|
|
|
this.connection.onclose = () => { this.onConnectionClosed(); };
|
|
|
|
this.connection.onopen = () => { this.onConnectionOpened(); };
|
|
|
|
|
2019-02-13 20:09:43 +00:00
|
|
|
this.element = document.createElement("div");
|
2019-01-28 21:08:37 +00:00
|
|
|
|
2019-02-13 20:09:43 +00:00
|
|
|
let urlLabel = document.createElement("span");
|
|
|
|
urlLabel.textContent = "URL:";
|
2019-01-28 21:08:37 +00:00
|
|
|
this.element.appendChild(urlLabel);
|
|
|
|
|
2019-02-13 20:09:43 +00:00
|
|
|
this.urlTextbox = document.createElement("input");
|
|
|
|
this.urlTextbox.type = "text";
|
2019-01-28 21:08:37 +00:00
|
|
|
this.urlTextbox.value = window.location.href.replace(/^http/, "ws");
|
|
|
|
this.element.appendChild(this.urlTextbox);
|
|
|
|
|
2019-02-13 20:09:43 +00:00
|
|
|
this.connectButton = document.createElement("input");
|
|
|
|
this.connectButton.type = "button";
|
|
|
|
this.connectButton.value = "connect";
|
|
|
|
this.connectButton.addEventListener("click", () => { this.onConnectButtonClicked(); });
|
2019-01-28 21:08:37 +00:00
|
|
|
this.element.appendChild(this.connectButton);
|
|
|
|
}
|
|
|
|
|
|
|
|
onConnectButtonClicked() {
|
|
|
|
if (!this.connection.isConnected) {
|
|
|
|
let url = this.urlTextbox.value;
|
|
|
|
this.connection.connectTo(url);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.connection.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
onConnectionOpened() {
|
2019-02-13 20:09:43 +00:00
|
|
|
this.connectButton.value = "disconnect";
|
2019-01-28 21:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onConnectionClosed() {
|
2019-02-13 20:09:43 +00:00
|
|
|
this.connectButton.value = "connect";
|
2019-01-28 21:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|