消息好吧,我有这样的代码闯过从WebSocket的
<script type="text/javascript">
var ws;
var userName = "";
var displayOnTextArea = function (msg) {
var tarea = $('#textarea');
tarea.prepend ( '<div style="background: rgb(51, 51, 51); color: white; padding: 31px; border: 1px solid rgba(0, 0, 0, 0.44); border-radius: 10px; padding-top: 40px; padding-bottom: 20px;">' + msg + '</div><br>');
}
var sendMessage = function (msg) {
if (userName == "SYSTEM")
ws.send ("<div style='background: rgb(88, 86, 86); background: -webkit-repeating-linear-gradient(45deg, #606dbc, #606dbc 10px, #465298 10px, #465298 20px); background: repeating-linear-gradient(45deg, #606dbc, #606dbc 10px, #465298 10px, #465298 20px); border: 1px solid rgba(0, 0, 0, 0.44); color: white; width: 100px; text-align: center; margin-left: 751px;; margin-top: -55px; padding: 5px; border-radius: 10px; font-family: josefin_sansbold;'>" + userName + "</div> " + msg);
else
ws.send ("<div style='background: rgb(88, 86, 86); <? if(User::isAdmin()) { echo 'background: -webkit-repeating-linear-gradient(45deg, #606dbc, #606dbc 10px, #465298 10px, #465298 20px); background: repeating-linear-gradient(45deg, #606dbc, #606dbc 10px, #465298 10px, #465298 20px);'; } ?> border: 1px solid rgba(0, 0, 0, 0.44); color: white; width: 100px; text-align: center; margin-left: 751px;; margin-top: -55px; padding: 5px; border-radius: 10px; font-family: josefin_sansbold;'>" + userName + "</div> " + msg);
}
var safe_tags = function(str) {
return str.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>') ;
}
$(document).keypress (function (event) {
if (event.which == 13) {
if ($('#input_').val() != "") {
if ($('#input_').val() != " ") {
displayOnTextArea ("<div style='background: rgb(88, 86, 86); <? if(User::isAdmin()) { echo 'background: -webkit-repeating-linear-gradient(45deg, #606dbc, #606dbc 10px, #465298 10px, #465298 20px); background: repeating-linear-gradient(45deg, #606dbc, #606dbc 10px, #465298 10px, #465298 20px);'; } ?> border: 1px solid rgba(0, 0, 0, 0.44); color: white; width: 100px; text-align: center; margin-left: 751px;; margin-top: -55px; padding: 5px; border-radius: 10px; font-family: josefin_sansbold;'><?=$User->username?></div>" + safe_tags($('#input_').val()));
sendMessage (safe_tags($('#input_').val()));
$('#input_'). val ("");
}
}
}
});
$(document).ready (function () {
userName = "<? echo $User->username; ?>";
console.log ("Attempting to connect to server");
ws = new WebSocket ("ws://scribblehost.ws:1035/avatarrealms162882"); //We need a serverip.
ws.onopen = function () {
console.log ("Connected");
userName = "SYSTEM";
displayOnTextArea ("<div style='background: rgb(88, 86, 86); background: -webkit-repeating-linear-gradient(45deg, #606dbc, #606dbc 10px, #465298 10px, #465298 20px); background: repeating-linear-gradient(45deg, #606dbc, #606dbc 10px, #465298 10px, #465298 20px); border: 1px solid rgba(0, 0, 0, 0.44); color: white; width: 100px; text-align: center; margin-left: 751px;; margin-top: -55px; padding: 5px; border-radius: 10px; font-family: josefin_sansbold;'>SYSTEM</div> Welcome <?=$User->username?> to the chat!");
sendMessage ("Welcome <?=$User->username?> to the chat!");
userName = "<? echo $User->username; ?>";
}
ws.onmessage = function (evt) {
console.log (evt.msg);
displayOnTextArea (evt.data);
}
});
</script>
<input type='text' id='input_' style='font-size:20px;height:30px;width: 100%;' autofocus="autofocus" /></div>
<div id='textarea' style='padding-top: 15px; background: none; resize: none; border: none; font-size: 20px; padding-bottom: 185px; max-height: 389px; padding-left: 145px; margin-top: -6px; overflow-y: hidden;'></div>
我怎样才能让我这样当用户乔伊斯聊天,它显示聊天5条最新消息? 我试过循环,但我不知道什么/在哪里循环
基本上,我想它显示聊天时的最新消息,当他们加入,所以他们知道用户在说什么,等等。
相关http://stackoverflow.com/questions/17359871/how-to-send-old-messages-with-websockets – 2014-09-26 11:25:05
也许这有助于 http://stackoverflow.com/questions/17359871/how - 发送旧邮件与Websockets/26177047#26177047 – 2014-10-03 13:31:45