header
ask question
Click here to ask Question Now Its free No registration required. Flash, Flex, Flash Media Server, ActionScript,Adobe Air. Most questions receive a response in an hour.
george
Points:0
Posts:0
Answered

1/6/2011 2:49:29 PM

Title: red5chat rooms problem


Hello. I have managed to install and setup red5 server and red5chat (source code taken from (http://sourceforge.net/projects/red5chat/files/red5chat/v1/red5chatv2.1.zip/download). The application works fine but there is a problem changing room. Although there is a choice to the application to choose between 3 rooms, this choice doesn't seems to work. This happens because all choices connects to the same rtmp link, as shown to actionscript code bellow:



function enterFriendRoom() {
	_root.room="chat";
	_root.rtmpString = "rtmp://"+_root.IP+':'+_root.port+"/"+_root.room;
	init_chat();
	TransitionManager.start(rooms_mc, {type:Squeeze, direction:1, duration:2, easing:Elastic.easeIn, dimension:1});
}
function enterLoveRoom() {
	_root.room="chat";
	_root.rtmpString = "rtmp://"+_root.IP+':'+_root.port+"/"+_root.room;
	init_chat();
	TransitionManager.start(rooms_mc, {type:Squeeze, direction:1, duration:2, easing:Elastic.easeIn, dimension:1});
}
function enterHotRoom() {
	_root.room="chat";
	_root.rtmpString = "rtmp://"+_root.IP+':'+_root.port+"/"+_root.room;
	init_chat();
	TransitionManager.start(rooms_mc, {type:Squeeze, direction:1, duration:2, easing:Elastic.easeIn, dimension:1});
}


My application name is "chat".
I tried to do the following changes but none of this works:
_root.room="newroom"
_root.room="chat/newroom"
I just cannot create a new room.

The server side Application.java code is shown bellow:




package chat;
import org.red5.server.adapter.ApplicationAdapter;
import org.red5.server.api.IConnection;
import org.red5.server.api.IClient;
import org.red5.server.api.IScope;
import org.red5.server.api.Red5;
import org.red5.server.api.service.IServiceCapableConnection;
import org.red5.server.api.so.ISharedObject;
import org.red5.server.api.ScopeUtils;
import org.red5.server.api.so.ISharedObjectService;
import org.red5.server.api.*;
import org.red5.server.api.scheduling.*;
import org.red5.samples.components.ClientManager;
import org.red5.server.api.service.ServiceUtils;
import org.red5.server.api.service.*;
import org.red5.server.api.service.IPendingServiceCallback;

//
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.*;
import java.util.HashMap;
//
//

public class Application extends ApplicationAdapter{	
	public HashMap<String,User> users = new HashMap<String,User>();
	public class User {
		public String id = null;
		public String pseudo = null;
		public String webcam = null;
		public String role = null;
		public String sex = null;
		public String onlineStatus = null;
		public String room=null;
		public String world=null;
		public String isWatching=null;
		public String hasWebcam=null;

		public User (String id,String  pseudo,String webcam, String role,String sex,String onlineStatus,String room,String world,String isWatching,String hasWebcam) {
		      this.id = id;
		      this.pseudo = pseudo;
		      this.webcam = webcam;
		      this.role = role;
		      this.sex = sex;
		      this.onlineStatus = "true";	
		      this.room = room;	
		      this.world = world;
		      this.isWatching = "false";	
		      this.world = world;		      
		   }
		} 
	
	   
	
	
	// ici le callback ! ! !
	/*if (conn instanceof IServiceCapableConnection) {
	    IServiceCapableConnection sc = (IServiceCapableConnection) conn;
	    sc.invoke("the_method", new Object[]{"One", 1}, new MyCallback());
	}
	sc.invoke("echo", new Object[]{"Hello world!"},
			 new IPendingServiceCallback() {
			 	public void resultReceived(IPendingServiceCall call) {
			 	System.err.println("Received: " + call.getResult());
			 	}
			  });
	*/

	
	class MyCallback implements IPendingServiceCallback {
	    public void resultReceived(IPendingServiceCall call) { 
	        // Do something with "call.getResult()"
	    }
	}
	//private ClientManager2 clientMgr = new ClientManager2("users_so","online",true);
	private ClientManager clientMgr = new ClientManager("users_so",true);	
	private static final Log log = LogFactory.getLog( Application.class );

	//ISharedObject users_so;
	//= getSharedObject(scope);

	//ArrayList listeUsers = new ArrayList();
	//ArrayList listeObjectUsers = new ArrayList();	
	
	/** Manager for the clients. */

	public void disconnectUser(String connectionID)
      {   //code to inform other clients that this client with this connection ID has left below...
      }
	/*
	 * (non-Javadoc)
	 * @see org.red5.server.adapter.ApplicationAdapter#connect(org.red5.server.api.IConnection, org.red5.server.api.IScope, java.lang.Object[])
	 * connect an users with 7 parameters: username,webcam,onlineStatus, role, room, world 
	 */
	public boolean connect(IConnection conn, IScope scope, Object[] params) {
		log.info("connect IConnection nombre params="+ params.length);
		// Check if the user passed valid parameters.
		//params = 	 pseudo onlineStatus role sexe  room world
		if (params.length==1) {			
			log.info("getNumberUsersConnected");
			((IServiceCapableConnection) conn).invoke("getNumberUsersConnected", new Object[]{users.size()});
			//rejectClient("getNumberUsersConnected !.");
			conn.close();
			return false;
		}
		if (params == null || params.length != 7) {
			log.info("Client must pass 7 params !");
			rejectClient("Client must pass 7 params !.");
			return false;
		}
		String id = conn.getClient().getId();
		String pseudo = params[0].toString();
		String webcam = params[1].toString();
		String onlineStatus = params[2].toString();
		String role=params[3].toString();
		String sex = params[4].toString();
		String room = params[5].toString();
		String world=params[6].toString();
		String isWatching="false";
		String hasWebcam="true";
//		 test if username is already used
		if (users.containsKey(pseudo)) {		     
		    	  rejectClient("Error: pseudo "+pseudo+" already used.");
		    	  return false;
		      }   		

		// Call original method of parent class.
		if (!super.connect(conn, scope, params)) {
			return false;
		}		
		log.info("ici2");
        users.put(pseudo,new User(id,pseudo,webcam, role,sex,onlineStatus,room,world,isWatching,hasWebcam));
        log.info("ici3");
        log.info("connected as :"+pseudo+"webcam="+webcam+" status="+onlineStatus+"role="+role+"sexe="+sex+"room="+room+"world="+world);
		clientMgr.addClient(scope, pseudo, id);
		ServiceUtils.invokeOnAllConnections (scope, "joinuser", new Object[] {id,pseudo,webcam, onlineStatus,role,sex,room,world} );
		return true;
	}
	/*
	 * (non-Javadoc)
	 * @see org.red5.server.adapter.ApplicationAdapter#disconnect(org.red5.server.api.IConnection, org.red5.server.api.IScope)
	 * disconnect an user form the chat and notify all others users 
	 */
	public void disconnect(IConnection conn, IScope scope) {       
		// Get the previously stored username.
		String id = conn.getClient().getId();			
		// Unregister user.
		String pseudo = clientMgr.removeClient(scope, id);
		User user=users.get(pseudo);
		users.remove(pseudo);
		log.info("removeuser invoked id:"+user.id+" pseudo="+user.pseudo+"sexe="+user.sex);
		ServiceUtils.invokeOnAllConnections (scope, "removeuser", new Object[] {user.id,user.pseudo,user.room,user.world} );
		super.disconnect(conn, scope);
		
	}	
	public boolean appJoin(IClient client, IScope scope) {
		log.info("Client joined app " + client.getId());
		// If you need the connecion object you can access it via.
		//IConnection conn = Red5.getConnectionLocal();
		return true;
	}
	public void send_to_all_iterate() {
		IConnection current = Red5.getConnectionLocal();
		Iterator<IConnection> it = scope.getConnections();
		
		while (it.hasNext()) {
		IConnection conn = it.next();
		if (conn.equals(current)) continue;
			if (conn instanceof IServiceCapableConnection) {
				((IServiceCapableConnection) conn).invoke("someClientMethod", new Object[]{"Hello World"});
				log.debug("sending notification to "+conn);
			}	
		}
	}	
	/*
	 * get all the already connected users
	 */
	public HashMap<String,User> getUserList() {
		/*IConnection conn = Red5.getConnectionLocal();		
		if (conn instanceof IServiceCapableConnection) {
			((IServiceCapableConnection) conn).invoke("receive_private", new Object[]{"zaza"});
			log.info("getUserList"+conn.getClient().getId());
		}*/
		//listeObjectUsers.toString()
		log.info("getUserList called");		
		return users;		
	}	
	/*
	 * send a public message msg from from_pseudo to ALL users
	 */
	public void send_public(String from_pseudo, String msg) {
		//Iterator<IConnection> it = scope.getConnections();
		log.info("send_public from_pseudo"+from_pseudo+" msg:"+msg);
		ServiceUtils.invokeOnAllConnections (scope, "receivePublicMsg", new Object[] {from_pseudo,msg} );
	}
	/*
	 * start Talk invoked on all users by whoTalks
	 */
	public void startTalk(String whoTalks) {
		log.info("startTalk: "+whoTalks);
		ServiceUtils.invokeOnAllConnections (scope, "talkStarted", new Object[] {whoTalks} );
	}
	/*
	 * stop Talk invoked on all users by whoTalks
	 */
	public void stopTalk(String whoTalks) {
		log.info("stopTalk: "+whoTalks);
		ServiceUtils.invokeOnAllConnections (scope, "talkEnded", new Object[] {whoTalks} );
	}
	/*
	 * gett the number of connected users to the chat
	 */
	public int getNumberUsersConnected(){
		return users.size();

	}
	/*
	 * start requestHand invoked on all users by whoRequests
	 */
	public void requestHand(String whoRequests) {
		log.info("send_public from_pseudo"+whoRequests);
		ServiceUtils.invokeOnAllConnections (scope, "handRequested", new Object[] {whoRequests} );
	}
	
	/*
	 * send a privateMessage msg from fromPseudo to sendToId user
	 */
	public void send_private_id(String sendToId,String fromPseudo, String msg) {
		//IConnection current = Red5.getConnectionLocal();
		Iterator<IConnection> it = scope.getConnections();
		log.debug("send_private to "+sendToId+" "+msg);
		//String uid = scope.getClient().getId();
		while (it.hasNext()) {
		IConnection conn = it.next();
		String id=conn.getClient().getId();
		if (!(sendToId.equals(id))) continue;
		log.info("receive_private "+sendToId+" "+msg);
			if (conn instanceof IServiceCapableConnection) {
				((IServiceCapableConnection) conn).invoke("receive_private", new Object[]{msg});
				log.info("received_private "+sendToId+" "+msg);
				return;
			}	
		}
	}	
	/*
	 * who is wathing who function
	 */
	public void watches(String UserId) {
        IConnection conn2 = Red5.getConnectionLocal();
	    String uid = conn2.getClient().getId();
	    
		Iterator<IConnection> it = scope.getConnections();
		log.debug("watches "+UserId);
		while (it.hasNext()) {
		IConnection conn = it.next();
		String id=conn.getClient().getId();
	
		if (!(UserId.equals(id))) continue;
			if (conn instanceof IServiceCapableConnection) {
				((IServiceCapableConnection) conn).invoke("watches", new Object[]{uid});
				log.info(uid+" is watching "+UserId);
				return;
			}	
		}
	}	
	/*
	 * ban the user _id
	 */
	public void ban(String _id) {
		Iterator<IConnection> it = scope.getConnections();
		//log.debug("ban called: "+_id);
		while (it.hasNext()) {
		IConnection conn = it.next();
		String id=conn.getClient().getId();
		if (!(_id.equals(id))) continue;
			if (conn instanceof IServiceCapableConnection) {
				((IServiceCapableConnection) conn).invoke("IhaveBeenBanned", new Object[]{_id});
				log.info("ban "+_id);
			}	
		}
	}			
	/*
	 * kick the user _id
	 */
	public void kick(String _id) {
		Iterator<IConnection> it = scope.getConnections();
		//log.debug("kick called: "+_id);
		while (it.hasNext()) {
		IConnection conn = it.next();
		String id=conn.getClient().getId();
		if (!(_id.equals(id))) continue;
			if (conn instanceof IServiceCapableConnection) {
				((IServiceCapableConnection) conn).invoke("IhaveBeenKicked", new Object[]{_id});
				log.info("kick "+_id);
			}	
		}
	}	
	/*
	 * send a private message msg from user fromPseudo to detsination user DestinationID
	 */
	public void send_private(String fromPseudo, String DestinationID,String msg) {
		//IConnection current = Red5.getConnectionLocal();
		Iterator<IConnection> it = scope.getConnections();
		log.debug("send_private to "+DestinationID+" "+msg);
		//String uid = scope.getClient().getId();
		while (it.hasNext()) {
		IConnection conn = it.next();
		String id=conn.getClient().getId();
		log.debug("id="+id+ " senTO="+DestinationID);
		//if (sendTo.equals(id)) log.info("PAREIL"); else log.info("differents");
		
		if (!(DestinationID.equals(id))) continue;
		log.info("receive_private "+DestinationID+" "+msg);
			if (conn instanceof IServiceCapableConnection) {
				((IServiceCapableConnection) conn).invoke("receivePrivateMsg", new Object[]{fromPseudo, msg});
				log.info("received_private "+DestinationID+" "+msg);
			}	
		}
	}	
	/*
	 * change the status of the webcam: can be "on" or "off"
	 */
	public void change_webcam(String webcam) {
		//
        IScope appScope = Red5.getConnectionLocal().getScope();
        IConnection conn = Red5.getConnectionLocal();
	    String uid = conn.getClient().getId();
	    log.debug("change_webcam called form id:"+uid+" :"+webcam);
        ServiceUtils.invokeOnAllConnections (appScope, "change_webcam", new Object[]{uid,webcam} );
	}
	/*
	 * change the status of the user to "status"
	 */
	//
	public void changeProfil(String webcam,String role, String onlineStatus) {
		//
        IScope appScope = Red5.getConnectionLocal().getScope();
        IConnection conn = Red5.getConnectionLocal();
	    String _id = conn.getClient().getId();
	    log.debug("changeProfil called form id:"+_id+" :"+webcam);
        ServiceUtils.invokeOnAllConnections (appScope, "changeProfil", new Object[]{_id,webcam,role,onlineStatus} );
       // return (new Object[]{uid,status});
	}

	public Double add(Double a, Double b){
        return a + b;
    }
	public Boolean appStart() {
		//users_so = SharedObject.get("users_so", false);
	     createSharedObject(Red5.getConnectionLocal().getScope(), "users_so", false);
	     log.debug("application START!");
		return true;
		
	}
	public boolean roomStart(IScope room) {
		log.info( "Red5First.room start " + room);
	      if (!super.roomStart(room))
	          return false;	      
	      //createSharedObject(room, "sampleSO", true);
	      //ISharedObject so = getSharedObject(room, "sampleSO");
	      // Now you could do something with the shared object...
	    
	      return true;            
	  }	
	public String whoami() {
	    IConnection conn = Red5.getConnectionLocal();
	    IClient client = conn.getClient();
	    IScope scope = conn.getScope();
	    return client.getId();
	    // ...
	}
	public void callclient() {
		log.info("callclient called");
		IConnection conn = Red5.getConnectionLocal();
		if (conn instanceof IServiceCapableConnection) {
		    IServiceCapableConnection sc = (IServiceCapableConnection) conn;
		    log.info("flashmethod called");
		    sc.invoke("flashmethod", new Object[]{"One", 1});
		}
	}
	public boolean appConnect( IConnection conn , Object[] params )
	{	
		//String id=conn.getClient().getId();
		//String username=(String)params[1];
		//String password=(String)params[2];
		//String sexe=(String)params[3];
		//String status=(String)params[4];
		// cherche si user d?j? dans la liste 
		//Cuser user=new Cuser(id,username,password,sexe,status);
		/*if (this.list[UserName]) {
			application.rejectConnection(newClient, {msg:"Nom d?j? utilis?, essayez de nouveau."});
			trace("### name taken");
			return;
		} 
		*/
		
		
	    log.info( "appConnect " + conn.getClient().getId() );
	    //boolean accept = (Boolean)params[0];
	    //String user=(String)params[1];
	   // if ( !accept ) rejectClient( "you passed false..." );
	    return true;
	}
	public void appDisconnect( IConnection conn){	    
		log.info( "Red5First.appDisconnect " + conn.getClient().getId() );
       // IScope appScope = Red5.getConnectionLocal().getScope();
       // ServiceUtils.invokeOnAllConnections (appScope, "removeuser", new Object[] { "zaza" } );
		
	}	
	public void appStop() {
		log.info( "Red5First.appStop" );

	}
}


I 'm not sure if the problem has to do with the SOs of the Application.java code.



1
Justin
Points: 600
Posts:0
1/8/2011 10:04:59 AM



hi george to create rooms you need to connect to a virtual folder in you main application.

for e.g

rtmp://myserver.com/myChatApp/room1
rtmp://myserver.com/myChatApp/room2

Above are two rooms created .All users connected to room1 will be in one room and all users connected to room2 will be in separate room .

2
george
Points: 0
Posts:0
1/8/2011 1:58:00 PM



Hi Justin. Thanks for your reply. I did what you suggested but nothing happened. I created two directories room1 and room2 to /usr/local/red5/webapps/chat which is my application directory (chat is the name of the application) but when I tried to connect by rtmp://myserver.com/chat/room1 i got the reject "NetConnection.Connect.Rejected".

I tried to change chat/WEB-INF/red5-web.properties from:
webapp.contextPath=/chat
to:
webapp.contextPath=/chat/room1
but even thought I got no result.

What can it be wrong?
but i got no result.

3
george
Points: 0
Posts:0
1/19/2011 11:00:46 AM



Hello to everyone. Finally i made it work with rooms. The main problems was at the server side java code. As Justin said if I wanted to create a new room I had to connect to a virtual folder to my main application.

for e.g
rtmp://myserver.com/chat/room1
rtmp://myserver.com/chat/room2
where chat is my application name.
room1 and room2 are virtual folders, no need to exists to the directory of my chat application.

To my server side code i had to override appConnect and appDisconnect instead of connect and disconnect methods.
I also had to replace line : ServiceUtils.invokeOnAllConnections (scope, myFunction, new Object[] {myParameters} );
by this line : ServiceUtils.invokeOnAllConnections (Red5.getConnectionLocal().getScope(), myFunction, new Object[] {myParameters} );

Some small other changes has to be done so the client get informed properly when a user disconnects from the server but that depends from the logic you'll choose to use.

4
Dimbic
Points: 0
Posts:0
1/20/2011 8:53:09 AM



george could you please publish or sent me your code?

5
george
Points: 0
Posts:0
1/21/2011 12:03:17 PM



Hi Dimbic.
What exactly wanna find out? Making original application work or creating rooms?

6
DimBiC
Points: 0
Posts:0
1/21/2011 12:51:42 PM



red5 chat already works but rooms doesn't work... if it is possible i want red5 server java code that implements rooms... Thanks in advance.

7
george
Points: 0
Posts:0
1/23/2011 2:41:26 PM



Hi DimBic. What's your mail? I'll send the code there.

8
DimBiC
Points: 0
Posts:0
1/24/2011 6:19:10 AM



dbilios@gmail.com Thanks.

9
Thierry
Points: 0
Posts:0
1/24/2011 12:05:31 PM



Hello , I installed Red5chat , all is ok on my server , I made some modifications for record to mysql , so all ok except the rooms , I did not find way to change room or create anothers ones ?
Could you please send me also yours explainations ?
Thank you
Thierry

10
george
Points: 0
Posts:0
Accepted Answer
1/25/2011 2:49:14 PM



Hi Thierry. What's your mail?
I don't publish the code cause i don't know if it's allowed by the GPL of the application to modify the code.

11
thierry
Points: 0
Posts:0
1/25/2011 3:30:11 PM



Hello George , thank you very much for answer my email is thierrysxm@thierrysxm.com or thierrysxm@wanadoo.fr

Thierry


12
mark hinnebusch
Points: 0
Posts:0
3/8/2011 9:58:45 AM



Hello, George. I also would like to see your code. my email is mark@bassanio.dsl.atlantic.net.

thanks,
mark

13
Aldo
Points: 0
Posts:0
4/13/2011 8:44:50 AM



Hello George,
Can you send to me too?
intelectus@gmail.com

Thanks in advance,

Aldo

14
TrumaN
Points: 0
Posts:0
4/14/2011 12:09:50 AM



Hi George,
I want to see your code too :)
maxpaynis@yandex.ru

Thanks

15
Stoev
Points: 0
Posts:0
5/21/2011 8:39:17 AM



Hi George,
could you help me to install red5 chat. I have experiance but not in off to deplace folder from original dovnloded zip file.

16
Stoev
Points: 0
Posts:0
5/21/2011 8:49:18 AM



my e-mail is
nortkele@abv.bg
Please helpq maybe i can help you with other

17
Amine
Points: 0
Posts:0
6/1/2011 7:26:01 AM



Hello George,
Can you send it to me too?
hedi.amine.souissi@bi-solutions.tn

Thanks in advance,

18
george
Points: 0
Posts:0
6/4/2011 5:26:06 AM



Hello to everyone and I'm sorry for my late reply. As I can see there are so many people out there with the same problem, so I post the code that allow you to create rooms by using rtmp://myserver.com/chat/room1, rtmp://myserver.com/chat/room1 etc.
I 'm newby with java so I think that the code could be much better then this mess, but at least it works fine. If anyone find any bug let us know so someone might solve it.


package chat;
import org.red5.server.adapter.ApplicationAdapter;
import org.red5.server.api.IConnection;
import org.red5.server.api.IClient;
import org.red5.server.api.IScope;
import org.red5.server.api.Red5;
import org.red5.server.api.service.IServiceCapableConnection;
import org.red5.server.api.so.ISharedObject;
import org.red5.server.api.ScopeUtils;
import org.red5.server.api.so.ISharedObjectService;
import org.red5.server.api.*;
import org.red5.server.api.scheduling.*;
import org.red5.samples.components.ClientManager;
import org.red5.server.api.service.ServiceUtils;
import org.red5.server.api.service.*;
import org.red5.server.api.service.IPendingServiceCallback;
//
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
//
import java.util.*;
import java.util.HashMap;
//
import java.io.*;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

public class Application extends ApplicationAdapter{	
	public HashMap<String,User> users = new HashMap<String,User>();
	public class User {
		public String id = null;
		public String pseudo = null;
		public String webcam = null;
		public String role = null;
		public String sex = null;
		public String onlineStatus = null;
		public String room=null;
		public String world=null;
		public String isWatching=null;
		public String hasWebcam=null;

		public User (String id,String  pseudo,String webcam, String role,String sex,String onlineStatus,String room,String world,String isWatching,String hasWebcam) {
			this.id = id;
			this.pseudo = pseudo;
			this.webcam = webcam;
			this.role = role;
			this.sex = sex;
			this.onlineStatus = "true";	
			this.room = room;	
			this.world = world;
			this.isWatching = "false";	
			this.world = world;		      
		}
	} 

	class MyCallback implements IPendingServiceCallback {
		public void resultReceived(IPendingServiceCall call) { 
			// Do something with "call.getResult()"
		}
	}
	
	private ClientManager clientMgr = new ClientManager("users_so",true);	
	private static final Log log = LogFactory.getLog( Application.class );
	
	/** Manager for the clients. */
	public void disconnectUser(String connectionID) {
		//code to inform other clients that this client with this connection ID has left below...
	}
	
	/** Does the same job as appDisconnect which is not called by nc.close but only on tab or browser close.
	      userDisconnect can be used from as2 just before nc.close*/
	public void userDisconnect() {
		IConnection conn = Red5.getConnectionLocal();
		IScope scope = Red5.getConnectionLocal().getScope();		
		// Get the previously stored username.
		String id = conn.getClient().getId();			
		// Unregister user.
		String pseudo = clientMgr.removeClient(scope, id);
		User user=users.get(pseudo);
		users.remove(pseudo);
		log.info("removeuser invoked id:"+user.id+" pseudo="+user.pseudo+"sexe="+user.sex);
		ServiceUtils.invokeOnAllConnections (Red5.getConnectionLocal().getScope(), "removeuser", new Object[] {user.id,user.pseudo,user.room,user.world} );
		super.disconnect(conn, scope);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.red5.server.adapter.ApplicationAdapter#connect(org.red5.server.api.IConnection, org.red5.server.api.IScope, java.lang.Object[])
	 * connect an users with 7 parameters: username,webcam,onlineStatus, role, room, world 
	 */
	public boolean appConnect(IConnection conn, Object[] params) {
IScope scope = Red5.getConnectionLocal().getScope();
		log.info("connect IConnection nombre params="+ params.length);
		// Check if the user passed valid parameters.
		//params =  pseudo onlineStatus role sexe room world
		if (params.length==1) {
			log.info("getNumberUsersConnected");
			((IServiceCapableConnection) conn).invoke("getNumberUsersConnected", new Object[]{Red5.getConnectionLocal().getScope().getClients().size()});
			//rejectClient("getNumberUsersConnected !.");
			conn.close();
			return false;
		}
		if (params == null || params.length != 7) {
			log.info("Client must pass 7 params !");
			rejectClient("Client must pass 7 params !.");
			return false;
		}
		String id = conn.getClient().getId();
		String pseudo = params[0].toString();
		String webcam = params[1].toString();
		String onlineStatus = params[2].toString();
		String role=params[3].toString();
		String sex = params[4].toString();
		String room = params[5].toString();
		String world=params[6].toString();
		String isWatching="false";
		String hasWebcam="true";
		//test if username is already used
//if (users.containsKey(pseudo)) {		     
//rejectClient("Error: pseudo "+pseudo+" already used.");
//return false;
//}   		
		Set<IClient> clients = Red5.getConnectionLocal().getScope().getClients();
		for(IClient c:clients) {
			if (c.getAttribute("pseudo").toString().equals(pseudo)) {
				rejectClient("Error: pseudo "+pseudo+" already used.");
				return false;
			}
		}
		
		// Call original method of parent class.
		if (!super.connect(conn, scope, params)) {
			return false;
		}		
		log.info("ici2");
		Red5.getConnectionLocal().getClient().setAttribute("pseudo",pseudo);
		Red5.getConnectionLocal().getClient().setAttribute("user",new User(id,pseudo,webcam, role,sex,onlineStatus,room,world,isWatching,hasWebcam));
		users.put(pseudo,new User(id,pseudo,webcam, role,sex,onlineStatus,room,world,isWatching,hasWebcam));
		log.info("ici3");
		log.info("connected as :"+pseudo+"webcam="+webcam+" status="+onlineStatus+"role="+role+"sexe="+sex+"room="+room+"world="+world);
		clientMgr.addClient(scope, pseudo, id);
		ServiceUtils.invokeOnAllConnections (Red5.getConnectionLocal().getScope(), "joinuser", new Object[] {id,pseudo,webcam, onlineStatus,role,sex,room,world} );
		return true;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.red5.server.adapter.ApplicationAdapter#disconnect(org.red5.server.api.IConnection, org.red5.server.api.IScope)
	 * disconnect an user form the chat and notify all others users 
	 */
	public void appDisconnect(IConnection conn) {     
		IScope scope = Red5.getConnectionLocal().getScope();		
		// Get the previously stored username.
		String id = conn.getClient().getId();			
		// Unregister user.
		String pseudo = clientMgr.removeClient(scope, id);
		User user=users.get(pseudo);
		users.remove(pseudo);
		log.info("removeuser invoked id:"+user.id+" pseudo="+user.pseudo+"sexe="+user.sex);
		ServiceUtils.invokeOnAllConnections (Red5.getConnectionLocal().getScope(), "removeuser", new Object[] {user.id,user.pseudo,user.room,user.world} );
		super.disconnect(conn, scope);
	}
	
	public boolean appJoin(IClient client, IScope scope) {
		log.info("Client joined app " + client.getId());
		// If you need the connecion object you can access it via.
		//IConnection conn = Red5.getConnectionLocal();
		return true;
	}

	public Boolean appStart() {
		//users_so = SharedObject.get("users_so", false);
		createSharedObject(Red5.getConnectionLocal().getScope(), "users_so", false);
		log.debug("application START!");
		return true;
	}
	
	public boolean roomStart(IScope room) {
		log.info( "Red5First.room start " + room);
		if (!super.roomStart(room)) 	return false;
		//createSharedObject(room, "sampleSO", true);
		//ISharedObject so = getSharedObject(room, "sampleSO");
		// Now you could do something with the shared object...
		return true;            
	}	
	
	public void appStop() {
		log.info( "Red5First.appStop" );
	}
	
	/*
	 * gett the number of connected users to the chat
	 */
	public int getNumberUsersConnected() {
		return Red5.getConnectionLocal().getScope().getClients().size();
	}
	
	/*
	 * get all the already connected users
	 */
	public HashMap<String,User> getUserList() {
		log.info("getUserList called");	
		HashMap<String,User> userslist = new HashMap<String,User>();
		Set<IClient> clients = Red5.getConnectionLocal().getScope().getClients();
		for(IClient c:clients) {
			userslist.put(c.getAttribute("pseudo").toString(),(User) c.getAttribute("user"));
		}
		return userslist;
	}

	public void send_to_all_iterate() {
		IConnection current = Red5.getConnectionLocal();
		Iterator<IConnection> it = scope.getConnections();
		while (it.hasNext()) {
		IConnection conn = it.next();
		if (conn.equals(current)) continue;
			if (conn instanceof IServiceCapableConnection) {
				((IServiceCapableConnection) conn).invoke("someClientMethod", new Object[]{"Hello World"});
				log.debug("sending notification to "+conn);
			}	
		}
	}
	
	/*
	 * send a public message msg from from_pseudo to ALL users
	 */
	public void send_public(String from_pseudo, String msg) {
		//Iterator<IConnection> it = scope.getConnections();
		log.info("send_public from_pseudo"+from_pseudo+" msg:"+msg);
		ServiceUtils.invokeOnAllConnections (Red5.getConnectionLocal().getScope(), "receivePublicMsg", new Object[] {from_pseudo,msg} );
	}
	
	/*
	 * start Talk invoked on all users by whoTalks
	 */
	public void startTalk(String whoTalks) {
		log.info("startTalk: "+whoTalks);
		ServiceUtils.invokeOnAllConnections (Red5.getConnectionLocal().getScope(), "talkStarted", new Object[] {whoTalks} );
	}
	
	/*
	 * stop Talk invoked on all users by whoTalks
	 */
	public void stopTalk(String whoTalks) {
		log.info("stopTalk: "+whoTalks);
		ServiceUtils.invokeOnAllConnections (Red5.getConnectionLocal().getScope(), "talkEnded", new Object[] {whoTalks} );
	}
	
	/*
	 * start requestHand invoked on all users by whoRequests
	 */
	public void requestHand(String whoRequests) {
		log.info("send_public from_pseudo"+whoRequests);
		ServiceUtils.invokeOnAllConnections (Red5.getConnectionLocal().getScope(), "handRequested", new Object[] {whoRequests} );
	}
	
	/*
	 * send a privateMessage msg from fromPseudo to sendToId user
	 */
	public void send_private_id(String sendToId,String fromPseudo, String msg) {
		//IConnection current = Red5.getConnectionLocal();
		Iterator<IConnection> it = scope.getConnections();
		log.debug("send_private to "+sendToId+" "+msg);
		//String uid = scope.getClient().getId();
		while (it.hasNext()) {
		IConnection conn = it.next();
		String id=conn.getClient().getId();
		if (!(sendToId.equals(id))) continue;
		log.info("receive_private "+sendToId+" "+msg);
			if (conn instanceof IServiceCapableConnection) {
				((IServiceCapableConnection) conn).invoke("receive_private", new Object[]{msg});
				log.info("received_private "+sendToId+" "+msg);
				return;
			}	
		}
	}
	
	/*
	 * who is wathing who function
	 */
	public void watches(String UserId) {
		IConnection conn2 = Red5.getConnectionLocal();
		String uid = conn2.getClient().getId();
		Iterator<IConnection> it = scope.getConnections();
		log.debug("watches "+UserId);
		while (it.hasNext()) {
		IConnection conn = it.next();
		String id=conn.getClient().getId();
		if (!(UserId.equals(id))) continue;
			if (conn instanceof IServiceCapableConnection) {
				((IServiceCapableConnection) conn).invoke("watches", new Object[]{uid});
				log.info(uid+" is watching "+UserId);
				return;
			}	
		}
	}
	
	/*
	 * ban the user _id
	 */
	public void ban(String _id) {
		Iterator<IConnection> it = scope.getConnections();
		//log.debug("ban called: "+_id);
		while (it.hasNext()) {
		IConnection conn = it.next();
		String id=conn.getClient().getId();
		if (!(_id.equals(id))) continue;
			if (conn instanceof IServiceCapableConnection) {
				((IServiceCapableConnection) conn).invoke("IhaveBeenBanned", new Object[]{_id});
				log.info("ban "+_id);
			}	
		}
	}
	
	/*
	 * kick the user _id
	 */
	public void kick(String _id) {
		Iterator<IConnection> it = scope.getConnections();
		//log.debug("kick called: "+_id);
		while (it.hasNext()) {
		IConnection conn = it.next();
		String id=conn.getClient().getId();
		if (!(_id.equals(id))) continue;
			if (conn instanceof IServiceCapableConnection) {
				((IServiceCapableConnection) conn).invoke("IhaveBeenKicked", new Object[]{_id});
				log.info("kick "+_id);
			}	
		}
	}
	
	/*
	 * send a private message msg from user fromPseudo to detsination user DestinationID
	 */
	public void send_private(String fromPseudo, String DestinationID,String msg) {
		//IConnection current = Red5.getConnectionLocal();
		Iterator<IConnection> it = scope.getConnections();
		log.debug("send_private to "+DestinationID+" "+msg);
		//String uid = scope.getClient().getId();
		while (it.hasNext()) {
			IConnection conn = it.next();
			String id=conn.getClient().getId();
			log.debug("id="+id+ " senTO="+DestinationID);
			//if (sendTo.equals(id)) log.info("PAREIL"); else log.info("differents");
			if (!(DestinationID.equals(id))) continue;
			log.info("receive_private "+DestinationID+" "+msg);
			if (conn instanceof IServiceCapableConnection) {
				((IServiceCapableConnection) conn).invoke("receivePrivateMsg", new Object[]{fromPseudo, msg});
				log.info("received_private "+DestinationID+" "+msg);
			}	
		}
	}
	
	/*
	 * change the status of the webcam: can be "on" or "off"
	 */
	public void change_webcam(String webcam) {
		IScope appScope = Red5.getConnectionLocal().getScope();
		IConnection conn = Red5.getConnectionLocal();
		String uid = conn.getClient().getId();
		log.debug("change_webcam called form id:"+uid+" :"+webcam);
		ServiceUtils.invokeOnAllConnections (appScope, "change_webcam", new Object[]{uid,webcam} );
	}
	
	/*
	 * change the status of the user to "status"
	 */
	//
	public void changeProfil(String webcam,String role, String onlineStatus) {
		IScope appScope = Red5.getConnectionLocal().getScope();
		IConnection conn = Red5.getConnectionLocal();
		String _id = conn.getClient().getId();
		log.debug("changeProfil called form id:"+_id+" :"+webcam);
		ServiceUtils.invokeOnAllConnections (appScope, "changeProfil", new Object[]{_id,webcam,role,onlineStatus} );
		// return (new Object[]{uid,status});
	}

	public Double add(Double a, Double b){
		return a + b;
	}
	  
	public String whoami() {
		IConnection conn = Red5.getConnectionLocal();
		IClient client = conn.getClient();
		IScope scope = conn.getScope();
		return client.getId();
		// ...
	}
	
	public void callclient() {
		log.info("callclient called");
		IConnection conn = Red5.getConnectionLocal();
		if (conn instanceof IServiceCapableConnection) {
			IServiceCapableConnection sc = (IServiceCapableConnection) conn;
			log.info("flashmethod called");
			sc.invoke("flashmethod", new Object[]{"One", 1});
		}
	}
	
}

19
zema
Points: 0
Posts:0
6/10/2011 2:36:47 AM



Hi George and thx for the code.
Would you be so kind to point what is changed in the code?
I mean relevant lines.
I would like to test the code, but if I just paste it inside text editor it adds lot of spaces and it seems not to work.
So, I thought I could just change relevant lines, keeping the rest of the original code.

Thx in advance
Zema

20
george
Points: 0
Posts:0
6/25/2011 9:01:44 AM



Hi Zema. I 'm sorry but it's been a long time since I changed this code so I don't remember what I really changed.
I suggest you download a program like ExamDiff that helps you compare two text files.
Have in mind, also that this code will work only with version 0.7.0 of Red5 server and not any other version above this.

21
marcel
Points: 0
Posts:0
9/16/2011 5:20:10 AM



hi

is there some one who can help me with the splash screen/intro? is there a way to disable this en connect to the default or other room, so we don't see a splash intro at the page? can some help me withthat?

thanks a alot

kind regards

marcel

22
Victor
Points: 0
Posts:0
1/27/2012 12:56:04 AM



George, thank you very much for sharing the code, it helped us a lot!

23
ktg
Points: 0
Posts:0
2/22/2013 9:36:58 AM



Hi, George
Your code is very helpful.
Thank you.


Post your Reply
Name  

Email

Type your Reply or Answer

Are you human? What is 1+15+8 



Members Login

Email  
Password
Forgot Password





This website focus on: Flash | Flex | FMS | RED5 | WOWZA | Flash Media Server | Adobe AIR | ActionScript,Flash Solutions | Flash Question | Flash Answers | Flash Developers | Flash Problem, Flash Help, Flash bugs, Flash workaround | Flash Blog | Flex Question Answers | Flash Forum | Flex Development | Actionscript development | Flash development | Adobe AIR development
Copyright © 2008 AskMeFlash.com. All rights reserved. Privacy Policy | Terms & Conditions