1. Go to this page and download the library: Download chipslays/porter library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
chipslays / porter example snippets
use Porter\Events\Event;
0:3737');
server()->on('ping', function (Event $event) {
$event->reply('pong');
});
server()->start();
use Workerman\Worker;
$worker = new Worker('websocket://0.0.0.0:3737');
use Workerman\Worker;
$context = [
// More see http://php.net/manual/en/context.ssl.php
'ssl' => [
'local_cert' => '/path/to/cert.pem',
'local_pk' => '/path/to/privkey.pem',
'verify_peer' => false,
// 'allow_self_signed' => true,
],
];
$worker = new Worker('websocket://0.0.0.0:3737', $context);
$worker->transport = 'ssl';
use Porter\Server;
$server = Server::getInstance();
$server->on(...);
server()->on(...);
$server = Server::getInstance();
$server->boot($worker);
// by helper function
server()->boot($worker);
use Workerman\Worker;
$worker = new Worker('websocket://0.0.0.0:3737');
server()->boot($worker); // booting server
$worker = new Worker('websocket://0.0.0.0:3737');
$worker->... // configure new worker
// change worker in already booted server
server()->setWorker($worker);
use Workerman\Worker;
$worker = new Worker('websocket://0.0.0.0:3737');
server()->setWorker($worker);
server()->getWorker();
use Porter\Server;
use Porter\Payload;
use Porter\Connection;
use Porter\Events\AbstractEvent;
class Ping extends AbstractEvent
{
public static string $id = 'ping';
public function handle(Connection $connection, Payload $payload, Server $server): void
{
$this->reply('pong');
}
}
server()->addEvent(Ping::class);
server()->autoloadEvents(__DIR__ . '/Events');
$server->on('ping', function (Event $event) {
$event->reply('pong');
});
server()->start();
use Porter\Terminal;
use Porter\Connection;
server()->onConnected(function (Connection $connection, string $header) {
Terminal::print('{text:darkGreen}Connected: ' . $connection->getRemoteAddress());
// Here also available vars: $_GET, $_COOKIE, $_SERVER.
Terminal::print("Query from client: {text:darkYellow}foo={$_GET['foo']}");
});
use Porter\Terminal;
use Porter\Connection;
server()->onDisconnected(function (Connection $connection) {
Terminal::print('{text:darkGreen}Connected: ' . $connection->getRemoteAddress());
});
use Porter\Terminal;
use Porter\Connection;
server()->onError(function (Connection $connection, $code, $message) {
Terminal::print("{bg:red}{text:white}Error {$code} {$message}");
});
use Porter\Terminal;
use Workerman\Worker;
server()->onStart(function (Worker $worker) {
//
});
use Porter\Terminal;
use Workerman\Worker;
server()->onStop(function (Worker $worker) {
//
});
use Porter\Terminal;
use Workerman\Worker;
server()->onReload(function (Worker $worker) {
//
});
server()->storage();
server()->storage()->put('foo', 'bar');
$storage = server()->storage();
$storage->get('foo');
// can also be a get as propperty
server()->storage()->put('foo', 'bar');
$storage = server()->storage;
$connection = server()->connection(1);
server()->to($connection, 'welcome message', [
'text' => 'Hello world!'
]);
// also can get like
$connection = server()->getWorker()->connections[1337] ?? null;
$connections = server()->connections();
server()->broadcast('update users count', ['count' => $connections->count()]);
// also can get like
$connections = server()->getWorker()->connections;
$v = server()->validator();
if ($v->email()->validate('[email protected]')) {
//
}
// available as helper
if (validator()->contains('example.com')->validate('[email protected]')) {
//
}
// by method
server()->channels();
// by property
server()->channels;
use Porter\Server;
use Porter\Payload;
use Porter\Connection;
use Porter\Events\AbstractEvent;
return new class extends AbstractEvent
{
public static string $type = 'hello to';
protected array $rules = [
'username' => ['stringType', ['length', [3, 18]]],
];
public function handle(Connection $connection, Payload $payload, Server $server): void
{
if (!$this->validate()) {
$this->reply('bad request', ['errors' => $this->errors]);
return;
}
$username = $this->payload->data['username'];
$this->reply(data: ['message' => "Hello, {$username}!"]);
}
};
use Porter\Server;
use Porter\Payload;
use Porter\Events\AbstractEvent;
use Porter\Connection;
class Ping extends AbstractEvent
{
public static string $type = 'ping';
public function handle(Connection $connection, Payload $payload, Server $server): void
{
$this->reply('pong');
}
}
// and next you need add (register) this class to events:
server()->addEvent(Ping::class);
// this is a object of Channel, getted by `channel_id` from client.
$this->channel;
$this->channel();
$this->channel()->broadcast('new message', [
'text' => $this->payload->get('text'),
'from' => $this->connection->nickname,
]);
// this is a object of Channel, getted by `target_id` from client.
$this->target;
$this->target();
$this->to($this->target, 'new message', [
'text' => $this->payload->get('text'),
'from' => $this->connection->nickname,
]);
$this->to($connection, 'ping');
$this->reply('ping');
// analog for:
$this->to($this->connection, 'ping');
$this->broadcast('user join', [
'text' => 'New user joined to chat.',
], [$this->connection]);
if (!$this->validate()) {
return $this->reply(/** ... */);
}
if ($this->hasErrors()) {
return $this->reply('bad request', ['errors' => $this->errors]);
}
public function handle(Connection $connection, Payload $payload, Server $server)
{
$this->get('nickname');
// as property
$this->data['nickname'];
$this->data->get('nickname');
// form payload instance
$payload->data['nickname'];
$payload->data->get('nickname');
$this->payload->data['nickname'];
$this->payload->data->get('nickname');
}
use Porter\Events\Event;
server()->on('new message', function (Event $event) {
// $event has all the same property && methods as in the example above
$event->to($event->target, 'new message', [
'text' => $this->payload->get('text'),
'from' => $this->connection->nickname,
]);
$event->channel()->broadcast('new message', [
'text' => $this->payload->get('text'),
'from' => $this->connection->nickname,
]);
});
$connection->channels; // object of Porter\Connection\Channels
/**
* Get connection channels.
*
* @return Channel[]
*/
public function all(): array
/**
* Get channels count.
*
* @return int
*/
public function count(): int
/**
* Method for when connection join to channel should detach channel id from connection.
*
* @param string $channelId
* @return void
*/
public function delete(string $channelId): void
/**
* Leave all channels for this connection.
*
* @return void
*/
public function leaveAll(): void
/**
* When connection join to channel should attach channel id to connection.
*
* You don't need to use this method, it will automatically fire inside the class.
*
* @param string $channelId
* @return void
*/
public function add(string $channelId): void
$client = new Client('ws://localhost:3737');
$client = new Client('wss://example.com:3737');
server()->storage()->load(__DIR__ . '/server-storage.data'); // you can use any filename
server()->storage();
// as standalone use without server
$store1 = new Porter\Storage(__DIR__ . '/path/to/file1');
$store2 = new Porter\Storage(__DIR__ . '/path/to/file2');
$store3 = new Porter\Storage(__DIR__ . '/path/to/file3');
server()->storage()->load(__DIR__ . '/path/to/file'); // you can use any filename