Calls: Fix leaving on page unload (#4248)

This commit is contained in:
Alexander Zinchuk 2024-02-06 16:49:37 +01:00
parent 588c7a95b5
commit 96c87c9755
4 changed files with 9 additions and 5 deletions

View File

@ -176,7 +176,7 @@ function onUpdate(update: ApiUpdate) {
sendUpdatesOnTickEnd();
}
export function sendToOrigin(data: WorkerMessageData, arrayBuffer?: ArrayBuffer) {
function sendToOrigin(data: WorkerMessageData, arrayBuffer?: ArrayBuffer) {
if (arrayBuffer) {
postMessage(data, [arrayBuffer]);
} else {

View File

@ -41,7 +41,7 @@ class HttpStream {
return data;
}
getURL(ip: string, port: number, testServers: boolean, isPremium: boolean) {
static getURL(ip: string, port: number, testServers: boolean, isPremium: boolean) {
if (port === 443) {
return `https://${ip}:${port}/apiw1${testServers ? '_test' : ''}${isPremium ? '_premium' : ''}`;
} else {
@ -55,7 +55,7 @@ class HttpStream {
this.resolveRead = resolve;
this.rejectRead = reject;
});
this.url = this.getURL(ip, port, testServers, isPremium);
this.url = HttpStream.getURL(ip, port, testServers, isPremium);
await fetch(this.url, {
method: 'POST',

View File

@ -40,7 +40,6 @@ const { SecurityError } = require('../errors/Common');
const { InvalidBufferError } = require('../errors/Common');
const { RPCMessageToError } = require('../errors');
const { TypeNotFoundError } = require('../errors/Common');
const { sendToOrigin } = require('../../../api/gramjs/worker/worker');
const LONGPOLL_MAX_WAIT = 3000;
const LONGPOLL_MAX_DELAY = 500;
@ -353,7 +352,11 @@ class MTProtoSender {
const data = await this._send_queue.getBeacon(state);
const encryptedData = await this._state.encryptMessageData(data);
sendToOrigin({ type: 'sendBeacon', data: encryptedData, url: this._fallbackConnection.socket.website });
postMessage({
type: 'sendBeacon',
data: encryptedData,
url: this._fallbackConnection.href,
});
}
/**

View File

@ -189,6 +189,7 @@ class HttpConnection extends Connection {
super(ip, port, dcId, loggers, testServers, isPremium);
this.shouldLongPoll = true;
this.socket = new HttpStream(this.disconnectCallback.bind(this));
this.href = HttpStream.getURL(this._ip, this._port, this._testServers, this._isPremium);
}
send(data) {