We use cookies to enhance your browsing experience, serve personalized ads or content, and analyze our traffic. By clicking "Accept All", you consent to our use of cookies. Click here for more information on our Privacy Policy.
Customize Consent Preferences
We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.
The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ...
Always Active
Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.
No cookies to display.
Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.
No cookies to display.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.
No cookies to display.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
No cookies to display.
Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.
Websockets can be enabled for a Red5pro application through its Application adapter class. The recommended way of doing this is to register websocket for the application in the appStart handler and remove it using the appStop handler. The code snippet given below shows how the Application adapter of the chat application is used for registering and unregistering with the websocket plugin.
publicclassApplicationextendsMultiThreadedApplicationAdapterimplementsApplicationContextAware{privatestaticLogger log =Red5LoggerFactory.getLogger(Application.class,"chat");privateApplicationContext applicationContext;@Overridepublicvoid setApplicationContext(ApplicationContext applicationContext)throwsBeansException{this.applicationContext = applicationContext;}@Overridepublicboolean appStart(IScope scope){
log.info("Chat starting");
configureApplicationScopeWebSocket(scope);returnsuper.appStart(scope);}@Overridepublicvoid appStop(IScope scope){
log.info("Chat stopping");// remove our appWebSocketScopeManager manager =((WebSocketPlugin)PluginRegistry.getPlugin("WebSocketPlugin")).getManager(scope);
manager.removeApplication(scope);
manager.stop();}/**
* Configures a websocket scope for a given application scope.
*
* @param scope Server application scope
*/privatevoid configureApplicationScopeWebSocket(IScope scope){// first get the websocket pluginWebSocketPlugin wsPlugin =((WebSocketPlugin)PluginRegistry.getPlugin(WebSocketPlugin.NAME));// get the websocket scope manager for the red5 scopeWebSocketScopeManager manager = wsPlugin.getManager(scope);if(manager ==null){// get the application adapterMultiThreadedApplicationAdapter app =(MultiThreadedApplicationAdapter) scope.getHandler();
log.debug("Creating WebSocketScopeManager for {}", app);// set the application in the plugin to create a websocket scope manager for it
wsPlugin.setApplication(app);// get the new manager
manager = wsPlugin.getManager(scope);}// the websocket scopeWebSocketScope wsScope =(WebSocketScope) scope.getAttribute(WSConstants.WS_SCOPE);// check to see if its already configuredif(wsScope ==null){
log.debug("Configuring application scope: {}", scope);// create a websocket scope for the application
wsScope =newWebSocketScope(scope);// register the ws scope
wsScope.register();}}}
Once the application is registered with the websocket plugin, the next step is to create your websocket handler class, extending the org.red5.net.websocket.listener.WebSocketDataListener class. This class will handle the standard server side websocket events for the clients. An example of the implementation would be the WebSocketChatDataListener.java class.
Finally we tell the application to use this WebSocketDataListener implementation to handle all websocket requests to our Red5pro application. This is done by adding a Bean definition to the context file (red5-web.xml) of the Red5pro application.
If you look at the Red5 red5-web.xml file of the red5 websocket chat sample application, you will see how a reference of the Red5 application is made available to the websocket listener via the virtual router (Router.java) using spring bean configuration.
<beanid="web.handler"class="org.red5.demos.chat.Application"/><beanid="router"class="org.red5.demos.chat.Router"><propertyname="app"ref="web.handler"/></bean><!-- WebSocket scope with our listeners --><beanid="webSocketScopeDefault"class="org.red5.net.websocket.WebSocketScope"lazy-init="true"><!-- Application scope --><constructor-argref="web.scope"/><!-- The ws scope listeners --><propertyname="listeners"><list><beanid="chatListener"class="org.red5.demos.chat.WebSocketChatDataListener"><propertyname="router"ref="router"/></bean></list></property></bean>
Websocket filter
Lastly, the websocket filter must be added to each web application that will act as a websocket end point. In the webapp descriptor webapps/myapp/WEB-INF/web.xml add this entry alongside any other filters or servlets.