我正在尝试在我的网站上建立一个聊天室。
我有来自服务器的参数数据。
但是我不知道如何使用参数与其他人交流。
聊天室模板:
var me = {};
var you = {};
me.avatar = "img/10_avatar-512.png";
you.avatar = "img/10_avatar-512.png";
//insert
function insertChat(who, text, time) {
if (time === undefined) {
time = 0;
}
var control = "";
if (who == "you") {
control = '<div class="chat-item">' +
'<div class="chat-cont-warp">' +
'<div class="left-head">' +
'<img src="' + you.avatar + '" class="head-img">' +
'</div>' +
'<div class="right-content">' +
'<div class="top-desc">' +
'<div class="user-name">COM</div>' +
'<div class="chat-item-wrap">' +
'<div class="chat-cont">' + text + '</div>' +
'<span class="send-time">' + date + '</span>' +
'</div>' +
'</div>' +
'</div>' +
'</div>' +
'</div>';
} else {
control = '<div class="chat-item">' +
'<div class="chat-cont-warp own-information">' +
'<div class="left-head">' +
'<img src="' + me.avatar + '" class="head-img avatar">' +
'</div>' +
'<div class="right-content">' +
'<div class="top-desc">' +
'<div class="user-name line_id">Player</div>' +
'<div class="chat-item-wrap">' +
'<div class="chat-cont">' + text + '</div>' +
'<span class="send-time">' + date + '</span>' +
'</div>' +
'</div>' +
'</div>' +
'</div>' +
'</div>';
}
//
setTimeout(function () {
$(".chat-list").append(control);
}, time);
//user info
$.getJSON(url, function (data) {
$('.avatar').attr("src", data.image_url);
$('.line_id').html(data.user_name);
});
}
//Clear list
function resetChat() {
$(".chat-list").empty();
}
// detect enter keypress + call click
$("#send-chat").keypress(function (e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13) {
$(".send-chat").click();
}
});
// detect mouse click
$(document).ready(function () {
$(".send-chat").click(function () {
var text = $("#send-chat").val();
if (text !== "") {
insertChat("me", text);
$("#send-chat").val('');
}
})
});
resetChat();
知道我必须聆听服务器并从所有人那里收到文本,但是如何?
我不确定如何使用这些参数发送消息。
{
"action":"login_public",
"data": {
"corp_id": "1",
"user_id": "785"
}
}
{
"action":"send_in_public",
"data": {
"corp_id": "1",
"user_id": "785",
"message_type": "text",
"message": "hello"
}
}
请多多指教我,谢谢