Skip to content

Instantly share code, notes, and snippets.

@javascripto
Last active September 1, 2019 18:42
Show Gist options
  • Select an option

  • Save javascripto/66247748fde4aa89d03ff262f2331071 to your computer and use it in GitHub Desktop.

Select an option

Save javascripto/66247748fde4aa89d03ff262f2331071 to your computer and use it in GitHub Desktop.
php swoole extension Instalation
# Instalation
sudo apt install -y php-pear php-dev
sudo pecl channel-update pecl.php.net
sudo pecl install swoole
# Enable swoole extension in php.ini
# php -i | grep php.ini$ # php.ini path
php_ini_path=$(ini=$(php -i | grep php.ini$) && echo ${ini:29})
echo "extension=swoole.so" >> $php_ini_path
const ws = new WebSocket('ws://localhost:9502');
ws.onmessage = r => console.log(r.data);
ws.send('Hello from Browser!');
<?php
$server = new swoole_websocket_server("127.0.0.1", 9502);
$server->on('open', function($server, $req) {
echo "connection open: {$req->fd}\n";
});
$server->on('message', function($server, $frame) {
echo "received message: {$frame->data}\n";
$server->push($frame->fd, json_encode(["hello", "world", "from", "server"]));
});
$server->on('close', function($server, $fd) {
echo "connection close: {$fd}\n";
});
$server->start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment