
	exports.Create = function (app){
		return new Mod( app );
	}
	
	function Mod (app){
		this.app_ = app;
		this.uinMap_ = {};
		this.idMap_ = {};
		
		this.scope_ = "demo";
		this.id_ = 1;
	}
	
	Mod.prototype.OnClientIn = function ( id, addr ){
		//console.log("a new connection in",id,addr);
		this.idMap_[id] = { addr:addr,nick:"游客" + this.id_ ,uin:this.id_};
		this.id_ ++;
		this.AddScope( id,this.scope_);
		this.SendUserNum();
		var pkt = {
			cmd:"login",
			uin:this.idMap_[id].uin,
			nick:this.idMap_[id].nick,
		}
		this.SendToClient( id,pkt);
	}
	
	Mod.prototype.OnClientData = function ( id, data ){
		//console.log("recieve data from client",id,data);
		if ( typeof(data) != "object" )
			return ;
		
		if( data.cmd == "chatText" ){
			data.nick = this.idMap_[id].nick;
			data.uin = this.idMap_[id].uin;
			return this.SendByScope( this.scope_,data);
		}else if( data.cmd == "modNick" ){
			this.idMap_[id].nick = data.nick;
			return this.SendToClient( id,data);
		}
		
			
	}
	
	Mod.prototype.OnClientOut = function ( id  ){
		//console.log("client login out",id);
		delete this.idMap_[id];
		this.DelScope( id,this.scope_ );
		this.SendUserNum();
	}
		
	Mod.prototype.OnOhterAppData = function ( id, addr ){
		
	}
	
	Mod.prototype.SendToClient = function (id,data){		
		this.app_.SendToClient(id,data);
	}
	
	Mod.prototype.CloseClient = function (id){
		this.app_.CloseClient(id);
	}
	Mod.prototype.AddScope = function (id,scope){
		this.app_.AddScope(scope,id);
	}
	Mod.prototype.DelScope = function (id,scope){
		this.app_.DelScope(scope,id);
	}
	Mod.prototype.SendByScope = function (scope,data){
		this.app_.SendToScope(scope,data);
	}
	
	Mod.prototype.SendUserNum = function (){
		var num = 0;
		for( var n in this.idMap_ )
			num ++;
		var pkt = {
			cmd:"userNum",
			num:num,
		}
		return this.SendByScope( this.scope_,pkt );
	}