当前位置: 首页 > article >正文

弹幕发送功能‘简单’实现

导入依赖

        <!--   websocket弹幕依赖   -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>

后端代码

package com.by.danmaku;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * @author lenovo
 * @version 0.1
 * @className WebSocketConfig
 * @date 2024/11/20 19:21
 * @since jdk11
 */

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 注册 WebSocket 处理器
        registry.addHandler(new DanmakuWebSocketHandler(), "/danmaku")
                .setAllowedOrigins("*"); // 允许所有来源,生产环境中应限制为特定来源
    }
}
package com.by.danmaku;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @author lenovo
 * @version 0.1
 * @className DanmakuWebSocketHandler
 * @date 2024/11/20 19:35
 * @since jdk11
 */
@Slf4j
@Component
public class DanmakuWebSocketHandler extends TextWebSocketHandler {

    private static final CopyOnWriteArraySet<WebSocketSession> sessions = new CopyOnWriteArraySet<>();

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        log.debug("欢迎进入直播间");
        sessions.add(session);
    }


    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        // 处理接收到的消息
        String payload = message.getPayload();
        log.debug("收到消息:{}",payload);
        for (WebSocketSession s : sessions){
            s.sendMessage(new TextMessage(payload));
        }
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        log.debug("有人离开直播间");
        sessions.remove(session);
    }

}

前端代码

<template>
  <div class="container">
  <vue-danmaku class="danmaku" v-model:danmus="danmus" :speeds="50" :channels="2" useSlot>
  <template v-slot:dm="{ danmu }">
  {{ danmu }}
  </template>
  </vue-danmaku>
  </div>

  <div>
  <el-input class="textarea" v-model="textarea" :rows="2" type="textarea" placeholder="请输入弹幕" />
  <el-button type="primary" @click="send">发送</el-button>
  </div>
  </template>

  <script setup lang="ts">
  import { ref, onMounted } from 'vue';
import vueDanmaku from 'vue3-danmaku';
import { wmsStore } from '../store/pinia';

const store = wmsStore();
const textarea = ref('');
const danmus = ref<string[]>([]);


const socket = new WebSocket('ws://localhost:8080/danmaku');

const send = () => {
  socket.send(textarea.value);
};

onMounted(() => {
  socket.onopen = function (event) {
    console.log('连接已打开!');
  };

  socket.onmessage = function (event) {
    const message = event.data;
    danmus.value.push(message);
    console.log(danmus.value);
  };

  socket.onclose = function (event) {
    console.log('断开!');
  };
});
</script>

  <style>
  .container {
  justify-content: center;
  display: flex;
  align-items: center;
}

.container .danmaku {
  height: 300px;
  width: 600px;
  background-image: url('/public/ims.png');
}

.textarea{
  width:400px;
  margin-top: 20px;
  margin-left: 100px;
  margin-right: 20px;
  display: inline-block;  
}
</style>


http://www.kler.cn/a/404407.html

相关文章:

  • C# 数据结构之【树】C#树
  • cocos creator 3.8 Node学习 3
  • RPC-健康检测机制
  • 音视频入门基础:MPEG2-TS专题(5)——FFmpeg源码中,判断某文件是否为TS文件的实现
  • .net6.0(.net Core)读取 appsettings.json 配置文件
  • 代码随想录:链表篇
  • 数据集论文:面向深度学习的土地利用场景分类与变化检测
  • 设计模式-Adapter(适配器模式)GO语言版本
  • 2024信创数据库TOP30之达梦DM8
  • php:nginx如何配置WebSocket代理?
  • 接雨水
  • 智能工厂的设计软件 为了监管控一体化的全能Supervisor 的监督学习 之 序8 进化论及科学的信息技术创新:分布式账本/区块链/智能合约 之2
  • yolov5 数据集分享:纯干货
  • GEE 训练教程——Sentinel-1的卷积(核函数)的分析和可视化
  • this.$prompt 限制输入长度
  • Windows环境GeoServer打包Docker极速入门
  • 出海第一步:搞定业务系统的多区域部署
  • 大模型-微调与对齐-非强化学习的对齐方法
  • CSS3 动画:前端开发的动态美
  • 实现了图像处理、绘制三维坐标系以及图像合成的操作
  • 对原jar包解压后修改原class文件后重新打包为jar
  • RestTemplate应用实践总结
  • 请问有什么限制预约报名人数的微信小程序/系统?
  • Arcgis 绘制地图
  • buuoj WEB做题笔记
  • scratch二次开发:控制blocks某些块不可以被删除