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 -> { } } }
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);
var res = new HashMap<>(); res.put("action", "join"); res.put("room", actionMessage.getMsg().getRoom()); res.put("self", participant); res.put("participants", room.getParticipants()); var message = new HashMap<>(); message.put("msg", res); message.put("tid", actionMessage.getTid()); sendMessage(session, message);
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);
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) {
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)); }
@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(); }); } }
|