srs信令java版

2023-10-10

java

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

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?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味😄

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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();
});
}
}