客户端
1: $("#entry").keypress(function (e) {
2:var route = "chat.chatHandler.send";
3:var target = $("#usersList").val();
4:if (e.keyCode != 13 /* Return */) return;
5:var msg = $("#entry").attr("value").replace("\n","");
6:if (!util.isBlank(msg)) {
7: pomelo.request(route,{//route = "chat.chatHandler.send"
8: rid: rid,
9: content: msg,
10: from: username,monospace; overflow:auto; width:702px; border-style:none"> 11: target: target
12: },255)">function (data) {
13: $("value",128)">""); // clear the entry field.
14:if (target != '*' && target != username) {
15: addMessage(username,target,msg);
16: $("#chatHistory").show();
17: }
18: });
19: }
20: });
1: handler.send = function(msg,session,next) {
var rid = session.get('rid');
var username = session.uid.split('*')[0];
var channelService = this.app.get('channelService');
var param = {
6: route: 'onChat',96)"> 7: msg: msg.content,96)"> 8: from: username,96)"> 9: target: msg.target
10: };
11: channel = channelService.getChannel(rid,255)">false);
12:
13://the target is all users
if(msg.target == '*') {
15: channel.pushMessage(param);
16: }
17://the target is specific user
18:else {
19:var tuid = msg.target + '*' + rid;
20:var tsid = channel.getMember(tuid)['sid'];
21: channelService.pushMessageByUids(param,[{
22: uid: tuid,monospace; overflow:auto; width:702px; border-style:none"> 23: sid: tsid
24: }]);
25: }
26: next(null,{
27: route: msg.route
28: });
29: };
1: channel = channelService.getChannel(rid,false);
1: app.configure('production|development',255); font-size:14px">function () {
2:// route configures
3: app.route('chat',routeUtil.chat);//routes的chat属性对应routeUtil.chat()方法
4: app.filter(pomelo.timeout());
5: });