<commit_before><?php

namespace DroneMill\EventsocketClient;

use Closure;
use Exception;
use InvalidArgumentException;
use WebSocket\Client as WebsocketClient;

class ClientException extends Exception {}

class Client {

	const MESSAGE_TYPE_BROADCAST = 1;
	const MESSAGE_TYPE_STANDARD = 2;
	const MESSAGE_TYPE_REQUEST = 3;
	const MESSAGE_TYPE_REPLY = 4;
	const MESSAGE_TYPE_SUSCRIBE = 5;
	const MESSAGE_TYPE_UNSUSCRIBE = 6;

	const ERROR_REQUEST_CLIENT_NO_EXIST = 'ErrorRequestClientNoExist';
	const ERROR_REQUEST_CLIENT_NOT_CONNECTED = 'ErrorRequestClientNotConnected';


	/**
	 * the Eventsocket server url
	 *
	 * @var  string
	 */
	protected $serverUrl;

	/**
	 * The client object returned from the server
	 *
	 * @var  stdClass
	 */
	protected $client;

	/**
	 * The websocket connection
	 *
	 * @var  WebsocketClient
	 */
	protected $ws;

	/**
	 * the message handlers
	 *
	 * @var  []
	 */
	protected $handlers = [
		self::MESSAGE_TYPE_BROADCAST  => [],
		self::MESSAGE_TYPE_STANDARD   => [],
		self::MESSAGE_TYPE_REQUEST    => [],
		self::MESSAGE_TYPE_REPLY      => [], // these are the live request handlers
	];


	///////////////////////////
	// BEGIN PUBLIC METHODS //
	///////////////////////////

	/**
	 * Construct a new Eventsocket Client.
	 *
	 * @param  [type]  $serverUrl  [description]
	 */
	public function __construct($serverUrl)
	{
		if (empty($serverUrl))
		{
			throw new InvalidArgumentException("Server URL cannot be empty");
		}

		$this->serverUrl = $serverUrl;

		$this->newClient();
		$this->connectWs();
	}

	/**
	 * Utility method to get the client id
	 *
	 * @return  string  the client id
	 */
	public function getId()
	{
		return $this->client->Id;
	}

	/**
	 * Register a broadcast handler
	 *
	 * @param   Closure  $handler  [description]
	 * @return  void
	 */
	public function registerBroadcastHandler(Closure $handler)
	{
		$this->handlers[static::MESSAGE_TYPE_BROADCAST][] = $handler;
	}

	/**
	 * Register a request handler
	 *
	 * @param   Closure  $handler  [description]
	 * @return  void
	 */
	public function registerRequestHandler(Closure $handler)
	{
		$this->handlers[static::MESSAGE_TYPE_REQUEST][] = $handler;
	}

	/**
	 * Receive a message from the socket
	 *
	 * @return void
	 */
	public function recv()
	{
		// receive a message from the socket
		$message = $this->ws->receive();

		// is the message is empty?
		if (empty($message))
		{
			echo 'Did not receive anything from socket...' . PHP_EOL;
			return;
		}

		// decode the message (json_decode + error checking)
		$message = $this->decodeMessage($message);

		// injest and route the message
		$this->injest($message);
	}

	/**
	 * Suscribe to a given event
	 *
	 * @param   string|[]  $event    the event to suscribe to
	 * @param   Closure    $handler  the event handler to be called
	 *   upon receiving of the event
	 * @return  void
	 */
	public function suscribe($events, Closure $handler)
	{
		if (is_string($events)) $events = [$events];

		// store the handler for each of the events it represents
		foreach ($events as $event)
		{
			// check that we have a place to store the handler
			if (! array_key_exists($event, $this->handlers[static::MESSAGE_TYPE_STANDARD]))
			{
				$this->handlers[static::MESSAGE_TYPE_STANDARD][$event] = [];
			}
<commit_msg>Remove pesky Echo statement!<commit_after><?php

namespace DroneMill\EventsocketClient;

use Closure;
use Exception;
use InvalidArgumentException;
use WebSocket\Client as WebsocketClient;

class ClientException extends Exception {}

class Client {

	const MESSAGE_TYPE_BROADCAST = 1;
	const MESSAGE_TYPE_STANDARD = 2;
	const MESSAGE_TYPE_REQUEST = 3;
	const MESSAGE_TYPE_REPLY = 4;
	const MESSAGE_TYPE_SUSCRIBE = 5;
	const MESSAGE_TYPE_UNSUSCRIBE = 6;

	const ERROR_REQUEST_CLIENT_NO_EXIST = 'ErrorRequestClientNoExist';
	const ERROR_REQUEST_CLIENT_NOT_CONNECTED = 'ErrorRequestClientNotConnected';


	/**
	 * the Eventsocket server url
	 *
	 * @var  string
	 */
	protected $serverUrl;

	/**
	 * The client object returned from the server
	 *
	 * @var  stdClass
	 */
	protected $client;

	/**
	 * The websocket connection
	 *
	 * @var  WebsocketClient
	 */
	protected $ws;

	/**
	 * the message handlers
	 *
	 * @var  []
	 */
	protected $handlers = [
		self::MESSAGE_TYPE_BROADCAST  => [],
		self::MESSAGE_TYPE_STANDARD   => [],
		self::MESSAGE_TYPE_REQUEST    => [],
		self::MESSAGE_TYPE_REPLY      => [], // these are the live request handlers
	];


	///////////////////////////
	// BEGIN PUBLIC METHODS //
	///////////////////////////

	/**
	 * Construct a new Eventsocket Client.
	 *
	 * @param  [type]  $serverUrl  [description]
	 */
	public function __construct($serverUrl)
	{
		if (empty($serverUrl))
		{
			throw new InvalidArgumentException("Server URL cannot be empty");
		}

		$this->serverUrl = $serverUrl;

		$this->newClient();
		$this->connectWs();
	}

	/**
	 * Utility method to get the client id
	 *
	 * @return  string  the client id
	 */
	public function getId()
	{
		return $this->client->Id;
	}

	/**
	 * Register a broadcast handler
	 *
	 * @param   Closure  $handler  [description]
	 * @return  void
	 */
	public function registerBroadcastHandler(Closure $handler)
	{
		$this->handlers[static::MESSAGE_TYPE_BROADCAST][] = $handler;
	}

	/**
	 * Register a request handler
	 *
	 * @param   Closure  $handler  [description]
	 * @return  void
	 */
	public function registerRequestHandler(Closure $handler)
	{
		$this->handlers[static::MESSAGE_TYPE_REQUEST][] = $handler;
	}

	/**
	 * Receive a message from the socket
	 *
	 * @return void
	 */
	public function recv()
	{
		// receive a message from the socket
		$message = $this->ws->receive();

		// is the message is empty?
		if (empty($message))
		{
			return;
		}

		// decode the message (json_decode + error checking)
		$message = $this->decodeMessage($message);

		// injest and route the message
		$this->injest($message);
	}

	/**
	 * Suscribe to a given event
	 *
	 * @param   string|[]  $event    the event to suscribe to
	 * @param   Closure    $handler  the event handler to be called
	 *   upon receiving of the event
	 * @return  void
	 */
	public function suscribe($events, Closure $handler)
	{
		if (is_string($events)) $events = [$events];

		// store the handler for each of the events it represents
		foreach ($events as $event)
		{
			// check that we have a place to store the handler
			if (! array_key_exists($event, $this->handlers[static::MESSAGE_TYPE_STANDARD]))
			{
				$this->handlers[static::MESSAGE_TYPE_STANDARD][$event] = [];
			}

{'fraction_non_alphanumeric': 0.13535911602209943, 'fraction_numerical': 0.001841620626151013, 'mean_word_length': 3.1668797953964196, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4689037', 'n_tokens_mistral': 2320, 'n_tokens_neox': 2044, 'n_words': 551}