Saturday, August 25, 2007

Adding client information with every client in Red5

In Red5 forum, I have seen so many times, developers are asking for custom client id to identify a client. Below I will try to explain how can one add additional client information with every client connected to your application.

First we will create a client object--

public class Client{
String clientId = null;
String clienName = null;
String clientRole = null;

public String getRole(){
return clientRole;
}
public String getName(){
return clientName;
}
public String getId(){
return clientId;
}
public void setRole(String role){
clientRole = role;
}
public void setName(String name){
clientName = name;
}
public void setId(String id){
clientId = id;
}
}

Now we are ready with our client object, We will add this client object with the IClient in Red5 like below

public boolean roomConnect(IConnection iconnection, Object params[]){
if(!super.roomConnect(iconnection, params)){
log.info((new StringBuilder()).append("Application failed to connect room: ").append(iconnection.getScope().getName()).toString());
return false;
}
else{
log.info((new StringBuilder()).append("Application room connect initiated for room ").append(iconnection.getScope().getName()).append(": ").toString());

//We will add our client information right here
Client client = new Client();
client.setId(params[0].toString());
client.setName(params[1].toString());
client.setRole(params[2].toString());
iconnection.getClient().setAttribute("client", client);

return true
}
}

public boolean roomJoin(IClient iclient, IScope iscope){

/*Here you can see how to access your client information throughout the application*/

Client client = ((Client)iclient.getAttribute("client"));
String clientName = client.getName();
String clientId = client.getId();
String clientRole = client.getRole();

return true;
}

This way you can have your own client id's names and roles for different clients.

To create new Red5 application follow my previous post here.

12 comments:

4rif wicaksono said...

dear sunil,

i'm very much newbie on red5, i try to make a video conference application, and i get stuck at at updating a userlist , i’m using clientmanager code provided by

* @author Joachim Bauch (jojo@struktur.de)
* org.red5.samples.components.ClientManager
.

but i have a problem, i can login as a new Chat-User and the Userlist is getting updated, however, it only updates the userlist for the one user who has just logged in.So the userlist for all the other users, who had already logged in before, is not getting updated.

can you tell me how to syncrhronize the userlist to other users who had already logged in?

i really appriciated if you want to help me,
thanks you very much.

Sunil Kumar Gupta said...

Hi Wicaksono,

Thanks for writing. I would suggest you to look into SharedObject. For user managment, i would prefer SharedObject.

Create a SharedObject on the server. Every time a new user login, just put the information in the SharedObject. Since every client is in sync with SharedObject, he will get information of new client.

Check this

SharedObject

Regards,
Sunil

4rif wicaksono said...

well i did try using sharedObject, but i found another problem using sharedObject, when i'm using sharedObject the first user get the userlist updated(when new user logged it the i send the username using sharedObject and add it at the list of user).

but the if there is new user he doesn't get the users list that already logged in.

so try to combine this two code, there a problem since i send the sharedObject as a string but the list(from client manager code) is using array, i don't know how to push it into the array.

can you tell show me how to push a value into an array, if you dont mind i've sent you my code.so you can undstand what i'm facing and help me to solve it.hope you dont mind to take a look.

i really appriciate your help and reply. thank you

Sunil Kumar Gupta said...

Hi Arif Wicaksono,

I did not receive your code yet. But i would suggest you to have SharedObject. Here i tell you the solution of your problem.

You have SharedObject at the server side. Now when a new user joins, you do something like this.

SharedObject.setAttribute("userid", "username");

The moment, you set SharedObject Attribute, all clients who are having onSync method will receive this change at client side. So, the moment, new user joins, every user gets the notification.

Suppose, you have 4 users already login and hence you have 4 attributes in SharedObject. Now when new user joins, he will receive all these 4 attributes in his onSync method. It means wheather you are a new user or existing user. Everyone will be in sync.

The same way when someone logout, you just need to do something like this

SharedObject.removeAttribute("userid");

This will again called onSync method on every connected client and you can then remove this id from every client.

Hope that helps.

Regards,
Sunil

4rif wicaksono said...

dear sunil, i've solved the problem now, but i have another question now, my question is how i get the multiple users webcam, i already using repeater and the dataprovider is the userarray,

but i always get the same webcam(if two users logged in, the camera windows will show two windows with one camera source, and it's my camera),,can u help me sunil?

in camera component i only attach the camera, can u suggest me how to fix this?

thank u very much for replying

ps: my eamil failed to sent. i dont know why.

4rif wicaksono said...

this is more code suni, this code is a component to my main application.

ns = new NetStream( nc );
video = new Video( 180, 180 );
mic.setLoopBack(true);
camera.setLoopback(true);
video.attachCamera( camera );
ns.attachCamera(camera);mic = Microphone.getMicrophone();
ns.attachAudio( mic );
ns.publish( userItem );

videoDisplay.video = video;

user item is userID from every user, bu i cannot get other camera, i only get mine, can you help me sunil?

Sunil Kumar Gupta said...

Hi Wicaksono,

Getting other's stream is very simple. Understand it in this way, You have one publisher and one subscriber. Now publisher has some id say 1 and he publish his stream with this id. e.g. publish(1)
Now subscriber wants to subscribe this stream, what he will do, he will just play this stream. e.g. play(1)

So you can have as many as subscribers that do the same.

When you do ns.publish( userItem );

This userItem is the name of stream that is available in server for others to subscribe. All you have to do is to play this stream from other clients.

Hope that helps.

4rif wicaksono said...

thank for replying sunil, with your help it works now, but i always get this error

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at VideoPod/init()[G:\Documents and Settings\paktua\My Documents\Flex Builder 3\_re_index\src\VideoPod.mxml:58]
at VideoPod/___VideoPod_Panel1_creationComplete()[G:\Documents and Settings\paktua\My Documents\Flex Builder 3\_re_index\src\VideoPod.mxml:22]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()[E:\dev\3.1.0\frameworks\projects\framework\src\mx\core\UIComponent.as:9156]
at mx.core::UIComponent/set initialized()[E:\dev\3.1.0\frameworks\projects\framework\src\mx\core\UIComponent.as:1167]

and the camera wont show if i'm not "dismiss all" button first. is that normal? how i can fix this sunil,?

i really appreciate your help, thank you very much

Sunil Kumar Gupta said...

Hi Wicaksono,

This is very common error we see in Flex. Mainly because we access the method or properties of an object which is null.

It's showing error in VideoPod.mxml at line 58. Check for such case, where you are acessing properties or methods on an object which is not yet initialized or have null in it.

Hope that helps.

4rif wicaksono said...

dear sunil,

the problem is here sunil :

video.attachNetStream( ns );
ns.play( userItem.identifier );

this code, is to subscribe the stream like you said, i've read that i must initiated the variable, i've try many ways but i not works, can you suggest to me how to fix this sunil?

and when i try to set the value of

userItem:Object = null;

i got this error

Error #2044: Unhandled NetStatusEvent:. level=error, code=NetStream.Publish.BadName
at VideoPod/init()

Sunil Kumar Gupta said...

Hi Arif,

I am not sure what do you mean by initiating a variable. ns.play("AnyName")

AnyName could be any String being published.

And regarding NetStream.Publish.BadName

if someone is already publishing on a stream with the specified name, NetStream.onStatus is called with a code property of NetStream.Publish.BadName

It means, you are trying to publish the stream twice with same name.

Hope that helps.

Regards,
Sunil

4rif wicaksono said...

dear sunil,

emm, can you explain to me how to make a web base conference, i've syncronize the userlist, i think( so far ) that i can conference between two people(in my
previous application when some users logged in, they directly join the conferences, there's only one room for all). but i want to make it more advance now.i want that when i logged in, there
will be the userlist button, so when i clicked one of other user button example : A's button, so my application will create new private conference between me
and A. and of course i can invite more than one users.

can you explain how to do this sunil? do you have any tutorial about this? i'm sorry to keep asking to you, i get this task from my lecture, i really need

your help.
btw i've sent my previous code to you ( sunil_gupta20801@yahoo.co.in ) if you dont mind to see it, so you can explain to me what should i change from my

application.

i'm very appriciate your help, many thanks to you sunil.

ps: my email is 4rifwicaksono@gmail.com

Daily Technology Tips

Catch me at Mashable

PayPal

Sign up for PayPal and start accepting credit card payments instantly.

Blog Explosion


Bloglines

Blogrush


BlogsByCategory.com