-
Notifications
You must be signed in to change notification settings - Fork 18
Working With Connections
Red5 is a very versatile media server.The open source version supports RTMP, Websockets, whereas the professional version augments with RTSP, HLS, and WebRTC connections. Since this is our open source targeted topic. Within Red5 server, all standard connections (other than HLS, WebRTC, and WebSockets) conform to the IConnection
interface. WebSocket connections, on the other hand, are of type WebSocketConnection
.
This section will discuss only the IConnection
object here. We will look at WebSocketConnection
in details when we go over WebSockets
in details (in later lessons).
In Red5 generally, all connections coming from a RTMP client such as flash / java are of type IConnection
. In Red5pro this would extend to RTSP connections as well. The connection handlers in Application main class (as we saw earlier) pass in the IConnection
object associated with that connection.
I can then use this object to run different operations on the connection. you can find details of all methods / properties offered by the IConnection
interface here: http://red5.org/javadoc/red5-server-common/org/red5/server/api/IConnection.html
Looking at its sub-classes you will notice that a IConnection
is implemented by RTMPConnection
and RTMPMinaConnection
, and is inherited by interfaces like IServiceCapableConnection
and IStreamCapableConnection
. Going further you will notice that its parent interfaces suggest that it is capable of storing attributes as well. In a gist this highlights the overall capability of the IConnection
object.
Let us see as few short snippet based examples on how we can interact with the IConnection
object.
While each handler does provide the IConnection
object associated with the connection that causes it to be triggered (such as appConnect or appDisconnect handlers), sometimes you just need to access the current client connection. "CurrentConnection" in this context means, the connection which causes the code to be triggered.
To access the IConnection
associated with the current(accessing) connection inside any method use the following code :
IConnection conn = Red5.getConnectionLocal();
You can disconnect a connection from within a handler or a custom method. To disconnect the current(accessing) connection from the application you can use the following snippet from within any method or handler.
IConnection conn = Red5.getConnectionLocal();
conn.close();
Or, to close the connection of a client attempting to connect, immediately you can do the following.
@Override
public boolean appConnect(IConnection conn, Object[] arg1) {
conn.close();
return super.appConnect(arg0, arg1);
}
You can store simple / complex data on the IConnection object itself, since it is capable of storing attributes on itself. To store a simple information such as a username on a client connection, you can do the following.
@Override
public boolean appConnect(IConnection conn, Object[] arg1) {
conn.setAttribute("username", "rajdeep");
return super.appConnect(arg0, arg1);
}
you can then use the getAttribute
method to fetch the attribute value at any other part of the code.
As explained earlier all resources are encapsulated within a 'SCOPE'. The application itself is the top level scope. you can get a list of any resource type you want from the application scope. To get a list of connections in your application try, the following function:
public Set<IConnection> getAllConnections()
{
return this.getScope().getClientConnections();
}
Connections in Red5, are managed sessions with a special session id attached to them. Within a connected lifespan, the session ID does not change. You can use this to identify a connection / client. The following code snippet can be used to fetch the current connection's ID.
IConnection conn = Red5.getConnectionLocal();
String connectionId = conn.getSessionId();
Red5 can also trigger a method on the client side for RTMP clients with arbitrary parameters. This is typically used to control the client state from the server. To call a client side method from Red5 you can use the following snippet :
IConnection conn = Red5.getConnectionLocal();
conn.invoke("someClientMethod", new Object[]{1,"hi",true});
There are other variations of the invoke
method. I suggest you to explore them on your own to learn more about them.
While these are a few instances of how you van interact with the IConnection
object in Red5, its certainly limited to this in any way. You can a lot more as you move through the development process yourself.