php socket laravel,laravel-websocket(一)

夹谷茂
2023-12-01

搭建简易websocket

------------------

# 1 . 安装swoole扩展

ubuntu查看安装php 的swoole插件

> php -m

如果有swoole就表示已经安装swoole扩展,直接看2

未安装则执行

* cd ~

* wget -c https://github.com/swoole/swoole-src/archive/v2.0.6.tar.gz

* cd swoole-src-2.0.6

* phpize

* ./configure

* make && make install

成功后显示

> Installing shared extensions: /usr/lib64/php/modules/

添加swoole扩展至php.ini

* 查看当前版本php.ini位置

> sudo find / -name php.ini

homestead会出现类似下面的情况,使用php -v查看当前版本号

```

/etc/php/5.6/fpm/php.ini

/etc/php/5.6/cli/php.ini

/etc/php/7.1/fpm/php.ini

/etc/php/7.1/cli/php.ini

/etc/php/7.2/fpm/php.ini

/etc/php/7.2/cli/php.ini

/etc/php/7.0/fpm/php.ini

/etc/php/7.0/cli/php.ini

```

如果是7.2版本

添加下面的拓展到对应fpm和cli目录php.ini下

> extension=swoole.so

重启php-fpm

再次查看是否安装成功

> php -m

# 2.安装redis包

> composer require predis/predis

> composer require \-\-dev "eaglewu/swoole-ide-helper:dev-master"

.env文件配置redis

# 3.env同级目录下创建SwooleStart.php文件

```php

server = new swoole_websocket_server("0.0.0.0", SWOOLE_PORT);

$log_file = SWOOLE_ROOT_PATH . 'storage/logs/swoole-' . date('Y-m-d') . '.log';

$this->server->set(array(

'worker_num' => 1, //工作进程数量

'daemonize' => true, //是否作为守护进程

'dispatch_mode' => 5, //

'log_file' => $log_file

));

// //监听WebSocket连接打开事件

$this->server->on('open', array($this, 'open'));

$this->server->on('workerStart', array($this, 'workerStart'));

$this->server->on('message', array($this, 'msg'));

$this->server->on('close', array($this, 'close'));

$this->server->start();

}

public function workerStart($server, $worker_id)

{

//定时器检测非法离线的用户让其下线

// swoole_timer_tick(10000, function ($worker_id) use ($server) {

echo "已同步最新的在线状态" . "\n";

// });

}

public function msg($server, $request)

{

echo $request->fd."消息".$request->data. "\n";

if ($request->fd == 1) {

$server->push(2, "由2发来的消息:".$request->data . "\n");

}else{

$server->push(1,"由1发来的消息:", $request->data . "\n");

}

// echo "消息";

$server->push($request->fd, $request->fd."你发到服务器的消息:" . ($request->data) . "\n");

}

public function open($server, $request)

{

echo "开启".$request->fd;

$server->push($request->fd, '开启' . "\n");

}

public function close($server, $fd)

{

echo "关闭".$fd;

$server->close($fd);

}

}

```

上面的文件表示websocket使用服务器9503端口

# 4. 创建swoole启动、关闭、重启命令

```php

argument('action');

switch ($action) {

case 'start':

$this->start();

break;

case 'stop':

$this->stop();

break;

case 'restart':

$this->restart();

break;

default:

break;

}

}

public function start()

{

shell_exec('php SwooleStart.php');

}

public function restart()

{

shell_exec('php artisan swoole stop');

shell_exec('php SwooleStart.php');

//shell_exec('php artisan swoole start');

}

public function stop()

{

shell_exec('pid=$(ps aux | grep SwooleStart | awk \'{print $2}\' | awk \'{print $1}\')

kill $pid');

}

}

```

开启

> php artisan swoole start

关闭

> php artisan swoole stop

重启

> php artisan swoole restart

# 5. 使用在线调试工具

http://easyswoole.com/wstool.html

用这个工具开启两个页面,开启swoole之后两个页面可以互相通信

ps:需要注意的是端口是否开启,可以使用http://tool.chinaz.com/port/ 这个站长工具端口扫描检查

文档:

https://wiki.swoole.com/wiki/page/p-server.html

框架:

https://www.easyswoole.com/

注释:执行php artisan swoole stop 后需要等待swoole服务完全关闭后在启动,可以启动的标志是执行`sudo netstat -anp|grep 9503(端口号)`后没有任何数据输出

 类似资料: