前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >srs信令java版

srs信令java版

作者头像
阿超
发布2023-10-18 14:55:04
2340
发布2023-10-18 14:55:04
举报
文章被收录于专栏:快乐阿超

会赚钱的人,即使身无分文,也还有自身这个财产。——亚兰

前两天讲到了 srs实现多人聊天室

但是遇到个问题,官方的信令是go语言版的,于是在gpt协助下翻译成java版了

https://gitee.com/VampireAchao/simple-srs-signaling

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>simple-srs-signaling</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>simple-srs-signaling</name>
    <description>simple-srs-signaling</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Spring Boot Starter WebSocket for WebSocket support -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>

        <!-- For STOMP messaging protocol over WebSocket -->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>webjars-locator-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>sockjs-client</artifactId>
            <version>1.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>stomp-websocket</artifactId>
            <version>2.3.3</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

当然,代码一股gpt味😄

代码语言:javascript
复制
package com.example.simplesrssignaling;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
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.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MyWebSocketHandler extends TextWebSocketHandler {

    private final Map<String, Room> nameRootMap = new ConcurrentHashMap<>();
    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        String payload = message.getPayload();

        ActionMessage actionMessage = objectMapper.readValue(payload, ActionMessage.class);
        String action = actionMessage.getMsg().getAction();

        switch (action) {
            case "join" -> handleJoin(actionMessage, session);
            case "publish" -> handlePublish(actionMessage, session);
            case "control" -> handleControl(actionMessage, session);
            default -> {
                // Log or send an error message for unrecognized actions.
            }
        }
    }

    private void handleJoin(ActionMessage actionMessage, WebSocketSession session) {
        String roomName = actionMessage.getMsg().getRoom();
        Room room = nameRootMap.computeIfAbsent(roomName, Room::new);

        Participant participant = new Participant();
        participant.setDisplay(actionMessage.getMsg().getDisplay());
        participant.setSession(session);
        room.add(participant);

        // Prepare response message
        var res = new HashMap<>();
        res.put("action", "join");
        res.put("room", actionMessage.getMsg().getRoom());
        res.put("self", participant); // Assuming you have a toJson method in Participant
        res.put("participants", room.getParticipants()); // Convert list of participants to JSONArray
        var message = new HashMap<>();
        message.put("msg", res);
        message.put("tid", actionMessage.getTid());
        sendMessage(session, message);

        // Notify other participants
        room.notify(session, participant, "join", actionMessage);
    }

    private void handlePublish(ActionMessage actionMessage, WebSocketSession session) {
        String roomName = actionMessage.getMsg().getRoom();
        Room room = nameRootMap.get(roomName);
        if (room != null) {
            Participant participant = room.get(actionMessage.getMsg().getDisplay());
            if (participant != null) {
                participant.setPublishing(true);

                // Notify other participants
                room.notify(session, participant, "publish", actionMessage);
            }
        }
    }

    private void handleControl(ActionMessage actionMessage, WebSocketSession session) {
        String roomName = actionMessage.getMsg().getRoom();
        Room room = nameRootMap.get(roomName);
        if (room != null) {
            Participant participant = room.get(actionMessage.getMsg().getDisplay());
            if (participant != null) {
                // You might need to handle more about control like starting a call, ending a call, etc.

                // Notify other participants
                room.notify(session, participant, "control", actionMessage);
            }
        }
    }

    @SneakyThrows
    private void sendMessage(WebSocketSession session, Object message) {
        if (!session.isOpen()) {
            return;
        }
        String jsonMessage = objectMapper.writeValueAsString(message);
        session.sendMessage(new TextMessage(jsonMessage));
    }

    // Define classes like ActionMessage, JoinResponse, and possibly more based on your needs.
    // This provided code should serve as a base structure upon which you can further build as per requirements.


    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        super.afterConnectionClosed(session, status);
        nameRootMap.values().removeIf(room -> {
            var participants = room.getParticipants();
            for (Participant participant : participants) {
                if (session.getId().equals(participant.getSession().getId())) {
                    room.remove(participant);
                    var actionMessage = new ActionMessage();
                    room.notify(session, participant, "leave", actionMessage);
                    break;
                }
            }
            return participants.isEmpty();
        });
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-10-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档