一文详解Laravel8/LaravelS实现弹幕功能

 3094

本篇文章给大家带来了关于Laravel8/LaravelS的相关知识,其中主要介绍了Laravel8基于LaravelS实现弹幕功能的方法步骤,感兴趣的朋友,下面一起来看一下,希望对大家有帮助。

Laravel8基于LaravelS实现弹幕弹幕功能

简介

Laravel8基于LaravelS实现弹幕弹幕功能。前面学了基于Swoole实现视频弹幕功能,这篇文章就来实现一个基于Laravel8的视频弹幕功能。如果对webpack不熟悉,那么在安装vue-baberrage组件时可能会报错却不知如何解决。下面开始一步一步实现。

前面学了基于Swoole实现视频弹幕功能,这篇文章就来实现一个基于Laravel8的视频弹幕功能。如果对webpack不熟悉,那么在安装vue-baberrage组件时可能会报错却不知如何解决。下面开始一步一步实现。

第一步:安装Laravel8

  1. composer create-project laravel/laravel labarrage

第二步:Laravel8中使用vue

Laravel8如何使用vue,请参考 Laravel8中使用vue

注意:安装vue时请使用 php artisan ui vue --auth

第三步:安装及安装vue-baberrage

安装vue及bootstrap

  1. npm install

安装弹幕组件

  1. npm install vue-baberrage --save

运行

  1. npm run dev

如果遇到BREAKING CHANGE: webpack < 5 used to include错误,请参考 Laravel8使用webpack报错的解决方法

后续只要文件改动就需要重新编译,后续将不再复述。

第四步:安装LaravelS实现Websocket服务器

请参考 Laravel8使用laravel-s实现WebSocket服务器

第五步:项目中引入vue-baberrage组件

文件:resources/js/app.js 新增如下内容

  1. import { vueBaberrage } from 'vue-baberrage'
  2. Vue.use(vueBaberrage)
  3.   
  4. Vue.component('danmu-component', require('./components/DanmuComponent.vue').default);

第五步:编写文弹幕组件

后续实现代码根据 学院君 文章改动

位置:resources/js/components/DanmuComponent.vue

  1. <template>
  2. <div id="danmu">
  3.   <div>
  4.     <vue-baberrage
  5.       :isShow = "barrageIsShow"
  6.       :barrageList = "barrageList"
  7.       :loop = "barrageLoop"
  8.       :maxWordCount = "60"
  9.       >
  10.     </vue-baberrage>
  11.   </div>
  12.   <div>
  13.     <div>
  14.       <select v-model="position">
  15.         <option value="top">从上</option>
  16.         <option value="abc">从右</option>
  17.       </select>
  18.       <input type="text" style="float:left" v-model="msg"/>
  19.       <button type="button" style="float:left" @click="addToList">发送</button>
  20.     </div>
  21.   </div>
  22. </div>
  23. </template>
  24. <script>
  25. import { MESSAGE_TYPE } from 'vue-baberrage'
  26. export default {
  27.   name: 'danmu',
  28.   data () {
  29.     return {
  30.       msg: 'hello 自如初!',
  31.       position: 'top',
  32.       barrageIsShow: true,
  33.       currentId: 0,
  34.       barrageLoop: false,
  35.       barrageList: []
  36.     }
  37.   },
  38.   methods: {
  39.     removeList () {
  40.       this.barrageList = []
  41.     },
  42.     addToList () {
  43.       if (this.position === 'top') {
  44.         this.barrageList.push({
  45.           id: ++this.currentId,
  46.           msg: this.msg + this.currentId,
  47.           barrageStyle: 'top',
  48.           time: 8,
  49.           type: MESSAGE_TYPE.FROM_TOP,
  50.           position: 'top'
  51.         })
  52.       } else {
  53.         this.barrageList.push({
  54.           id: ++this.currentId,
  55.           msg: this.msg,
  56.           time: 15,
  57.           type: MESSAGE_TYPE.NORMAL
  58.         })
  59.       }
  60.     }
  61.   }
  62. }
  63. </script>
  64. <style scoped>
  65. #danmu {
  66.   text-align: center;
  67.   color: #2c3e50;
  68. }
  69. .stage {
  70.   height: 300px;
  71.   width: 100%;
  72.   background: #025d63;
  73.   margin: 0;
  74.   position: relative;
  75.   overflow: hidden;
  76. }
  77. h1, h2 {
  78.   font-weight: normal;
  79. }
  80. ul {
  81.   list-style-type: none;
  82.   padding: 0;
  83. }
  84. li {
  85.   display: inline-block;
  86.   margin: 0 10px;
  87. }
  88. {
  89.   color: #42b983;
  90. }
  91. .baberrage-stage {
  92.   z-index: 5;
  93. }
  94. .baberrage-stage .baberrage-item.normal{
  95.   color:#FFF;
  96. }
  97. .top{
  98.   border:1px solid #66aabb;
  99. }
  100. .danmu-control{
  101.   position: absolute;
  102.   margin: 0 auto;
  103.   width: 100%;
  104.   bottom: 300px;
  105.   top: 70%;
  106.   height: 69px;
  107.   box-sizing: border-box;
  108.   text-align: center;
  109.   display: flex;
  110.   justify-content: center;
  111.   div {
  112.     width: 300px;
  113.     background: rgba(0, 0, 0, 0.6);
  114.     padding: 15px;
  115.     border-radius: 5px;
  116.     border: 2px solid #8ad9ff;
  117.   }
  118.   input,button,select{
  119.     height:35px;
  120.     padding:0;
  121.     float:left;
  122.     background:#027fbb;
  123.     border:1px solid #CCC;
  124.     color:#FFF;
  125.     border-radius:0;
  126.     width:18%;
  127.     box-sizing: border-box;
  128.   }
  129.   select{
  130.     height:33px;
  131.     margin-top:1px;
  132.     border: 0px;
  133.     outline: 1px solid rgb(204,204,204);
  134.   }
  135.   input{
  136.     width:64%;
  137.     height:35px;
  138.     background:rgba(0,0,0,.7);
  139.     border:1px solid #8ad9ff;
  140.     padding-left:5px;
  141.     color:#FFF;
  142.   }
  143. }
  144. </style>

第六步:视图中使用组件

位置:resources/views/danmu.blade.php

  1. @extends('layouts.app')
  2.   
  3. @section('content')
  4. <danmu-component></danmu-component>
  5. @endsection

第七步:注册路由

  1. Route::get('/danmu', function() {
  2.   return view('danmu');
  3. });

执行 npm run dev

第八步:编写websocket服务器

文件:App\Handlers\WebSocketHandler.php

  1. <?php
  2. namespace App\Handlers;
  3. use Hhxsv5\LaravelS\Swoole\WebSocketHandlerInterface;
  4. use Illuminate\Support\Facades\Log;
  5. use Swoole\Http\Request;
  6. use Swoole\WebSocket\Frame;
  7. use Swoole\WebSocket\Server;
  8. class WebSocketHandler implements WebSocketHandlerInterface
  9. {
  10.   public function __construct()
  11.   {
  12.   }
  13.   // 连接建立时触发
  14.   public function onOpen(Server $server, Request $request)
  15.   {
  16.     Log::info('WebSocket 连接建立:' . $request->fd);
  17.   }
  18.   // 收到消息时触发
  19.   public function onMessage(Server $server, Frame $frame)
  20.   {
  21.     // $frame->fd 是客户端 id,$frame->data 是客户端发送的数据
  22.     Log::info("从 {$frame->fd} 接收到的数据: {$frame->data}");
  23.     foreach($server->connections as $fd){
  24.       if (!$server->isEstablished($fd)) {
  25.         // 如果连接不可用则忽略
  26.         continue;
  27.       }
  28.       $server->push($fd , $frame->data); // 服务端通过 push 方法向所有连接的客户端发送数据
  29.     }
  30.   }
  31.   // 连接关闭时触发
  32.   public function onClose(Server $server, $fd, $reactorId)
  33.   {
  34.     Log::info('WebSocket 连接关闭:' . $fd);
  35.   }
  36. }

第九步:laravels.php注册

文件:config/laravels.php

  1. 'websocket' => [
  2.   'enable' => true,
  3.   'handler' => \App\Handlers\WebSocketHandler::class,
  4. ],

第十步:启动

  1. php bin/laravels start

这样就完成啦


本文网址:https://www.zztuku.com/index.php/detail-13794.html
站长图库 - 一文详解Laravel8/LaravelS实现弹幕功能
申明:本文转载于《自如初博客》,如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐