Best Phoronix-test-suite code snippet using pts_web_socket_client.connect
pts_web_socket_client.php
Source:pts_web_socket_client.php
...29 //socket_set_option($this->socket_master, SOL_SOCKET, SO_REUSEADDR, 1);30 //socket_bind($this->socket_master, $address, $port);31 //echo socket_strerror(socket_last_error());32 //socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 2, 'usec' => 0));33 $sc = socket_connect($this->socket_master, $address, $port);34 // socket_listen($this->socket_master);35 //echo socket_strerror(socket_last_error());36 $this->make_hand_shake($this->socket_master);37 $this->connect($this->socket_master);38 //echo 'WebSocket Client Connected: ' . $address . ':' . $port . PHP_EOL;39 $p = $this->send('ping');40 return $sc && $p != null;41 }42 public function send($msg)43 {44 return $this->send_data($this->socket_master, $msg);45 }46 public function receive()47 {48 $buffer = null;49 $bytes = socket_recv($this->socket_master, $buffer, 8192, 0);50 if($bytes === false)51 return; // error52 if($bytes > 0)53 {54 return $this->decode_data($this->user_master, $buffer);55 }56 else if($bytes === false)57 {58 echo PHP_EOL . socket_strerror(socket_last_error()) . PHP_EOL;59 }60 else61 {62 // NO DATA RECEIVED63 // $this->disconnect($this->socket_master);64 }65 }66 protected function debug_msg(&$socket, $msg)67 {68 echo PHP_EOL;69 if($socket && is_resource($socket))70 {71 $address = null;72 socket_getpeername($socket, $address);73 echo $address . ': ';74 }75 echo $msg . PHP_EOL;76 }77 protected function decode_data(&$user, &$data)78 {79 $msg_opcode = bindec(substr(sprintf('%08b', ord($data[0])), 4, 4));80 $data_length = ord($data[1]) & 127;81 // TODO XXX: sometimes the opcode is 8 (close)... figure out why....82 if($data_length === 126)83 {84 $mask = substr($data, 4, 4);85 $encoded_data = substr($data, 4);86 }87 else if($data_length === 127)88 {89 $mask = substr($data, 10, 4);90 $encoded_data = substr($data, 4);91 }92 else93 {94 $mask = substr($data, 2, 4);95 $encoded_data = substr($data, 6, $data_length);96 }97 $decoded_data = null;98 if(false && $user->user_agent == 'phoronix-test-suite')99 {100 // The PTS WebSocket client isn't currently masking data due to bug it seems101 $decoded_data .= $encoded_data;102 }103 else104 {105 for($i = 0; $i < strlen($encoded_data); $i++)106 {107 $decoded_data .= $encoded_data[$i] ^ $mask[($i % 4)];108 }109 }110 return $decoded_data;111 }112 protected function send_json_data($socket, $json)113 {114 $data = json_encode($json, JSON_UNESCAPED_SLASHES);115 $this->send_data($socket, $data);116 }117 protected function send_data($socket, $data, $masked = true)118 {119 if(self::$debug_mode)120 {121 $this->debug_msg($socket, 'Sending: ' . $data);122 }123 $data_length = strlen($data);124 $encoded = null;125 // FRAME THE MESSAGE126 $encoded .= chr(0x81);127 if($data_length <= 125)128 {129 $encoded .= chr($data_length);130 }131 else if($data_length <= 65535)132 {133 $encoded .= chr(126) . chr($data_length >> 8) . chr($data_length & 0xFF);134 }135 else136 {137 $encoded .= chr(127) . pack('N', 0) . pack('N', $data_length);138 }139 // XXX:140 if($masked)141 {142 $mask = null;143 for($i = 0; $i < 4; $i++)144 {145 $mask .= chr(rand(0, 255));146 }147 $encoded .= $mask;148 // MESSAGE DATA149 for($i = 0; $i < strlen($data); $i++)150 {151 $encoded .= $data[$i] ^ $mask[$i % 4];152 }153 }154 else155 {156 $encoded .= $data;157 }158 // SEND159 $t = socket_write($socket, $encoded, strlen($encoded));160 usleep(100000); // XXX without this, doing lots of send() at once tends to result in only the first one getting through161 return $t;162 }163 public function send_json_data_by_user_id($user_id, $msg)164 {165 foreach($this->users as &$u)166 {167 if($u->id == $user_id)168 {169 $this->send_json_data($u->socket, $msg);170 break;171 }172 }173 }174 private function connect($socket)175 {176 $user = new pts_web_socket_user();177 $user->id = uniqid();178 $user->socket = $socket;179 return $user;180 }181 public function disconnect()182 {183 socket_close($this->socket_master);184 }185 protected function make_hand_shake(&$socket, $get = 'phoronix-test-suite')186 {187 $this->send_data($socket, 'GET /' . $get . ' HTTP/1.1188 Host: localhost189 Upgrade: websocket190 Connection: Upgrade191 Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==192 Sec-WebSocket-Protocol: phoronixtestsuite193 Sec-WebSocket-Version: 13194 User-Agent: phoronix-test-suite195 Date: ' . date('D, d M Y H:i:s e'));196 $bytes = socket_recv($socket, $buffer, 2048, 0);197 $user = $this->connect($socket);198 return $this->process_hand_shake($user, $buffer);199 }200 protected function process_hand_shake($user, $buffer)201 {202 //echo 'HANDSHAKE = ' . PHP_EOL; var_dump($buffer);203 list($resource, $host, $origin, $key, $version, $user_agent) = $this->extract_headers($buffer);204 $protocol_handshake = array(205 'HTTP/1.1 101 WebSocket Protocol Handshake',206 'Date: ' . date('D, d M Y H:i:s e'),207 'Connection: Upgrade',208 'Upgrade: WebSocket',209 'Sec-WebSocket-Origin: ' . $origin,210 'Access-Control-Allow-Origin: ' . $origin,211 'Access-Control-Allow-Credentials: true',...
connect
Using AI Code Generation
1require_once 'pts_web_socket_client.php';2$pts_web_socket_client = new pts_web_socket_client();3$pts_web_socket_client->connect('localhost', 9000);4$pts_web_socket_client->send('Hello World!');5$pts_web_socket_client->close();6require_once 'pts_web_socket_client.php';7$pts_web_socket_client = new pts_web_socket_client();8$pts_web_socket_client->connect('localhost', 9000);9$pts_web_socket_client->send('Hello World!');10$pts_web_socket_client->close();11require_once 'pts_web_socket_client.php';12$pts_web_socket_client = new pts_web_socket_client();13$pts_web_socket_client->connect('localhost', 9000);14$pts_web_socket_client->send('Hello World!');15$pts_web_socket_client->close();16require_once 'pts_web_socket_client.php';17$pts_web_socket_client = new pts_web_socket_client();18$pts_web_socket_client->connect('localhost', 9000);19$pts_web_socket_client->send('Hello World!');20$pts_web_socket_client->close();21require_once 'pts_web_socket_client.php';22$pts_web_socket_client = new pts_web_socket_client();23$pts_web_socket_client->connect('localhost', 9000);24$pts_web_socket_client->send('Hello World!');25$pts_web_socket_client->close();
connect
Using AI Code Generation
1$socket = new pts_web_socket_client();2$socket->connect('localhost', 8080, '/websocket');3$socket->send('Hello World');4$socket->disconnect();5echo $socket->getResponse();6echo $socket->getStatusCode();7print_r($socket->getResponseHeaders());8echo $socket->getResponseHeader('Content-Type');9echo $socket->getResponseHeader('Content-Length');10echo $socket->getResponseHeader('Server');11echo $socket->getResponseHeader('Connection');12echo $socket->getResponseHeader('Date');13echo $socket->getResponseHeader('X-Powered-By');14echo $socket->getResponseHeader('Cache-Control');15echo $socket->getResponseHeader('Expires');16echo $socket->getResponseHeader('Last-Modified');17echo $socket->getResponseHeader('Pragma');18echo $socket->getResponseHeader('X-Content-Type-Options');19echo $socket->getResponseHeader('X-Frame-Options');20echo $socket->getResponseHeader('X-XSS-Protection');21echo $socket->getResponseHeader('Content-Encoding');
connect
Using AI Code Generation
1require_once 'pts_web_socket_client.php';2$pts_web_socket_client = new pts_web_socket_client();3$pts_web_socket_client->send('Hello World');4$pts_web_socket_client->disconnect();5require_once 'pts_web_socket_server.php';6$pts_web_socket_server = new pts_web_socket_server();7$pts_web_socket_server->send('Hello World');8$pts_web_socket_server->disconnect();9require_once 'pts_web_socket.php';10$pts_web_socket = new pts_web_socket();11$pts_web_socket->send('Hello World');12$pts_web_socket->disconnect();13require_once 'pts_web_socket.php';14$pts_web_socket = new pts_web_socket();15$pts_web_socket->send('Hello World');16$pts_web_socket->disconnect();17require_once 'pts_web_socket.php';18$pts_web_socket = new pts_web_socket();19$pts_web_socket->send('Hello World');20$pts_web_socket->disconnect();21require_once 'pts_web_socket.php';22$pts_web_socket = new pts_web_socket();
connect
Using AI Code Generation
1$ws = new pts_web_socket_client('localhost', 9000, '/echo');2$ws->connect();3$ws->send('Hello World!');4$ws->close();5$ws = new pts_web_socket_client('localhost', 9000, '/echo');6$ws->connect();7$ws->send('Hello World!');8$ws->close();9$ws = new pts_web_socket_client('localhost', 9000, '/echo');10$ws->connect();11$ws->send('Hello World!');12$ws->close();13$ws = new pts_web_socket_client('localhost', 9000, '/echo');14$ws->connect();15$ws->send('Hello World!');16$ws->close();17$ws = new pts_web_socket_client('localhost', 9000, '/echo');18$ws->connect();19$ws->send('Hello World!');20$ws->close();21$ws = new pts_web_socket_client('localhost', 9000, '/echo');22$ws->connect();23$ws->send('Hello World!');24$ws->close();25$ws = new pts_web_socket_client('localhost', 9000, '/echo');26$ws->connect();27$ws->send('Hello World!');28$ws->close();29$ws = new pts_web_socket_client('localhost', 9000, '/echo');30$ws->connect();31$ws->send('Hello World!');32$ws->close();33$ws = new pts_web_socket_client('localhost', 9000, '/echo');34$ws->connect();35$ws->send('Hello World!');36$ws->close();
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Execute automation tests with connect on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.
Test now for FreeGet 100 minutes of automation test minutes FREE!!