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

导入依赖

XML 复制代码
        <!--   websocket弹幕依赖   -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>

后端代码

java 复制代码
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("*"); // 允许所有来源,生产环境中应限制为特定来源
    }
}
java 复制代码
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);
    }

}

前端代码

javascript 复制代码
<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>
相关推荐
计算机毕设指导69 分钟前
基于Springboot学生宿舍水电信息管理系统【附源码】
java·spring boot·后端·mysql·spring·tomcat·maven
计算机-秋大田17 分钟前
基于Spring Boot的兴顺物流管理系统设计与实现(LW+源码+讲解)
java·vue.js·spring boot·后端·spring·课程设计
小蒜学长2 小时前
医疗报销系统的设计与实现(代码+数据库+LW)
数据库·spring boot·学习·oracle·课程设计
橘猫云计算机设计2 小时前
基于SSM的《计算机网络》题库管理系统(源码+lw+部署文档+讲解),源码可白嫖!
java·数据库·spring boot·后端·python·计算机网络·毕设
小盼江2 小时前
水果生鲜农产品推荐系统 协同过滤余弦函数推荐水果生鲜农产品 Springboot Vue Element-UI前后端分离 代码+开发文档+视频教程
vue.js·spring boot·ui
坚定信念,勇往无前3 小时前
Spring Boot中整合Flink CDC 数据库变更监听器来实现对MySQL数据库
数据库·spring boot·flink
坚定信念,勇往无前4 小时前
Spring Boot 如何保证接口安全
spring boot·后端·安全
尚学教辅学习资料5 小时前
基于SpringBoot+Vue+uniapp的高校招聘小程序+LW参考示例
spring boot·uni-app·招聘系统
Pro_er5 小时前
Vue3 性能优化十大技巧:打造高性能应用的终极指南
vue·前端开发
m0_748247556 小时前
springboot中配置logback-spring.xml
spring boot·spring·logback