码迷,mamicode.com
首页 > 其他好文 > 详细

读pomelo的教程-2

时间:2014-08-10 23:49:30      阅读:939      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   for   ar   cti   

下面从头到尾记录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

读pomelo的教程-2,布布扣,bubuko.com

读pomelo的教程-2

标签:style   blog   color   os   io   for   ar   cti   

原文地址:http://www.cnblogs.com/redclock/p/3903391.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!