-
Notifications
You must be signed in to change notification settings - Fork 126
/
WebSocketLocalHandler.java
124 lines (102 loc) · 3.96 KB
/
WebSocketLocalHandler.java
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
118
119
120
121
122
123
124
package io.antmedia.enterprise.streamapp;
import java.io.IOException;
import org.apache.catalina.core.ApplicationContextFacade;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import io.antmedia.websocket.WebSocketCommunityHandler;
import io.antmedia.websocket.WebSocketConstants;
import jakarta.websocket.EndpointConfig;
import jakarta.websocket.OnClose;
import jakarta.websocket.OnError;
import jakarta.websocket.OnMessage;
import jakarta.websocket.OnOpen;
import jakarta.websocket.Session;
import jakarta.websocket.server.ServerEndpoint;
@ServerEndpoint(value="/websocket", configurator=AMSEndpointConfigurator.class)
public class WebSocketLocalHandler {
WebSocketCommunityHandler handler;
private String userAgent = "N/A";
protected static Logger logger = LoggerFactory.getLogger(WebSocketLocalHandler.class);
@OnOpen
public void onOpen(Session session, EndpointConfig config) {
if(config.getUserProperties().containsKey(AMSEndpointConfigurator.USER_AGENT)) {
userAgent = (String) config.getUserProperties().get(AMSEndpointConfigurator.USER_AGENT);
}
logger.info("Web Socket opened session:{} user-agent:{}", session.getId(), userAgent);
//increase max text buffer size - Chrome 90 requires
session.setMaxTextMessageBufferSize(8192 * 10);
}
@OnClose
public void onClose(Session session) {
if(handler != null) {
handler.onClose(session);
}
}
@OnError
public void onError(Session session, Throwable throwable) {
if(handler != null) {
handler.onError(session, throwable);
}
}
@OnMessage
public void onMessage(Session session, String message) {
if(handler == null) {
ConfigurableWebApplicationContext ctxt = null;
try {
ApplicationContextFacade servletContext = (ApplicationContextFacade) FieldUtils.readField(session.getContainer(), "servletContext", true);
ctxt = (ConfigurableWebApplicationContext) WebApplicationContextUtils.getWebApplicationContext(servletContext);
} catch (Exception e) {
logger.error("Application context can not be set to WebSocket handler");
logger.error(ExceptionUtils.getMessage(e));
}
if(ctxt != null && ctxt.isRunning()) {
createHandler(ctxt, session);
handler.onMessage(session, message);
}
else {
sendNotInitializedError(session);
}
}
else {
handler.onMessage(session, message);
}
}
private void createHandler(ApplicationContext context, Session session) {
try {
boolean rtmpForward;
try {
rtmpForward = session.getRequestParameterMap().get("rtmpForward").get(0).contains("true");
} catch (Exception e) {
rtmpForward = false;
}
// If user want to RTMP play, should add rtmp query in websocket URL.
if(io.antmedia.rest.RestServiceBase.isEnterprise() && !rtmpForward) {
Class clazz = Class.forName("io.antmedia.enterprise.webrtc.WebSocketEnterpriseHandler");
handler = (WebSocketCommunityHandler) clazz.getConstructor(ApplicationContext.class, Session.class).newInstance(context, session);
}
else {
handler = new WebSocketCommunityHandler(context, session);
}
handler.setUserAgent(userAgent);
} catch (Exception e) {
logger.error("WebSocket handler cannot be created");
logger.error(ExceptionUtils.getMessage(e));
}
}
public void sendNotInitializedError(Session session) {
JSONObject jsonResponse = new JSONObject();
jsonResponse.put(WebSocketConstants.COMMAND, WebSocketConstants.ERROR_COMMAND);
jsonResponse.put(WebSocketConstants.DEFINITION, WebSocketConstants.NOT_INITIALIZED_YET);
try {
session.getBasicRemote().sendText(jsonResponse.toJSONString());
} catch (IOException e) {
logger.error(ExceptionUtils.getStackTrace(e));
}
}
}