Skip to content

Instantly share code, notes, and snippets.

@leocavalcante
Created November 19, 2019 22:31
Show Gist options
  • Save leocavalcante/70e48f6456f418268016d4e51819e2e1 to your computer and use it in GitHub Desktop.
Save leocavalcante/70e48f6456f418268016d4e51819e2e1 to your computer and use it in GitHub Desktop.
<?php declare(strict_types=1);
namespace ReactOnSwoole;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\Runtime;
use Swoole\Server\Port;
use Swoole\WebSocket\Frame;
use Swoole\WebSocket\Server;
use function Ratchet\Client\connect;
$dir = __DIR__;
require_once "$dir/vendor/autoload.php";
Runtime::enableCoroutine();
$server = new Server('0.0.0.0', 8282);
$server->on('message', function (Server $server, Frame $frame) {
var_dump($frame->data);
});
/** @var Port $http */
$http = $server->addlistener('0.0.0.0', 8082, SWOOLE_BASE);
$http->set(['open_http_protocol' => true]);
$http->on('request', function (Request $request, Response $response) {
$response->end('OK');
connect('ws://localhost:8282')->then(function ($conn) {
var_dump('Connected');
$conn->send('Hello World!');
$conn->close();
}, function ($e) {
echo "Could not connect: {$e->getMessage()}\n";
});
});
$server->start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment