99
1010import java .io .IOException ;
1111import java .io .UnsupportedEncodingException ;
12- import java .lang .ref .WeakReference ;
1312import java .nio .ByteBuffer ;
1413import java .util .Arrays ;
1514import java .util .Collections ;
2827import org .apache .commons .lang3 .StringUtils ;
2928import org .apache .tomcat .websocket .Constants ;
3029import org .apache .tomcat .websocket .WsSession ;
30+ import org .red5 .net .websocket .model .WSMessage ;
3131import org .red5 .server .AttributeStore ;
3232import org .slf4j .Logger ;
3333import org .slf4j .LoggerFactory ;
@@ -50,9 +50,7 @@ public class WebSocketConnection extends AttributeStore implements Comparable<We
5050
5151 private static final Logger log = LoggerFactory .getLogger (WebSocketConnection .class );
5252
53- private static final boolean isTrace = log .isTraceEnabled ();
54-
55- private static final boolean isDebug = log .isDebugEnabled ();
53+ private static final boolean isTrace = log .isTraceEnabled (), isDebug = log .isDebugEnabled ();
5654
5755 // Sending async on windows times out
5856 private static boolean useAsync ;
@@ -69,7 +67,7 @@ public class WebSocketConnection extends AttributeStore implements Comparable<We
6967 private final WsSession wsSession ;
7068
7169 // reference to the scope for manager access
72- private WeakReference < WebSocketScope > scope ;
70+ private final WebSocketScope scope ;
7371
7472 // unique identifier for the session
7573 private final String wsSessionId ;
@@ -111,7 +109,9 @@ public class WebSocketConnection extends AttributeStore implements Comparable<We
111109 public WebSocketConnection (WebSocketScope scope , Session session ) {
112110 log .debug ("New WebSocket - scope: {} session: {}" , scope , session );
113111 // set the scope for ease of use later
114- this .scope = new WeakReference <>(scope );
112+ this .scope = scope ;
113+ // add the session to the user props
114+ session .getUserProperties ().put (WSConstants .WS_SCOPE , scope );
115115 // set our path
116116 path = scope .getPath ();
117117 if (isDebug ) {
@@ -167,15 +167,20 @@ public WebSocketConnection(WebSocketScope scope, Session session) {
167167 // add the timeouts to the user props
168168 userProps .put (Constants .READ_IDLE_TIMEOUT_MS , readTimeout );
169169 userProps .put (Constants .WRITE_IDLE_TIMEOUT_MS , sendTimeout );
170- // set the close timeout to 5 seconds
171- userProps .put (Constants .SESSION_CLOSE_TIMEOUT_PROPERTY , TimeUnit .SECONDS .toMillis (5 ));
170+ // write timeout used when sending WebSocket messages in blocking mode
171+ userProps .put (Constants .BLOCKING_SEND_TIMEOUT_PROPERTY , Long .getLong (Constants .BLOCKING_SEND_TIMEOUT_PROPERTY , 8000L ).longValue ());
172+ // write timeout Tomcat uses when writing a session close message when the close is abnormal
173+ userProps .put (Constants .ABNORMAL_SESSION_CLOSE_SEND_TIMEOUT_PROPERTY , Long .getLong (Constants .ABNORMAL_SESSION_CLOSE_SEND_TIMEOUT_PROPERTY , 10000L ).longValue ());
174+ // time Tomcat waits for a peer to send a WebSocket session close message after Tomcat has sent a close message
175+ // to the peer
176+ userProps .put (Constants .SESSION_CLOSE_TIMEOUT_PROPERTY , Long .getLong (Constants .SESSION_CLOSE_TIMEOUT_PROPERTY , 5000L ).longValue ());
172177 if (isDebug ) {
173178 log .debug ("userProps: {}" , userProps );
174179 }
175180 // set maximum messages size to 10,000 bytes
176181 session .setMaxTextMessageBufferSize (10000 );
177- // set maximum idle timeout to 30 seconds ( read timeout)
178- session .setMaxIdleTimeout (readTimeout );
182+ // set maximum idle timeout to the largest of the read and send timeouts
183+ session .setMaxIdleTimeout (Math . max ( readTimeout , sendTimeout ) );
179184 }
180185
181186 /**
@@ -320,6 +325,16 @@ public void sendPong(byte[] buf) throws IllegalArgumentException, IOException {
320325 }
321326 }
322327
328+ /**
329+ * Send a received message to the scope.
330+ *
331+ * @param wsMessage
332+ * WSMessage
333+ */
334+ public void onReceive (WSMessage wsMessage ) {
335+ scope .onMessage (wsMessage );
336+ }
337+
323338 /**
324339 * Close the connection.
325340 */
@@ -386,7 +401,7 @@ public static void setUseAsync(boolean useAsync) {
386401 * @return WebSocketScope
387402 */
388403 public WebSocketScope getScope () {
389- return scope != null ? scope . get () : null ;
404+ return scope ;
390405 }
391406
392407 /**
0 commit comments