Redclock's Blog

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

下面从头到尾记录chat demo的Login的过程

      1. client:点击login按钮,取得username和rid两个值
        $("#login").click(function() {
                username = $("#loginUser").attr("value");
                rid = $('#channelList').val();

        之后对username和rid验证

      2. client:连接gate服务器
        pomelo.init({
                        host: host,
                        port: port,
                        log: true
                    }

        请求路由 'gate.gateHandler.queryEntry'。

        pomelo.request(route, {
                    uid: uid
                }

         

      3. server:gate服务器
        handler.queryEntry = function(msg, session, next) {
            var uid = msg.uid;
            if(!uid) {
                next(null, {
                    code: 500
                });
                return;
            }
            // get all connectors
            var connectors = this.app.getServersByType('connector');
            if(!connectors || connectors.length === 0) {
                next(null, {
                    code: 500
                });
                return;
            }
            // select connector
            var res = dispatcher.dispatch(uid, connectors);
            next(null, {
                code: 200,
                host: res.host,
                port: res.clientPort
            });
        };

        主要就是分配一个connector服务器,返回它的host和port

      4. client:连接connector服务器
        var route = "connector.entryHandler.enter";
                        pomelo.request(route, {
                            username: username,
                            rid: rid
                        }

         

      5. server:connector服务器
        handler.enter = function(msg, session, next) {
            var self = this;
            var rid = msg.rid;
            var uid = msg.username + '*' + rid
            var sessionService = self.app.get('sessionService');
        
            console.log("rid="+rid +" uid="+uid);
            //duplicate log in
            if( !! sessionService.getByUid(uid)) {
                next(null, {
                    code: 500,
                    error: true
                });
                return;
            }
        
            session.bind(uid);
            session.set('rid', rid);
            session.push('rid', function(err) {
                if(err) {
                    console.error('set rid for session service failed! error is : %j', err.stack);
                }
            });
            session.on('closed', onUserLeave.bind(null, self.app));
        
            //put user into channel
            self.app.rpc.chat.chatRemote.add(session, uid, self.app.get('serverId'), rid, true, function(users){
                next(null, {
                    users:users
                });
            });
        };

        检测一下是否重复登陆了,把session与uid绑定,在setting里设置rid,注意需要push,session设置回调“closed”,rpc调用chat服务器

      6. server:chat服务器添加用户
        ChatRemote.prototype.add = function(uid, sid, name, flag, cb) {
            var channel = this.channelService.getChannel(name, flag);
            var username = uid.split('*')[0];
            var param = {
                route: 'onAdd',
                user: username
            };
            channel.pushMessage(param);
        
            if( !! channel) {
                channel.add(uid, sid);
            }
        
            cb(this.get(name, flag));
        };

        这里的uid是“uid*rid”, sid是server id,name是rid,flag是true。向channel中所有用户发送onaAdd信息,再把uid加入channel

posted on 2014-08-10 21:08  Redclock  阅读(1645)  评论(0编辑  收藏  举报