41 lines
1.4 KiB
JavaScript
41 lines
1.4 KiB
JavaScript
class ControllerBase {
|
|
ProcessResponse(response) {
|
|
alert("Вызов нереализованного метода. Сообщите об этой ошибке разработчикам");
|
|
}
|
|
|
|
constructor(url, path) {
|
|
this.socket_ = new WebSocket(url + "/" + path);
|
|
|
|
// При создании сокета
|
|
this.socket_.onopen = function(event) {
|
|
console.log("connected to server");
|
|
};
|
|
|
|
// При получении ответа
|
|
var self = this;
|
|
this.socket_.onmessage = function(event) {
|
|
console.log(`received: '${event.data}'`);
|
|
|
|
let response = JSON.parse(event.data);
|
|
self.ProcessResponse(response);
|
|
};
|
|
|
|
// При закрытии сокета
|
|
this.socket_.onclose = function(event) {
|
|
if (event.wasClean)
|
|
console.log("connection closed");
|
|
else
|
|
console.log("failed to close connection");
|
|
};
|
|
|
|
// При ошибке
|
|
this.socket_.onerror = function(error) {
|
|
alert(`Возникла ошибка при подключении к серверу. Пожалуйста, сообщите о ней разработчикам: ${error}`);
|
|
};
|
|
}
|
|
|
|
SendRequestImpl(request) {
|
|
this.socket_.send(request);
|
|
}
|
|
}
|