<commit_before><?php

//require_once('./daemonize.php');
require_once('./core/users.php');

abstract class core_websockets {

  // Configuration Start 
  protected $debug_mode				  = false; // debug tool I left in code. verbose mode
  protected $max_request_handshake_size 	  = 1024; // chrome : ~503B firefox : ~530B IE 11 : ~297B 
  // There is no way to use http status code to send some application error to client we MUST open the connection first
  protected $max_client                 	  = 100;  // 1024 is the max with select() keep space for rejecting socket I suggest keeping 24
  protected $error_maxclien	 		  = "WS SERVER reach it maximum limit. Please try again later"; // Set the error message sent to client. 
  protected $headerOriginRequired                 = false;
  protected $headerProtocolRequired               = false;
  protected $willSupportExtensions                = false;  // Turn it to true if you support any extensions

  // TODO : these 2 variables will be used to protect OOM and dynamically set max_client based on mem allowed per user
  protected $max_writeBuffer			  = 49152; //48K out 
  protected $max_readBuffer			  = 49152; //48K in 
  // Configuration End
  
  protected $userClass = 'WebSocketUser'; // redefine this if you want a custom user class.  The custom user class should inherit from WebSocketUser.
  protected $maxBufferSize;        
  protected $master;
  protected $readWatchers                         = array();
  protected $writeWatchers                        = null;
  protected $users                                = array();
  protected $interactive                          = true;
  protected $nbclient                             = 0;


  protected $mem;
   
  function __construct($addr, $port, $bufferLength = 16384 ) {
    $this->maxBufferSize = $bufferLength;
    $this->master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)  or die("Failed: socket_create()");
    socket_set_option($this->master, SOL_SOCKET, SO_REUSEADDR, 1) or die("Failed: socket_option()");
    socket_bind($this->master, $addr, $port)                      or die("Failed: socket_bind()");
    socket_listen($this->master,1024)                             or die("Failed: socket_listen()");
    socket_set_nonblock($this->master)							              or die("Failed: socket_set_nonblock()");
    $this->sockets['m'] = $this->master;
    $this->stdout("Server started\nListening on: $addr:$port\nMaster socket: ".$this->master);
  }

  abstract protected function onmessage(&$user,$message); // Called immediately when the data is recieved. 
  abstract protected function onopen(&$user);        // Called after the handshake response is sent to the client.
  abstract protected function onclose($user);           // Called after the connection is closed.
  abstract public    function run();                   // event loop within trait in eventloop.php support now (socket,libev)
  abstract protected function addWritewatchers(&$user,$open);

  protected function connecting($user) {
    // Override to handle a connecting user, after the instance of the User is created, but before
    // the handshake has completed.
  }

  //here an example how to use a single ws app to handle multiple virtualhost / 
  protected function broadcast($msg,&$sender) {
    $vhost=$sender->headers['host'].$sender->headers['get'];
    $message = $this->frame($msg);
    foreach ($this->users as $user) {
      if ($user->handshaked) { // prevent sending data to user who have not complete the handshake session.
        if ($vhost ==($user->headers['host'].$user->headers['get'])) {
          $this->stdout("same channel ".$channel,true);
          $this->ws_write($user, $message);
        }
      }
    }
  }<commit_msg>Update websockets.php<commit_after><?php

//require_once('./daemonize.php');
require_once('./core/users.php');

abstract class core_websockets {

  // Configuration Start 
  protected $debug_mode				  = false; // debug tool I left in code. verbose mode
  protected $max_request_handshake_size 	  = 1024; // chrome : ~503B firefox : ~530B IE 11 : ~297B 
  // There is no way to use http status code to send some application error to client we MUST open the connection first
  protected $max_client                 	  = 100;  // 1024 is the max with select() keep space for rejecting socket I suggest keeping 24
  protected $error_maxclien	 		  = "WS SERVER reach it maximum limit. Please try again later"; // Set the error message sent to client. 
  protected $headerOriginRequired                 = false;
  protected $headerProtocolRequired               = false;
  protected $willSupportExtensions                = false;  // Turn it to true if you support any extensions

  // TODO : these 2 variables will be used to protect OOM and dynamically set max_client based on mem allowed per user
  protected $max_writeBuffer			  = 49152; //48K out 
  protected $max_readBuffer			  = 49152; //48K in 
  // Configuration End
  
  protected $userClass = 'WebSocketUser'; // redefine this if you want a custom user class.  The custom user class should inherit from WebSocketUser.
  protected $maxBufferSize;        
  protected $master;
  protected $readWatchers                         = array();
  protected $writeWatchers                        = null;
  protected $users                                = array();
  protected $interactive                          = true;
  protected $nbclient                             = 0;


  protected $mem;
   
  function __construct($addr, $port, $bufferLength = 16384 ) {
    $this->maxBufferSize = $bufferLength;
    $this->master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)  or die("Failed: socket_create()");
    socket_set_option($this->master, SOL_SOCKET, SO_REUSEADDR, 1) or die("Failed: socket_option()");
    socket_bind($this->master, $addr, $port)                      or die("Failed: socket_bind()");
    socket_listen($this->master,1024)                             or die("Failed: socket_listen()");
    socket_set_nonblock($this->master)							              or die("Failed: socket_set_nonblock()");
    $this->stdout("Server started\nListening on: $addr:$port\nMaster socket: ".$this->master);
  }

  abstract protected function onmessage(&$user,$message); // Called immediately when the data is recieved. 
  abstract protected function onopen(&$user);        // Called after the handshake response is sent to the client.
  abstract protected function onclose($user);           // Called after the connection is closed.
  abstract public    function run();                   // event loop within trait in eventloop.php support now (socket,libev)
  abstract protected function addWritewatchers(&$user,$open);

  protected function connecting($user) {
    // Override to handle a connecting user, after the instance of the User is created, but before
    // the handshake has completed.
  }

  //here an example how to use a single ws app to handle multiple virtualhost / 
  protected function broadcast($msg,&$sender) {
    $vhost=$sender->headers['host'].$sender->headers['get'];
    $message = $this->frame($msg);
    foreach ($this->users as $user) {
      if ($user->handshaked) { // prevent sending data to user who have not complete the handshake session.
        if ($vhost ==($user->headers['host'].$user->headers['get'])) {
          $this->stdout("same channel ".$channel,true);
          $this->ws_write($user, $message);
        }
      }
    }
  }
{'fraction_non_alphanumeric': 0.10518558220554737, 'fraction_numerical': 0.013399437223636608, 'mean_word_length': 2.691394658753709, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1634927', 'n_tokens_mistral': 2111, 'n_tokens_neox': 1931, 'n_words': 685}