<script type="text/javascript">
window.contextPath = "<%=path%>";
window["serverDomin"] = "192.168.8.22";
</script>
$.WebIM方法是主函数,用它可以覆盖local.chat中的基本配置,它可以完成聊天窗口的创建。$.WebIM.newWebIM方法是新创建一个窗口,只是消息的接收者是一个新用户。
复制代码 代码如下:
$.WebIM({
sender: userName,
receiver: receiver
});
$.WebIM.newWebIM({
receiver: receiver
});
remote.jsjac.chat.login(document.userForm);方法是用户登录到Openfire服务器
参数如下:
httpbase: window.contextPath + "/JHB/", //请求后台http-bind服务器url
domain: window["serverDomin"], //"192.168.5.231", // 192.168.5.231 当前有效域名
username: "", // 登录用户名
pass: "", // 密码
timerval: 2000, // 设置请求超时
resource: "WebIM", // 链接资源标识
register: true // 是否注册
remote.jsjac.chat.logout();是退出、断开openfire的链接
2、本地聊天应用核心代码 local.chat-2.0.js
复制代码 代码如下:
/***
* jquery local chat
* @version v2.0
* @createDate -- 2012-5-28
* @author hoojo
* @email hoojo_@126.com
* @blog &
* @requires jQuery v1.2.3 or later, send.message.editor-1.0.js
* Copyright (c) 2012 M. hoo
**/
;(function ($) {
if (/1\.(0|1|2)\.(0|1|2)/.test($.fn.jquery) || /^1.1/.test($.fn.jquery)) {
alert('WebIM requires jQuery v1.2.3 or later! You are using v' + $.fn.jquery);
return;
}
var faceTimed, count = 0;
var _opts = defaultOptions = {
version: 2.0,
chat: "#chat",
chatEl: function () {
var $chat = _opts.chat;
if ((typeof _opts.chat) == "string") {
$chat = $(_opts.chat);
} else if ((typeof _opts.chat) == "object") {
if (!$chat.get(0)) {
$chat = $($chat);
}
}
return $chat;
},
sendMessageIFrame: function (receiverId) {
return $("iframe[name='sendMessage" + receiverId + "']").get(0).contentWindow;
},
receiveMessageDoc: function (receiverId) {
receiverId = receiverId || "";
var docs = [];
$.each($("iframe[name^='receiveMessage" + receiverId + "']"), function () {
docs.push($(this.contentWindow.document));
});
return docs;
//return $($("iframe[name^='receiveMessage" + receiverId + "']").get(0).contentWindow.document);
},
sender: "", // 发送者
receiver: "", // 接收者
setTitle: function (chatEl) {
var receiver = this.getReceiver(chatEl);
chatEl.find(".title").html("和" + receiver + "聊天对话中");
},
getReceiver: function (chatEl) {
var receiver = chatEl.attr("receiver");
if (~receiver.indexOf("@")) {
receiver = receiver.split("@")[0];
}
return receiver;
},
// 接收消息iframe样式
receiveStyle: [
'<html>',
'<head><style type="text/css">',
'body{border:0;margin:0;padding:3px;height:98%;cursor:text;background-color:white;font-size:12px;font-family:Courier,serif,monospace;}',
'.msg{margin-left: 1em;}p{margin:0;padding:0;}.me{color: blue;}.you{color:green;}',
'</style></head>',
'<body></body>',
'</html>'
].join(""),
writeReceiveStyle: function (receiverId) {
this.receiveMessageDoc(receiverId)[0].get(0).write(this.receiveStyle);
},
datetimeFormat: function (v) {
if (~~v < 10) {
return "0" + v;
}
return v;
},
getDatetime: function () {
// 设置当前发送日前
var date = new Date();
var datetime = date.getFullYear() + "-" + date.getMonth() + "-" + date.getDate();
datetime = " " + _opts.datetimeFormat(date.getHours())
+ ":" + _opts.datetimeFormat(date.getMinutes())
+ ":" + _opts.datetimeFormat(date.getSeconds());
return datetime;
},
/***
* 发送消息的格式模板
* flag = true 表示当前user是自己,否则就是对方
**/
receiveMessageTpl: function (userName, styleTpl, content, flag) {
var userCls = flag ? "me" : "you";
if (styleTpl && flag) {
content = [ "<span>", content, "</span>" ].join("");
}
return [
'<p>', _opts.getDatetime(), ' ', userName, ':</p>',
'<p>', content, '</p>'
].join("");
},
// 工具类按钮触发事件返回html模板
sendMessageStyle: {
cssStyle: {
bold: "font-weight: bold;",
underline: "text-decoration: underline;",
italic: "font-style: oblique;"
},
setStyle: function (style, val) {
if (val) {
_opts.sendMessageStyle[style] = val;
} else {
var styleVal = _opts.sendMessageStyle[style];
if (styleVal === undefined || !styleVal) {
_opts.sendMessageStyle[style] = true;
} else {
_opts.sendMessageStyle[style] = false;
}
}
},
getStyleTpl: function () {
var tpl = "";
$.each(_opts.sendMessageStyle, function (style, item) {
//alert(style + "#" + item + "#" + (typeof item));
if (item === true) {
tpl += _opts.sendMessageStyle.cssStyle[style];
} else if ((typeof item) === "string") {
//alert(style + "-------------" + sendMessageStyle[style]);
tpl += style + ":" + item + ";";
}
});
return tpl;
}
},
// 向接收消息iframe区域写消息
writeReceiveMessage: function (receiverId, userName, content, flag) {
if (content) {
// 发送消息的样式
var styleTpl = _opts.sendMessageStyle.getStyleTpl();
var receiveMessageDoc = _opts.receiveMessageDoc(receiverId);
$.each(receiveMessageDoc, function () {
var $body = this.find("body");
// 向接收信息区域写入发送的数据
$body.append(_opts.receiveMessageTpl(userName, styleTpl, content, flag));
// 滚动条滚到底部
this.scrollTop(this.height());
});
}
},
// 发送消息
sendHandler: function ($chatMain) {
var doc = $chatMain.find("iframe[name^='sendMessage']").get(0).contentWindow.document;
var content = doc.body.innerHTML;
content = $.trim(content);
content = content.replace(new RegExp("<br>", "gm"), "");
// 获取即将发送的内容
if (content) {
var sender = $chatMain.attr("sender");
var receiverId = $chatMain.attr("id");
// 接收区域写消息
_opts.writeReceiveMessage(receiverId, sender, content, true);
//############# XXX
var receiver = $chatMain.find("#to").val();
//var receiver = $chatMain.attr("receiver");
// 判断是否是手机端会话,如果是就发送纯text,否则就发送html代码
var flag = _opts.isMobileClient(receiver);
if (flag) {
var text = $(doc.body).text();
text = $.trim(text);
if (text) {
// 远程发送消息
remote.jsjac.chat.sendMessage(text, receiver);
}
} else { // 非手机端通信 可以发送html代码
var styleTpl = _opts.sendMessageStyle.getStyleTpl();
content = [ "<span>", content, "</span>" ].join("");
remote.jsjac.chat.sendMessage(content, receiver);
}
// 清空发送区域
$(doc).find("body").html("");
}
},
faceImagePath: "images/emotions/",
faceElTpl: function (i) {
return [
"<img src='",
this.faceImagePath,
(i - 1),
"fixed.bmp' gif='",
this.faceImagePath,
(i - 1),
".gif'/>"
].join("");
},
// 创建表情html elements
createFaceElement: function ($chat) {
var faces = [];
for (var i = 1; i < 100; i++) {
faces.push(this.faceElTpl(i));
if (i % 11 == 0) {
faces.push("<br/>");
}
}
$chat.find("#face").html(faces.join(""));
this.faceHandler($chat);
},
// 插入表情
faceHandler: function ($chat) {
$chat.find("#face img").click(function () {
$chat.find("#face").hide(150);
var imgEL = "<img src='" + $(this).attr("gif") + "'/>";
var $chatMain = $(this).parents(".chat-main");
var win = $chatMain.find("iframe[name^='sendMessage']").get(0).contentWindow;
var doc = win.document;
sendMessageEditor.insertAtCursor(imgEL, doc, win);
});
// 表情隐藏
$chat.find("#face, #face img").mouseover(function () {
window.clearTimeout(faceTimed);
}).mouseout(function () {
window.clearTimeout(faceTimed);
faceTimed = window.setTimeout(function () {
$chat.find("#face").hide(150);
}, 700);
});
},
/***
* 发送消息工具栏按钮事件方法
**/
toolBarHandler: function () {
var $chat = $(this).parents(".chat-main");
var targetCls = $(this).attr("class");
if (targetCls == "face") {
$chat.find("#face").show(150);
window.clearTimeout(faceTimed);
faceTimed = window.setTimeout(function () {
$chat.find("#face").hide(150);
}, 1000);
} else if (this.tagName == "DIV") {
_opts.sendMessageStyle.setStyle(targetCls);
} else if (this.tagName == "SELECT") {
_opts.sendMessageStyle.setStyle($(this).attr("name"), $(this).val());
if ($(this).attr("name") == "color") {
$(this).css("background-color", $(this).val());
}
}
// 设置sendMessage iframe的style css
_opts.writeSendStyle();
},
// 设置sendMessage iframe的style css
writeSendStyle: function () {
var styleTpl = _opts.sendMessageStyle.getStyleTpl();
var styleEL = ['<style type="text/css">body{', styleTpl,'}</style>'].join("");
$("body").find("iframe[name^='sendMessage']").each(function () {
var $head = $(this.contentWindow.document).find("head");
if ($head.find("style").size() > 1) {
$head.find("style:gt(0)").remove();
}
if (styleTpl) {
$head.append(styleEL);
}
});
},
isMobileClient: function (receiver) {
var moblieClients = ["iphone", "ipad", "ipod", "wp7", "android", "blackberry", "Spark", "warning", "symbian"];
var flag = false;
for (var i in moblieClients) {
if (~receiver.indexOf(moblieClients[i])) {
return true;
}
}
return false;
},
// 聊天界面html元素
chatLayoutTemplate: function (userJID, sender, receiver, product, flag) {
var display = "";
if (flag) {
display = "style='display: none;'";
}
return [
'<div sender="', sender, '" receiver="', receiver, '">',
'<div><div>',
'<table>',
'<tr>',
'<td colspan="3"></td>',
'</tr>',
'<tr>',
'<td>',
'<iframe frameborder="0"></iframe>',
'</td>',
'<td rowspan="4" ', display, '></td>',
'<td rowspan="4" ', display, '>',
'<ul>',
'<div>商品详情</div>',
'<li>',
'<img src="', product.pic, '"/></li>',
'<li>', product.name, '</li>',
'<li>团购价:<span>', product.price, '</span>元</li>',
'<li>市场价:<s><i>', product.marketPrice, '</i></s>元</li>',
'<li>快递公司:', product.deliverOrgs, '</li>',
'<li>仓库:', product.wareHouses, '</li>',
product.skuAttrs,
'</ul>',
'</td>',
'</tr>',
'<tr>',
'<td>',
'<select>',
'<option>宋体</option>',
'<option>黑体</option>',
'<option>幼圆</option>',
'<option>华文行楷</option>',
'<option>华文楷体</option>',
'<option>华文楷体</option>',
'<option>华文彩云</option>',
'<option>华文隶书</option>',
'<option>微软雅黑</option>',
'<option>Fixedsys</option>',
'</select>',
'<select>',
'<option value="12px">大小</option>',
'<option value="10px">10</option>',
'<option value="12px">12</option>',
'<option value="14px">14</option>',
'<option value="16px">16</option>',
'<option value="18px">18</option>',
'<option value="20px">20</option>',
'<option value="24px">24</option>',
'<option value="28px">28</option>',
'<option value="36px">36</option>',
'<option value="42px">42</option>',
'<option value="52px">52</option>',
'</select>',
'<select>',
'<option value="" selected="selected">颜色</option>',
'<option value="#000000"></option>',
'<option value="#FFFFFF"></option>',
'<option value="#008000"></option>',
'<option value="#800000"></option>',
'<option value="#808000"></option>',
'<option value="#000080"></option>',
'<option value="#800080"></option>',
'<option value="#808080"></option>',
'<option value="#FFFF00"></option>',
'<option value="#00FF00"></option>',
'<option value="#00FFFF"></option>',
'<option value="#FF00FF"></option>',
'<option value="#FF0000"></option>',
'<option value="#0000FF"></option>',
'<option value="#008080"></option>',
'</select>',
'<div></div>',
'<div></div>',
'<div></div>',
'<div></div>',
'<div>消息记录</div>',
'</td>',
'</tr>',
'<tr>',
'<td>',
'<iframe frameborder="0"></iframe>',
'</td>',
'</tr>',
'<tr>',
'<td><input type="text" value="hoojo"/><input type="button" value="关闭"/>',
'<input type="button" value="发送(Enter)"/> </td>',
'</tr>',
'</table></div>',
'<div></div>',
'</div>',
'</div>'
].join("");
},
initWebIM: function (userJID, receiver) {
var product = {
name: "小玩熊",
pic: "http://avatar.csdn.net/9/7/A/2_ibm_hoojo.jpg",
price: "198.00",
marketPrice: "899.90",
deliverOrgs: "EMS",
wareHouses: "A库",
skuAttrs: ""
};
var chatEl = $(_opts.chatLayoutTemplate(userJID, _opts.sender, receiver, product));
$("body").append(chatEl);
// 拖拽
$("#" + userJID).easydrag();
// 初始化sendMessageEditor相关信息
sendMessageEditor.iframe = this.sendMessageIFrame(userJID);
sendMessageEditor.init(userJID);
_opts.setTitle(chatEl);
_opts.writeReceiveStyle(userJID);
_opts.writeSendStyle();
_opts.createFaceElement(chatEl);
// 查看更多详情
chatEl.find(".more").click(function () {
var $ul = $(this).parents("ul");
$ul.find(".more").toggle();
$ul.find(".info").toggle();
$ul.find(".pic").toggle();
});
// 收缩详情
chatEl.find(".split").toggle(function () {
$(".product-info").hide();
$(this).parents(".radius").css("border-right-width", "0");
}, function () {
$(".product-info").show();
$(this).parents(".radius").css("border-right-width", "8px");
});
// 工具类绑定事件 settings.toolBarHandler
chatEl.find(".tool-bar td").children().click(this.toolBarHandler);
chatEl.find("#send").click(function () {
var $chatMain = $(this).parents(".chat-main");
_opts.sendHandler($chatMain);
});
chatEl.find("#close").click(function () {
var $chatMain = $(this).parents(".chat-main");
$chatMain.hide(500);
});
// 首先取消事件绑定,当一次性发多条消息的情况下会同时绑定多个相同事件
$(".have-msg, .no-msg, .chat-main").unbind("click");
$(".have-msg").bind("click", function () {
$(this).hide();
$(".no-msg").show();
$(".chat-main:hidden").show(150);
});
$(".no-msg").click(function () {
$(".chat-main:hidden").each(function (i, item) {
var top = i * 10 + 50;
var left = i * 20 + 50;
$(this).show(500).css({top: top, left: left});
});
});
$(".chat-main").click(function () {
$(".chat-main").css("z-index", 9999);
$(this).css({"z-index": 10000});
});
$(this.sendMessageIFrame(userJID).document).keyup(function (event) {
var e = event || window.event;
var keyCode = e.which || e.keyCode;
if (keyCode == 13) {
var $chatMain = $("#" + $(this).find("body").attr("jid"));
_opts.sendHandler($chatMain);
}
});
},
// 建立新聊天窗口
newWebIM: function (settings) {
var chatUser = remote.userAddress(settings.receiver);
var userJID = "u" + hex_md5(chatUser);
_opts.initWebIM(userJID, chatUser);
$("#" + userJID).find(remote.receiver).val(chatUser);
$("#" + userJID).show(220);
},
// 远程发送消息时执行函数
messageHandler: function (user, content) {
var userName = user.split("@")[0];
var tempUser = user;
if (~tempUser.indexOf("https://www.jb51.net/")) {
tempUser = tempUser.substr(0, tempUser.indexOf("https://www.jb51.net/"));
}
var userJID = "u" + hex_md5(tempUser);
// 首次初始webIM
if (!$("#" + userJID).get(0)) {
// 初始IM面板;
_opts.initWebIM(userJID, user);
}
// 设置消息接受者的名称
$("#" + userJID).find(remote.receiver).val(user);
if ($("#" + userJID).get(0)) {
// 消息提示
if ($("div[id='" + userJID + "']:hidden").get(0)) {
var haveMessage = $(".have-msg");
haveMessage.show();
$(".no-msg").hide();
}
_opts.messageTip("闪聊有了新消息,请查收!");
// 向chat接收信息区域写消息
remote.jsjac.chat.writeMessage(userJID, userName, content);
}
},
// 消息提示
messageTip: function () {
if (count % 2 == 0) {
window.focus();
document.title = "你来了新消息,请查收!";
} else {
document.title = "";
}
if (count > 4) {
document.title = "";
count = 0;
} else {
window.setTimeout(_opts.messageTip, 1000);
count ++;
}
}
};
// 初始化远程聊天程序相关方法
var initRemoteIM = function (settings) {
// 初始化远程消息
remote.jsjac.chat.init();
// 设置客户端写入信息方法
remote.jsjac.chat.writeReceiveMessage = settings.writeReceiveMessage;
// 注册事件
$(window).bind({
unload: remote.jsjac.chat.unloadHandler,
error: remote.jsjac.chat.errorHandler,
beforeunload: remote.jsjac.chat.logout
});
}
$.extend({
WebIM: function (opts) {
opts = opts || {};
// 覆盖默认配置
defaultOptions = $.extend(defaultOptions, defaultOptions, opts);
var settings = $.extend({}, defaultOptions, opts);
initRemoteIM(settings);
settings.newWebIM(settings);
$.WebIM.settings = settings;
}
});
$.WebIM.settings = $.WebIM.settings || _opts;
$.WebIM.initWebIM = _opts.initWebIM;
$.WebIM.newWebIM = _opts.newWebIM;
$.WebIM.messageHandler = _opts.messageHandler;
})(jQuery);
这里的方法基本上是聊天窗口上的应用,主要是本地聊天程序的js、HTML元素的操作。如字体、字体大小、颜色、表情、消息的发送等,不涉及到聊天消息发送的核心代码,其中有用到发送远程消息的方法。
remote.jsjac.chat.sendMessage(text, receiver); 这个是发送远程消息的方法,参数1是消息内容、参数2是消息的接收者
如果你有看到这篇文章 它是一个单纯的WebIM本地的聊天界面。
3、远程聊天JavaScript核心代码,它是和jsjac库关联的。
remote.jsjac.chat-2.0.js
复制代码 代码如下: