laravel8基于laravels实现弹幕弹幕功能
简介
laravel8基于laravels实现弹幕弹幕功能。前面学了基于swoole实现视频弹幕功能,这篇文章就来实现一个基于laravel8的视频弹幕功能。如果对webpack不熟悉,那么在安装vue-baberrage组件时可能会报错却不知如何解决。下面开始一步一步实现。
前面学了基于swoole实现视频弹幕功能,这篇文章就来实现一个基于laravel8的视频弹幕功能。如果对webpack不熟悉,那么在安装vue-baberrage组件时可能会报错却不知如何解决。下面开始一步一步实现。
第一步:安装laravel8
composer create-project laravel/laravel labarrage
第二步:laravel8中使用vue
laravel8如何使用vue,请参考 laravel8中使用vue。
注意:安装vue时请使用 php artisan ui vue --auth
第三步:安装及安装vue-baberrage
安装vue及bootstrap
npm install
安装弹幕组件
npm install vue-baberrage --save
运行
npm run dev
如果遇到breaking change: webpack < 5 used to include错误,请参考 laravel8使用webpack报错的解决方法。
后续只要文件改动就需要重新编译,后续将不再复述。
第四步:安装laravels实现websocket服务器
请参考 laravel8使用laravel-s实现websocket服务器
第五步:项目中引入vue-baberrage组件
文件:resources/js/app.js 新增如下内容
import { vuebaberrage } from 'vue-baberrage'
vue.use(vuebaberrage)
vue.component('danmu-component', require('./components/danmucomponent.vue').default);
第五步:编写文弹幕组件
后续实现代码根据 学院君 文章改动
位置:resources/js/components/danmucomponent.vue
<template>
<div id="danmu">
<div class="stage">
<vue-baberrage
:isshow = "barrageisshow"
:barragelist = "barragelist"
:loop = "barrageloop"
:maxwordcount = "60"
>
</vue-baberrage>
</div>
<div class="danmu-control">
<div>
<select v-model="position">
<option value="top">从上</option>
<option value="abc">从右</option>
</select>
<input type="text" style="float:left" v-model="msg"/>
<button type="button" style="float:left" @click="addtolist">发送</button>
</div>
</div>
</div>
</template>
<script>
import { message_type } from 'vue-baberrage'
export default {
name: 'danmu',
data () {
return {
msg: 'hello 自如初!',
position: 'top',
barrageisshow: true,
currentid: 0,
barrageloop: false,
barragelist: []
}
},
methods: {
removelist () {
this.barragelist = []
},
addtolist () {
if (this.position === 'top') {
this.barragelist.push({
id: ++this.currentid,
msg: this.msg + this.currentid,
barragestyle: 'top',
time: 8,
type: message_type.from_top,
position: 'top'
})
} else {
this.barragelist.push({
id: ++this.currentid,
msg: this.msg,
time: 15,
type: message_type.normal
})
}
}
}
}
</script>
<style lang="scss" scoped>
#danmu {
text-align: center;
color: #2c3e50;
}
.stage {
height: 300px;
width: 100%;
background: #025d63;
margin: 0;
position: relative;
overflow: hidden;
}
h1, h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
.baberrage-stage {
z-index: 5;
}
.baberrage-stage .baberrage-item.normal{
color:#fff;
}
.top{
border:1px solid #66aabb;
}
.danmu-control{
position: absolute;
margin: 0 auto;
width: 100%;
bottom: 300px;
top: 70%;
height: 69px;
box-sizing: border-box;
text-align: center;
display: flex;
justify-content: center;
div {
width: 300px;
background: rgba(0, 0, 0, 0.6);
padding: 15px;
border-radius: 5px;
border: 2px solid #8ad9ff;
}
input,button,select{
height:35px;
padding:0;
float:left;
background:#027fbb;
border:1px solid #ccc;
color:#fff;
border-radius:0;
width:18%;
box-sizing: border-box;
}
select{
height:33px;
margin-top:1px;
border: 0px;
outline: 1px solid rgb(204,204,204);
}
input{
width:64%;
height:35px;
background:rgba(0,0,0,.7);
border:1px solid #8ad9ff;
padding-left:5px;
color:#fff;
}
}
</style>
第六步:视图中使用组件
位置:resources/views/danmu.blade.php
@extends('layouts.app')
@section('content')
<danmu-component></danmu-component>
@endsection
第七步:注册路由
route::get('/danmu', function() {
return view('danmu');
});
执行 npm run dev
第八步:编写websocket服务器
文件:app\handlers\websockethandler.php
<?php
namespace app\handlers;
use hhxsv5\laravels\swoole\websockethandlerinterface;
use illuminate\support\facades\log;
use swoole\http\request;
use swoole\websocket\frame;
use swoole\websocket\server;
class websockethandler implements websockethandlerinterface
{
public function __construct()
{
}
// 连接建立时触发
public function onopen(server $server, request $request)
{
log::info('websocket 连接建立:' . $request->fd);
}
// 收到消息时触发
public function onmessage(server $server, frame $frame)
{
// $frame->fd 是客户端 id,$frame->data 是客户端发送的数据
log::info("从 {$frame->fd} 接收到的数据: {$frame->data}");
foreach($server->connections as $fd){
if (!$server->isestablished($fd)) {
// 如果连接不可用则忽略
continue;
}
$server->push($fd , $frame->data); // 服务端通过 push 方法向所有连接的客户端发送数据
}
}
// 连接关闭时触发
public function onclose(server $server, $fd, $reactorid)
{
log::info('websocket 连接关闭:' . $fd);
}
}
第九步:laravels.php注册
文件:config/laravels.php
'websocket' => [
'enable' => true,
'handler' => \app\handlers\websockethandler::class,
],
第十步:启动
php bin/laravels start
这样就完成啦
推荐学习:《laravel视频教程》
以上就是一文详解laravel8/laravels实现弹幕功能的详细内容。
