<?php require_once 'lib/Utils.php'; require_once 'lib/ServerConnection.php'; $connection = new ServerConnection('127.0.0.1', 15700); check($connection->isConnected()); $data = json_encode(array('action' => 'help')); print_r(unpack("c*", $data)); $connection->send(pack("i", strlen($data)) . $data . pack("i", strlen($data)) . $data); $resp = $connection->recvPacket(); print_r($resp); die; $connection->sendPacket(array('action' => 'execute', 'data' => array('environmentId' => 0, 'code' => 'print("lalala"); return { 1, 3 };'))); $resp = $connection->recvPacket(); check($resp['rs'] == 0); check($resp['error'] == 'ERROR_BAD_SEQUENCE');
<?php require_once 'lib/Utils.php'; require_once 'lib/ServerConnection.php'; define('CONNECTIONS_CREATE', 100); timerStart(); $connections = array(); for ($i = 0; $i < CONNECTIONS_CREATE; $i++) { $connection = new ServerConnection('127.0.0.1', 15600); check($connection->isConnected()); $connections[] = $connection; } timerTick(sprintf('%d connections opened', sizeof($connections))); $requests = 0; $stime = microtime(true); while (microtime(true) - $stime < 10) { $random = rand(0, sizeof($connections) - 1); $connection = $connections[$random]; $connection->sendPacket(array('action' => 'auth', 'data' => array('uid' => 1, 'login' => 'tester', 'password' => ''))); $resp = $connection->recvPacketText(); if ($resp === false) { printf("FAILED\n"); break; } //check($resp['rs'] == 0); //check($resp['error'] == 'ERROR_BAD_LOGIN_PASSWORD'); $requests++; } printf("Duration: %f, requests: %f\n", 10, $requests); /* $connection->sendPacket(array(
/** * @param $nodeAddress * @param $ctx */ public function makeFriends($nodeAddress, $ctx) { $node = stream_socket_client('tls://' . $nodeAddress, $errno, $errstr, 5, STREAM_CLIENT_CONNECT, $ctx); if (!$node || !$ctx || $errno || $errstr) { exit('Are you sure this address is someone who wants to make friends? ' . $nodeAddress); } $node = new ServerConnection($node); if (!$node->writeRaw($this->key)) { exit('I couldn\'t get a hold of the person you wanted to introduce me to, something about fwrite failing...?'); } if (!($response = $node->readOnce())) { exit('I couldn\'t get a hold of the person you wanted to introduce me to, something about fread failing...?'); } //Process results (should be list of servers to connect to) if (!($servers = json_decode($response, true))) { exit('I couldn\'t make friends today, are you sure you introduced me correctly?'); } $this->connectionHandlers[0]->connections[] = $node; foreach ($servers as $server) { $node = stream_socket_client('tls://' . $server, $errno, $errstr, 5, STREAM_CLIENT_CONNECT, $ctx); if (!$node || !$ctx || $errno || $errstr) { exit('I met the friend you introduced me to, but their other friend wasn\'t interested in me. :( ' . $nodeAddress); } $node = new ServerConnection($node); if (!$node->writeRaw($this->key)) { exit('I couldn\'t get a hold of the person you wanted to introduce me to, something about fwrite failing...?'); } if (!($response = $node->readOnce())) { exit('I couldn\'t get a hold of the person you wanted to introduce me to, something about fread failing...?'); } $this->connectionHandlers[0]->connections[] = $node; } echo "Successfully made " . count($this->connectionHandlers[0]->connections) . " friends\n"; }
check($resp['rs'] == 1, $resp); $connection->sendPacket(array('action' => 'ExecuteCode', 'data' => array('environmentId' => 0, 'code' => sprintf('ChatManager.addUserToChannel(%d, "%s");', 2, USER_1_2_CHANNEL)))); $resp = $connection->recvPacket(); check($resp['rs'] == 1, $resp); // ---------------------------------------------------------// // ADD USER // ---------------------------------------------------------// $user_1 = new ServerConnection('127.0.0.1', 15600); check($user_1->isConnected()); $user_1->sendPacket(array('action' => 'auth', 'data' => array('uid' => 1, 'key' => USER_1_KEY))); $resp = $user_1->recvPacket(); check($resp['rs'] == 1, $resp); // ---------------------------------------------------------// // ADD ANOTHER USER // ---------------------------------------------------------// $user_2 = new ServerConnection('127.0.0.1', 15600); check($user_2->isConnected()); // BAD KEY $user_2->sendPacket(array('action' => 'auth', 'data' => array('uid' => 2, 'key' => USER_1_KEY))); $resp = $user_2->recvPacket(); check($resp['rs'] == 0, $resp); // OK $user_2->sendPacket(array('action' => 'auth', 'data' => array('uid' => 2, 'key' => USER_2_KEY))); $resp = $user_2->recvPacket(); check($resp['rs'] == 1, $resp); // ---------------------------------------------------------// // SEND MESSAGE // ---------------------------------------------------------// $user_1->sendPacket(array('action' => 'sendMessage', 'data' => array('channel' => USER_1_2_CHANNEL, 'message' => 'Hello world!'))); $resp = $user_1->recvPacket(); check($resp['event'] == 'newMessage', $resp);
<?php require_once 'lib/Utils.php'; require_once 'lib/ServerConnection.php'; $connection = new ServerConnection('127.0.0.1', 15600); check($connection->isConnected()); $connection->sendPacket(array('action' => 'auth', 'data' => array('uid' => 1, 'login' => 'tester', 'password' => ''))); $resp = $connection->recvPacket(); print_r($resp); /* $connection->sendPacket(array( 'action' => 'auth', 'data' => array( 'uid' => 1, 'login' => 'tester', 'password' => 'tester', ), )); $resp = $connection->recvPacket(); check($resp['rs'] == 1); $connection->sendPacket(array( 'action' => 'time', 'sq' => 1, )); $resp = $connection->recvPacket(); check($resp['data']['time']); check($resp['sq'] == 1); */