PHP

利用Swoole实现PHP+websocket直播,即时通讯代码,及linux下swoole安装基本配置

字号+ 编辑: 国内TP粉 修订: 种花家 来源: cnblogs 2023-10-02 我要说两句(0)

一篇具有参考价值的文章, 飨给各位开发大神。

php安装swoole

1. 下载swoole安装

wget http://pecl.php.net/get/swoole-1.9.1.tgz
tar -zxvf swoole-1.9.1.tgz
cd swoole-1.9.1
phpize
./configure
make
make install

2. 在php.ini添加swoole.so

extension="swoole.so"

命令行下输入php -m或者php --ri swoole查看插件列表里是否有swoole, 有的话代表安装成功

环境依赖

仅支持Linux,FreeBSD,MacOS,3类操作系统

Linux内核版本2.3.32以上

PHP5.3.10以上版本

gcc4.4以上版本或者clang

cmake2.4+,编译为libswoole.so作为C/C++库时需要使用cmake

PHP版本依赖

swoole仅支持PHP5.3.10或更高版本,建议使用PHP5.4+

swoole不依赖php的stream、sockets、pcntl、posix、sysvmsg等扩展。PHP只需安装最基本的扩展即可

PHP直播代码

1.start.php 使用时需要开启,服务器输入(php start.php)


<?php

// 使用PHPCLI模式运行

// 设置路径
define('_ROOT_', dirname(__FILE__));
require_once _ROOT_.'/function.php';

$server = new swoole_websocket_server("0.0.0.0(这里就是四个0,不要改)", 8888);

$server->on('open', function (swoole_websocket_server $server, $request) {
    if(!file_exists(_ROOT_.'/client/'.$request->fd.'.client')){
        @file_put_contents(_ROOT_.'/client/'.$request->fd.'.client',$request->fd);
    }
});


$server->on('message', function (swoole_websocket_server $server, $frame) {
    foreach(notice(_ROOT_.'/client/') as $v){
            $server->push($v,$frame->data);
    }
});


$server->on('close', function ($ser, $fd) {
    @unlink(_ROOT_.'/client/'.$fd.'.client');
});


$server->start();

2.index.html 直播页面,访问该页面观看直播

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>在线直播界面</title>
</head>
<body>
<img id="receiver" style='width:640px;height:480px'/>
<script type="text/javascript" charset="utf-8">
    var ws = new WebSocket("ws://改成自己服务器ip:8888");
    var image = document.getElementById('receiver');
    ws.onopen = function(){
    }
    ws.onmessage = function(data)
    {
        image.src=data.data;
    }
</script>
</body>
</html>

3.rec.html主播录制页面,访问该页面进行直播录制

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>主播录制界面</title>
</head>
<body>
<video id="video" autoplay="" style='width:640px;height:480px'></video>
<canvas id="output" style="display:none"></canvas>
<script type="text/javascript" charset="utf-8">
    var ws = new WebSocket("ws://自己服务器ip:8888");
    var back = document.getElementById('output');
    var backcontext = back.getContext('2d');
    var video = document.getElementById("video");
    var success = function(stream){
        video.src = window.URL.createObjectURL(stream);
    }
    ws.onopen = function(){
        draw();
    }
    var draw = function(){
        try{
            backcontext.drawImage(video,0,0, back.width, back.height);
        }catch(e){
            if (e.name == "NS_ERROR_NOT_AVAILABLE") {
                return setTimeout(draw, 100);
            } else {
                throw e;
            }
        }
        if(video.src){
            ws.send(back.toDataURL("image/jpeg", 0.5));
        }
        setTimeout(draw, 100);
    }
    navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
    navigator.mozGetUserMedia || navigator.msGetUserMedia;
    navigator.getUserMedia({video:true, audio:false}, success, console.log);
</script>
</body>
</html>

4. function.php 统计数据页面

<?php

function clearDir($dir)
{
    $n = 0;
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if ($file == '.' or $file == '..') {
                continue;
            }
            if (is_file($dir . $file)) {
                $n++;
            }
        }
    }

    closedir($dh);
    return $n;
}

function notice($dir){
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if ($file == '.' or $file == '..') {
                continue;
            }
            if (is_file($dir . $file)) {
                $array[]=file_get_contents($dir.$file);
            }
        }
    }
    closedir($dh);
    return $array;
}

5. 在同级目录下建立client文件,存放信息

PHP 即时通讯

1.socket.php 一样,使用时需要开启

<?php
    // 创建websocket服务器对象,监听0.0.0.0:9502端口
    $ws = new swoole_websocket_server("0.0.0.0", 9502);
    // 监听WebSocket连接打开事件
    $ws->on('open', function ($ws, $request) {
        $fd[] = $request->fd;
        $GLOBALS['fd'][] = $fd;
        //$ws->push($request->fd, "hello, welcome\n");
    });
    // 监听WebSocket消息事件
    $ws->on('message', function ($ws, $frame) {
        $msg = 'from'.$frame->fd.":{$frame->data}\n";
    // var_dump($GLOBALS['fd']);
    // exit;
        foreach($GLOBALS['fd'] as $aa){
            foreach($aa as $i){
                $ws->push($i,$msg);
            }
        }
       // $ws->push($frame->fd, "server: {$frame->data}");
       // $ws->push($frame->fd, "server: {$frame->data}");
    });
    // 监听WebSocket连接关闭事件
    $ws->on('close', function ($ws, $fd) {
        echo "client-{$fd} is closed\n";
    });
    $ws->start();

2. socket.html聊天页面

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="msg"></div>
<input type="text" id="text">
<input type="submit" value="发送数据" ="song()">
</body>
<script>
    var msg = document.getElementById("msg");
    var wsServer = 'ws://60.205.208.176:9502';
    //调用websocket对象建立连接:
    //参数:ws/wss(加密)://ip:port (字符串)
    var websocket = new WebSocket(wsServer);
    //onopen监听连接打开
    websocket.onopen = function (evt) {
        //websocket.readyState 属性:
        /*
        CONNECTING    0    The connection is not yet open.
        OPEN    1    The connection is open and ready to communicate.
        CLOSING    2    The connection is in the process of closing.
        CLOSED    3    The connection is closed or couldn't be opened.
        */
        msg.innerHTML = websocket.readyState;
    };
    function song(){
        var text = document.getElementById('text').value;
        document.getElementById('text').value = '';
        //向服务器发送数据
        websocket.send(text);
    }
      // 监听连接关闭

//        console.log("Disconnected");

    // onmessage 监听服务器数据推送
    websocket.onmessage = function (evt) {
        msg.innerHTML += evt.data +'<br>';

    };

//    websocket.onerror = function (evt, e) {

//    };
</script>
</html>


阅完此文,您的感想如何?
  • 有用

    0

  • 没用

    1

  • 开心

    0

  • 愤怒

    0

  • 可怜

    0

1.如文章侵犯了您的版权,请发邮件通知本站,该文章将在24小时内删除;
2.本站标注原创的文章,转发时烦请注明来源;
3.交流群: PHP+JS聊天群

相关课文
  • mac开发接入微信公众号接口返回报错 cURL error 56: SSLRead() return error -9806

  • pecl安装程序时报错Array and string offset access syntax with curly braces is no longer supported

  • PHP的换行符是什么

  • 由于商家传入的H5交易参数有误,该笔交易暂时无法完成,请联系商家解决

我要说说
网上宾友点评