Created
November 15, 2012 09:37
-
-
Save xiaojue/4077673 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(function(require, exports, module) { | |
//web chat基础类,不负责业务层和UI | |
var $ = require('jquery'), | |
Events = require('events'), | |
chatlist = require('mods/chat/chat-list'), | |
chatwindow = require('mods/chat/chat-window'); | |
require('vendor/jquery.comet'); | |
var chat = function(config) { | |
var _config = { | |
//html target | |
target:'#chat', | |
//获取链接url | |
connect_url: 'http://nas.uc.sina.com/webroom', | |
//客户端发送信息url | |
channel_req: "/im/req", | |
//房间ID,usercode等页面信息,需要传入 | |
roomid: null, | |
usercode: null, | |
//error 为0时被T出,出错时-1 | |
limiterror: 5 | |
}; | |
$.extend(_config, config); | |
this.config = _config; | |
this.user = { | |
//身份 | |
owner: null, | |
//昵称 | |
nick: null, | |
//用户名-pid | |
username: null, | |
//用户进入时的权限范围 | |
auth: null, | |
//进入房间的时间 | |
jointime: null | |
}; | |
this.server = { | |
//server ip 需要cometd init | |
server: null, | |
//服务端返回数据的channel地址 | |
channel: null, | |
//当前server所需的ukey | |
ukey: null | |
}; | |
//是否已经嵌入聊天室 = 嵌入后才能初始化其他UI组件 | |
this.comet = $.comet; | |
this.embed = false; | |
}; | |
//继承窗口和右侧的方法 | |
$.extend(chat.prototype, (new chatlist())); | |
$.extend(chat.prototype, (new chatwindow())); | |
//主方法 | |
$.extend(chat.prototype, { | |
constructor: chat, | |
//握手取得通道 | |
_handshake: function(cb) { | |
var self = this, | |
cg = self.config; | |
$.ajax({ | |
type: 'get', | |
url: cg.connect_url, | |
dataType: 'jsonp', | |
data: { | |
roomid: cg.roomid, | |
uid: cg.usercode | |
}, | |
success: function(result) { | |
//jsonp返回后混淆sever | |
$.extend(self.server, result); | |
if ($.isFunction(cb)) cb(); | |
}, | |
error: function() { | |
self.trigger('handshake_timeout'); | |
} | |
}); | |
}, | |
//建立链接 | |
_connect: function(cb) { | |
var self = this, | |
cg = self.config; | |
//出错的handle | |
this.comet._oTransport._cancelConnect = function(oMsg) { | |
//如果失败了,开始计数错误次数,连续超过5次断开,服务器返回不重连断开 | |
self.config.limiterror--; | |
if (self.config.limiterror <= 0) self.leave(); | |
else console.warn('链接出错'); | |
}; | |
this.comet.init(this.server.server); | |
this.send({ | |
cmd: 'authuser', | |
uid: cg.usercode | |
}); | |
this.comet.subscribe(this.server.channel, function(oMsg) { | |
self._receive(oMsg); | |
//重置错误次数 | |
self.config.limiterror = 5; | |
}); | |
this.on('vcard',function(data){ | |
delete data['type']; | |
$.extend(self.user,data); | |
}); | |
this.send({cmd:'vcard'}); | |
if ($.isFunction(cb)) cb(); | |
}, | |
//回调自定义事件 | |
_receive: function(oMsg) { | |
var type = oMsg.data.type; | |
this.trigger(type, oMsg.data); | |
}, | |
//发布消息 | |
send: function(data) { | |
this.comet.publish(this.config.channel_req, data); | |
}, | |
//离开或被T出 | |
leave: function(cb) { | |
this.comet.startBatch(); | |
this.comet.unsubscribe(this.server.channel); | |
this.comet.disconnect(); | |
this.comet.endBatch(); | |
if($.isFunction(cb)) cb(); | |
}, | |
//初始化chat | |
init: function(cb) { | |
//握手通讯 | |
var self = this; | |
//onload 之后加载 可不出现loading条 | |
$(function() { | |
self._handshake(function() { | |
//握手成功的话,链接comet,否则自定义timeout事件回调处理UI | |
self._connect(cb); | |
}); | |
}); | |
} | |
}); | |
//使用自定义事件解耦并对外提供API | |
Events.mixTo(chat); | |
module.exports = chat; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment