1
1
package io .antmedia .enterprise .streamapp ;
2
2
3
+ import java .io .IOException ;
4
+
3
5
import javax .websocket .EndpointConfig ;
4
6
import javax .websocket .OnClose ;
5
7
import javax .websocket .OnError ;
12
14
import org .apache .commons .lang3 .exception .ExceptionUtils ;
13
15
import org .apache .commons .lang3 .reflect .FieldUtils ;
14
16
import org .apache .tomcat .websocket .server .DefaultServerEndpointConfigurator ;
17
+ import org .json .simple .JSONObject ;
15
18
import org .slf4j .Logger ;
16
19
import org .slf4j .LoggerFactory ;
17
- import org .springframework .web .context .WebApplicationContext ;
20
+ import org .springframework .context .ApplicationContext ;
21
+ import org .springframework .web .context .ConfigurableWebApplicationContext ;
18
22
import org .springframework .web .context .support .WebApplicationContextUtils ;
19
23
20
24
import io .antmedia .websocket .WebSocketCommunityHandler ;
25
+ import io .antmedia .websocket .WebSocketConstants ;
21
26
22
27
23
28
@ ServerEndpoint (value ="/websocket" , configurator =DefaultServerEndpointConfigurator .class )
@@ -28,43 +33,71 @@ public class WebSocketLocalHandler {
28
33
protected static Logger logger = LoggerFactory .getLogger (WebSocketLocalHandler .class );
29
34
30
35
@ 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
53
38
}
54
39
55
40
56
41
@ OnClose
57
42
public void onClose (Session session ) {
58
- handler .onClose (session );
43
+ if (handler != null ) {
44
+ handler .onClose (session );
45
+ }
59
46
}
60
47
61
48
@ OnError
62
49
public void onError (Session session , Throwable throwable ) {
63
- handler .onError (session , throwable );
50
+ if (handler != null ) {
51
+ handler .onError (session , throwable );
52
+ }
64
53
}
65
54
66
55
@ OnMessage
67
56
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
+ }
69
102
}
70
103
}
0 commit comments