Skip to content

Commit 2566d27

Browse files
author
Yi
committed
完成逻辑
0 parents  commit 2566d27

File tree

14 files changed

+228
-0
lines changed

14 files changed

+228
-0
lines changed

‎.idea/.gitignore‎

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/compiler.xml‎

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/encodings.xml‎

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/jarRepositories.xml‎

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/misc.xml‎

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pom.xml‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.7.10</version>
9+
</parent>
10+
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<groupId>com.beiyoufamily</groupId>
14+
<artifactId>video-call-java</artifactId>
15+
<version>1.0-SNAPSHOT</version>
16+
17+
<properties>
18+
<maven.compiler.source>8</maven.compiler.source>
19+
<maven.compiler.target>8</maven.compiler.target>
20+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21+
<fastjson.version>2.0.32</fastjson.version>
22+
</properties>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-websocket</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.projectlombok</groupId>
31+
<artifactId>lombok</artifactId>
32+
<optional>true</optional>
33+
</dependency>
34+
<dependency>
35+
<groupId>com.alibaba</groupId>
36+
<artifactId>fastjson</artifactId>
37+
<version>${fastjson.version}</version>
38+
</dependency>
39+
</dependencies>
40+
41+
</project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.beiyoufamily;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* @author Yi Dai daiyi.lucky@qq.com
8+
* @since 2023/8/8 14:40
9+
*/
10+
11+
@SpringBootApplication
12+
public class Application {
13+
14+
public static void main(String[] args) {
15+
SpringApplication.run(Application.class, args);
16+
}
17+
18+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.beiyoufamily.web.socket;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.springframework.stereotype.Component;
6+
7+
import javax.websocket.OnClose;
8+
import javax.websocket.OnError;
9+
import javax.websocket.OnOpen;
10+
import javax.websocket.Session;
11+
import javax.websocket.server.PathParam;
12+
import javax.websocket.server.ServerEndpoint;
13+
import java.io.IOException;
14+
import java.util.Map;
15+
import java.util.Objects;
16+
import java.util.concurrent.ConcurrentHashMap;
17+
18+
/**
19+
* @author Yi Dai daiyi.lucky@qq.com
20+
* @since 2023/8/8 14:40
21+
*/
22+
23+
@Slf4j
24+
@Component
25+
@ServerEndpoint("/peerServerEndpoint/{peerId}")
26+
public class PeerServerEndpoint {
27+
28+
private static final Map<String, Session> peerIdSessionMap = new ConcurrentHashMap<>();
29+
30+
@OnOpen
31+
public void onOpen(Session session, @PathParam("peerId") String peerId) {
32+
log.info("on open:the session is is :{},the peer id is:{}", session.getId(), peerId);
33+
Session removedSession = PeerServerEndpoint.peerIdSessionMap.remove(peerId);
34+
try {
35+
if (Objects.nonNull(removedSession)) {
36+
removedSession.close();
37+
}
38+
} catch (IOException e) {
39+
throw new RuntimeException(e);
40+
} finally {
41+
PeerServerEndpoint.peerIdSessionMap.put(peerId, session);
42+
refreshOnlineSessionsList();
43+
}
44+
}
45+
46+
@OnClose
47+
public void onClose(Session session, @PathParam("peerId") String peerId) {
48+
log.warn("on close:the session is is :{},the peer id is:{}", session.getId(), peerId);
49+
50+
Session removedSession = PeerServerEndpoint.peerIdSessionMap.remove(peerId);
51+
try {
52+
if (Objects.nonNull(removedSession)) {
53+
removedSession.close();
54+
}
55+
} catch (IOException e) {
56+
throw new RuntimeException(e);
57+
} finally {
58+
refreshOnlineSessionsList();
59+
}
60+
}
61+
62+
@OnError
63+
public void onError(Session session, Throwable e, @PathParam("peerId") String peerId) {
64+
log.error("on error:the session is is :{},the exception class is: {},the peer id is:{}", session.getId(), e.getClass(), peerId);
65+
onClose(session, peerId);
66+
67+
e.printStackTrace();
68+
}
69+
70+
private void refreshOnlineSessionsList() {
71+
PeerServerEndpoint.peerIdSessionMap.forEach((key, value) -> {
72+
value.getAsyncRemote().sendText(JSON.toJSONString(PeerServerEndpoint.peerIdSessionMap.keySet()));
73+
});
74+
}
75+
76+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.beiyoufamily.web.socket.configuration;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
6+
7+
/**
8+
* @author Yi Dai daiyi.lucky@qq.com
9+
* @since 2023/8/8 14:40
10+
*/
11+
12+
@Configuration
13+
public class ServerEndpointExporterConfiguration {
14+
15+
@Bean
16+
public ServerEndpointExporter serverEndpointExporter() {
17+
return new ServerEndpointExporter();
18+
}
19+
20+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
server:
2+
port: 8081

0 commit comments

Comments
 (0)