欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

javascript - 怎么配置WebSocket服务器,用的SegmentFault

程序员文章站 2023-12-30 08:45:28
...
rt
想用websocket代替轮询
请问怎么配置websocket服务器

回复内容:

rt
想用websocket代替轮询
请问怎么配置websocket服务器

PHP自身并没有提供WebSocket相关函数,这个你得用第三方扩展,比如峰哥的用C开发的PECL扩展Swoole,里面直接就封装有WebSocket Server,比如官网的示例:
http://www.swoole.com/

on('Open', function($server, $req) {
    echo "connection open: ".$req->fd;
});
$serv->on('Message', function($server, $frame) {
    echo "message: ".$frame->data;
    $server->push($frame->fd, json_encode(["hello", "world"]));
});
$serv->on('Close', function($server, $fd) {
    echo "connection close: ".$fd;
});
$serv->start();

PHPWebIM是Swoole官方基于PHP Swoole扩展和Swoole Framework开发的WebSocket网页即时聊天工具.PHPWebIM支持WebSocket+Comet两种协议,可用于所有种类的浏览器包括IE.
https://github.com/matyhtf/PHPWebIM
Demo: http://webim.swoole.com/

编译安装好PHP后,执行 PHP_PREFIX/bin/pecl install swoole 即可安装 Swoole 扩展,因为 Swoole 扩展已经收录到 PHP 官方 PECL 仓库,而且已经支持PHP7:
https://pecl.php.net/package/swoole

上一篇:

下一篇: