403Webshell
Server IP : 80.87.202.40  /  Your IP : 216.73.216.169
Web Server : Apache
System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64
User : bitrix ( 600)
PHP Version : 8.2.27
Disable Function : NONE
MySQL : OFF |  cURL : ON |  WGET : ON |  Perl : ON |  Python : OFF |  Sudo : ON |  Pkexec : ON
Directory :  /home/bitrix/ext_www/rospirotorg.ru/bitrix/js/pull/connector/src/transport/

Upload File :
current_dir [ Writeable] document_root [ Writeable]

 

Command :


[ Back ]     

Current File : /home/bitrix/ext_www/rospirotorg.ru/bitrix/js/pull/connector/src/transport/websocket.js
/* eslint-disable @bitrix24/bitrix24-rules/no-native-events-binding */

import { ConnectionType } from '../../../client/src/consts';
import { AbstractConnector } from './base';
import { getDateForLog } from '../../../util/src/util';

export class WebSocketConnector extends AbstractConnector
{
	socket: ?WebSocket;
	connectionType = ConnectionType.WebSocket;

	onSocketOpenHandler = this.onSocketOpen.bind(this);
	onSocketCloseHandler = this.onSocketClose.bind(this);
	onSocketErrorHandler = this.onSocketError.bind(this);
	onSocketMessageHandler = this.onSocketMessage.bind(this);

	connect()
	{
		if (this.socket)
		{
			if (this.socket.readyState === WebSocket.OPEN || this.socket.readyState === WebSocket.CONNECTING)
			{
				return;
			}

			this.socket.removeEventListener('open', this.onSocketOpenHandler);
			this.socket.removeEventListener('close', this.onSocketCloseHandler);
			this.socket.removeEventListener('error', this.onSocketErrorHandler);
			this.socket.removeEventListener('message', this.onSocketMessageHandler);

			this.socket.close();
			this.socket = null;
		}

		this.createSocket();
	}

	disconnect(code, message)
	{
		if (this.socket !== null)
		{
			this.socket.removeEventListener('open', this.onSocketOpenHandler);
			this.socket.removeEventListener('close', this.onSocketCloseHandler);
			this.socket.removeEventListener('error', this.onSocketErrorHandler);
			this.socket.removeEventListener('message', this.onSocketMessageHandler);

			this.socket.close(code, message);
		}
		this.socket = null;
		this.disconnectCode = code;
		this.disconnectReason = message;
		this.connected = false;
	}

	createSocket()
	{
		if (this.socket)
		{
			throw new Error('Socket already exists');
		}

		if (!this.path)
		{
			throw new Error('Websocket connection path is not defined');
		}

		this.socket = new WebSocket(this.path);
		this.socket.binaryType = 'arraybuffer';

		this.socket.addEventListener('open', this.onSocketOpenHandler);
		this.socket.addEventListener('close', this.onSocketCloseHandler);
		this.socket.addEventListener('error', this.onSocketErrorHandler);
		this.socket.addEventListener('message', this.onSocketMessageHandler);
	}

	/**
	 * Sends some data to the server via websocket connection.
	 * @param {ArrayBuffer} buffer Data to send.
	 */
	send(buffer): boolean
	{
		if (!this.socket || this.socket.readyState !== 1)
		{
			console.error(`${getDateForLog()}: Pull: WebSocket is not connected`);

			return false;
		}

		this.socket.send(buffer);

		return true;
	}

	onSocketOpen()
	{
		this.connected = true;
	}

	onSocketClose(e)
	{
		this.socket = null;
		this.disconnectCode = e.code;
		this.disconnectReason = e.reason;
		this.connected = false;
	}

	onSocketError(e)
	{
		this.callbacks.onError(e);
	}

	onSocketMessage(e)
	{
		this.callbacks.onMessage(e.data);
	}

	destroy()
	{
		if (this.socket)
		{
			this.socket.close();
			this.socket = null;
		}
	}
}

Youez - 2016 - github.com/yon3zu
LinuXploit