Skip to content

Commit d8b8794

Browse files
committed
refactor
1 parent 81e180c commit d8b8794

File tree

2 files changed

+61
-28
lines changed

2 files changed

+61
-28
lines changed

.project

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
</arguments>
1717
</buildCommand>
1818
<buildCommand>
19-
<name>org.eclipse.m2e.core.maven2Builder</name>
19+
<name>org.eclipse.wst.validation.validationbuilder</name>
2020
<arguments>
2121
</arguments>
2222
</buildCommand>
2323
<buildCommand>
24-
<name>org.eclipse.wst.validation.validationbuilder</name>
24+
<name>org.eclipse.m2e.core.maven2Builder</name>
2525
<arguments>
2626
</arguments>
2727
</buildCommand>
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.antmedia.enterprise.streamapp;
22

3+
import java.io.IOException;
4+
35
import javax.websocket.EndpointConfig;
46
import javax.websocket.OnClose;
57
import javax.websocket.OnError;
@@ -12,12 +14,15 @@
1214
import org.apache.commons.lang3.exception.ExceptionUtils;
1315
import org.apache.commons.lang3.reflect.FieldUtils;
1416
import org.apache.tomcat.websocket.server.DefaultServerEndpointConfigurator;
17+
import org.json.simple.JSONObject;
1518
import org.slf4j.Logger;
1619
import org.slf4j.LoggerFactory;
17-
import org.springframework.web.context.WebApplicationContext;
20+
import org.springframework.context.ApplicationContext;
21+
import org.springframework.web.context.ConfigurableWebApplicationContext;
1822
import org.springframework.web.context.support.WebApplicationContextUtils;
1923

2024
import io.antmedia.websocket.WebSocketCommunityHandler;
25+
import io.antmedia.websocket.WebSocketConstants;
2126

2227

2328
@ServerEndpoint(value="/websocket", configurator=DefaultServerEndpointConfigurator.class)
@@ -28,43 +33,71 @@ public class WebSocketLocalHandler {
2833
protected static Logger logger = LoggerFactory.getLogger(WebSocketLocalHandler.class);
2934

3035
@OnOpen
31-
public void onOpen(Session session, EndpointConfig config)
32-
{
33-
try {
34-
ApplicationContextFacade servletContext = (ApplicationContextFacade) FieldUtils.readField(session.getContainer(), "servletContext", true);
35-
WebApplicationContext ctxt = WebApplicationContextUtils.getWebApplicationContext(servletContext);
36-
37-
if(io.antmedia.rest.RestServiceBase.isEnterprise()) {
38-
Class clazz = Class.forName("io.antmedia.enterprise.webrtc.WebSocketEnterpriseHandler");
39-
handler = (WebSocketCommunityHandler) clazz.newInstance();
40-
}
41-
else {
42-
handler = new WebSocketCommunityHandler();
43-
}
44-
handler.setAppContext(ctxt);
45-
46-
handler.onOpen(session, config);
47-
logger.error("WebSocket opened for {}", ctxt.getApplicationName());
48-
49-
} catch (Exception e) {
50-
logger.error("Exception in WebSocket handler open");
51-
logger.error(ExceptionUtils.getMessage(e));
52-
}
36+
public void onOpen(Session session, EndpointConfig config) {
37+
//do nothing
5338
}
5439

5540

5641
@OnClose
5742
public void onClose(Session session) {
58-
handler.onClose(session);
43+
if(handler != null) {
44+
handler.onClose(session);
45+
}
5946
}
6047

6148
@OnError
6249
public void onError(Session session, Throwable throwable) {
63-
handler.onError(session, throwable);
50+
if(handler != null) {
51+
handler.onError(session, throwable);
52+
}
6453
}
6554

6655
@OnMessage
6756
public void onMessage(Session session, String message) {
68-
handler.onMessage(session, message);
57+
if(handler == null) {
58+
ConfigurableWebApplicationContext ctxt = null;
59+
try {
60+
ApplicationContextFacade servletContext = (ApplicationContextFacade) FieldUtils.readField(session.getContainer(), "servletContext", true);
61+
ctxt = (ConfigurableWebApplicationContext) WebApplicationContextUtils.getWebApplicationContext(servletContext);
62+
} catch (Exception e) {
63+
logger.error("Application context can not be set to WebSocket handler");
64+
logger.error(ExceptionUtils.getMessage(e));
65+
}
66+
67+
if(ctxt != null && ctxt.isRunning()) {
68+
createHandler(ctxt, session);
69+
handler.onMessage(session, message);
70+
}
71+
}
72+
else {
73+
handler.onMessage(session, message);
74+
}
75+
}
76+
77+
private void createHandler(ApplicationContext context, Session session) {
78+
try {
79+
if(io.antmedia.rest.RestServiceBase.isEnterprise()) {
80+
Class clazz = Class.forName("io.antmedia.enterprise.webrtc.WebSocketEnterpriseHandler");
81+
handler = (WebSocketCommunityHandler) clazz.getConstructor(ApplicationContext.class).newInstance(context);
82+
}
83+
else {
84+
handler = new WebSocketCommunityHandler(context, session);
85+
}
86+
} catch (Exception e) {
87+
logger.error("WebSocket handler cannot be created");
88+
logger.error(ExceptionUtils.getMessage(e));
89+
}
90+
}
91+
92+
93+
public void sendNotInitializedError(Session session) {
94+
JSONObject jsonResponse = new JSONObject();
95+
jsonResponse.put(WebSocketConstants.COMMAND, WebSocketConstants.ERROR_COMMAND);
96+
jsonResponse.put(WebSocketConstants.DEFINITION, WebSocketConstants.NOT_INITIALIZED_YET);
97+
try {
98+
session.getBasicRemote().sendText(jsonResponse.toJSONString());
99+
} catch (IOException e) {
100+
logger.error(ExceptionUtils.getStackTrace(e));
101+
}
69102
}
70103
}

0 commit comments

Comments
 (0)