-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
989 additions
and
2 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
...se/207-websocket-chat/src/main/java/com/git/hui/boot/chat/config/MyEndpointConfigure.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.git.hui.boot.chat.config; | ||
|
||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.BeanFactory; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationContextAware; | ||
|
||
import javax.websocket.server.ServerEndpointConfig; | ||
|
||
/** | ||
* 解决注入其他类的问题 | ||
*/ | ||
public class MyEndpointConfigure extends ServerEndpointConfig.Configurator implements ApplicationContextAware { | ||
|
||
private static volatile BeanFactory context; | ||
|
||
@Override | ||
public <T> T getEndpointInstance(Class<T> clazz) { | ||
if (context == null) { | ||
return null; | ||
} | ||
return context.getBean(clazz); | ||
} | ||
|
||
@Override | ||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { | ||
MyEndpointConfigure.context = applicationContext; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
spring-case/207-websocket-chat/src/main/java/com/git/hui/boot/chat/rest/VideoController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package com.git.hui.boot.chat.rest; | ||
|
||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.messaging.handler.annotation.DestinationVariable; | ||
import org.springframework.messaging.handler.annotation.MessageMapping; | ||
import org.springframework.messaging.simp.SimpMessageHeaderAccessor; | ||
import org.springframework.stereotype.Controller; | ||
|
||
import javax.websocket.Session; | ||
import java.text.SimpleDateFormat; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author YiHui | ||
* @date 2023/12/12 | ||
*/ | ||
@Slf4j | ||
@Controller | ||
public class VideoController { | ||
@MessageMapping("video/{target}") | ||
public void videoChat(String message, @DestinationVariable("target") String target, SimpMessageHeaderAccessor headerAccessor) { | ||
log.info("接收到请求参数: {}", message); | ||
try { | ||
//jackson | ||
ObjectMapper mapper = new ObjectMapper(); | ||
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); | ||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | ||
|
||
//JSON字符串转 HashMap | ||
HashMap hashMap = mapper.readValue(message, HashMap.class); | ||
|
||
//消息类型 | ||
String type = (String) hashMap.get("type"); | ||
|
||
//to user | ||
String toUser = (String) hashMap.get("toUser"); | ||
String fromUser = (String) hashMap.get("fromUser"); | ||
|
||
//msg | ||
String msg = (String) hashMap.get("msg"); | ||
|
||
//sdp | ||
String sdp = (String) hashMap.get("sdp"); | ||
|
||
//ice | ||
Map iceCandidate = (Map) hashMap.get("iceCandidate"); | ||
|
||
HashMap<String, Object> map = new HashMap<>(); | ||
map.put("type", type); | ||
|
||
//对方挂断 | ||
if ("hangup".equals(type)) { | ||
map.put("fromUser", fromUser); | ||
map.put("msg", "对方挂断!"); | ||
} | ||
|
||
//视频通话请求 | ||
if ("call_start".equals(type)) { | ||
map.put("fromUser", fromUser); | ||
map.put("msg", "1"); | ||
} | ||
|
||
//视频通话请求回应 | ||
if ("call_back".equals(type)) { | ||
map.put("fromUser", toUser); | ||
map.put("msg", msg); | ||
} | ||
|
||
//offer | ||
if ("offer".equals(type)) { | ||
map.put("fromUser", toUser); | ||
map.put("sdp", sdp); | ||
} | ||
|
||
//answer | ||
if ("answer".equals(type)) { | ||
map.put("fromUser", toUser); | ||
map.put("sdp", sdp); | ||
} | ||
|
||
//ice | ||
if ("_ice".equals(type)) { | ||
map.put("fromUser", toUser); | ||
map.put("iceCandidate", iceCandidate); | ||
} | ||
|
||
WsAnswerHelper.publish(target, "/topic/video", mapper.writeValueAsString(map)); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
162 changes: 162 additions & 0 deletions
162
spring-case/207-websocket-chat/src/main/java/com/git/hui/boot/chat/rest/WebRtcWSServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package com.git.hui.boot.chat.rest; | ||
|
||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.git.hui.boot.chat.config.MyEndpointConfigure; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.websocket.OnClose; | ||
import javax.websocket.OnError; | ||
import javax.websocket.OnMessage; | ||
import javax.websocket.OnOpen; | ||
import javax.websocket.Session; | ||
import javax.websocket.server.PathParam; | ||
import javax.websocket.server.ServerEndpoint; | ||
import java.text.SimpleDateFormat; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
/** | ||
* WebRTC + WebSocket | ||
*/ | ||
@Slf4j | ||
@Component | ||
@ServerEndpoint(value = "/webrtc/{username}", configurator = MyEndpointConfigure.class) | ||
public class WebRtcWSServer { | ||
|
||
/** | ||
* 连接集合 | ||
*/ | ||
private static final Map<String, Session> sessionMap = new ConcurrentHashMap<>(); | ||
|
||
/** | ||
* 连接建立成功调用的方法 | ||
*/ | ||
@OnOpen | ||
public void onOpen(Session session, @PathParam("username") String username, @PathParam("publicKey") String publicKey) { | ||
sessionMap.put(username, session); | ||
} | ||
|
||
/** | ||
* 连接关闭调用的方法 | ||
*/ | ||
@OnClose | ||
public void onClose(Session session) { | ||
for (Map.Entry<String, Session> entry : sessionMap.entrySet()) { | ||
if (entry.getValue() == session) { | ||
sessionMap.remove(entry.getKey()); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 发生错误时调用 | ||
*/ | ||
@OnError | ||
public void onError(Session session, Throwable error) { | ||
error.printStackTrace(); | ||
} | ||
|
||
/** | ||
* 服务器接收到客户端消息时调用的方法 | ||
*/ | ||
@OnMessage | ||
public void onMessage(String message, Session session) { | ||
try { | ||
//jackson | ||
ObjectMapper mapper = new ObjectMapper(); | ||
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); | ||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | ||
|
||
//JSON字符串转 HashMap | ||
HashMap hashMap = mapper.readValue(message, HashMap.class); | ||
|
||
//消息类型 | ||
String type = (String) hashMap.get("type"); | ||
|
||
//to user | ||
String toUser = (String) hashMap.get("toUser"); | ||
Session toUserSession = sessionMap.get(toUser); | ||
String fromUser = (String) hashMap.get("fromUser"); | ||
|
||
//msg | ||
String msg = (String) hashMap.get("msg"); | ||
|
||
//sdp | ||
String sdp = (String) hashMap.get("sdp"); | ||
|
||
//ice | ||
Map iceCandidate = (Map) hashMap.get("iceCandidate"); | ||
|
||
HashMap<String, Object> map = new HashMap<>(); | ||
map.put("type", type); | ||
|
||
//呼叫的用户不在线 | ||
if (toUserSession == null) { | ||
toUserSession = session; | ||
map.put("type", "call_back"); | ||
map.put("fromUser", "系统消息"); | ||
map.put("msg", "Sorry,呼叫的用户不在线!"); | ||
|
||
send(toUserSession, mapper.writeValueAsString(map)); | ||
return; | ||
} | ||
|
||
//对方挂断 | ||
if ("hangup".equals(type)) { | ||
map.put("fromUser", fromUser); | ||
map.put("msg", "对方挂断!"); | ||
} | ||
|
||
//视频通话请求 | ||
if ("call_start".equals(type)) { | ||
map.put("fromUser", fromUser); | ||
map.put("msg", "1"); | ||
} | ||
|
||
//视频通话请求回应 | ||
if ("call_back".equals(type)) { | ||
map.put("fromUser", toUser); | ||
map.put("msg", msg); | ||
} | ||
|
||
//offer | ||
if ("offer".equals(type)) { | ||
map.put("fromUser", toUser); | ||
map.put("sdp", sdp); | ||
} | ||
|
||
//answer | ||
if ("answer".equals(type)) { | ||
map.put("fromUser", toUser); | ||
map.put("sdp", sdp); | ||
} | ||
|
||
//ice | ||
if ("_ice".equals(type)) { | ||
map.put("fromUser", toUser); | ||
map.put("iceCandidate", iceCandidate); | ||
} | ||
|
||
send(toUserSession, mapper.writeValueAsString(map)); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
/** | ||
* 封装一个send方法,发送消息到前端 | ||
*/ | ||
private void send(Session session, String message) { | ||
try { | ||
System.out.println(message); | ||
|
||
session.getBasicRemote().sendText(message); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.