我正在尝试使用长轮询构建一个laravel聊天应用程序(我知道有nodejs/redis但是有问题)因此我一直在尝试将这个示例实现到Laravel和MySQL中.
然而,AJAX GET状态请求始终坚持的pending....我想这可能是因为它没有更新div,我从同一页面上的AJAX POST请求显示新输入的新值.目前,div仅在刷新时自行更新.
这是我的代码:
视图
function getMsg(timestamp){
var queryString = {"timestamp":timestamp};
$.get(
"create",
queryString,
function(data){
var obj = $.parseJSON(data);
console.log(obj.timestamp);
$('#response').append('<p>'+obj.body+'</p>');
getMsg(obj.timestamp);
}
).fail( function(xhr, textStatus, errorThrown) {
alert(xhr.responseText);
});
}
getMsg();
Run Code Online (Sandbox Code Playgroud)
调节器
public function create()
{
$Msg = new Chatting;
if(Request::ajax()){
set_time_limit(0);
session_write_close();
while(true){
$last_ajax_call = isset($_GET['timestamp'])?(int)$_GET['timestamp']:null;
clearstatcache();
$last_timestamp = $Msg->select(array('created_at'))->orderBy('created_at','desc')->first();
$last_change = json_decode($last_timestamp);
if($last_ajax_call == null || $last_change->created_at > $last_ajax_call){
$result = array(
'body'=> $Msg->select('body','created_at')->where('created_at','=',$last_change->created_at)->first()->body,
'timestamp'=> $last_change->created_at …Run Code Online (Sandbox Code Playgroud) 假设我有2个ArrayList of Points:
(0,2)->(0,3)->(0,4)(0,2)->(0,3)->(0,6)我想获得一个新列表: (0,2)->(0,3)
我怎么做?
当前解决方案
使用两个foreach循环逐个元素地比较两个列表.我认为这是一种非常低效的方式.还有其他方法吗?
假设我有一个带注册的聊天应用程序,它对 Apache 服务器进行长轮询。我已经阅读了一些资料,但我仍然感到困惑,并希望非常确定。根据我的理解,它可以是:
我非常倾向于第一个。你能帮我更正/扩展两者中的任何一个,或者如果两者都错了,请添加数字 3?谢谢!
A : User,
B : Conversations,
C : Messages
Run Code Online (Sandbox Code Playgroud)
我希望与 A中的A-> B-> C关系中的最新消息一起进行对话.
模型中的工作关系
User belongsToMany Conversation
Conversation belongsToMany User
Conversation hasMany Message
Message belongsTo Conversation
User hasMany Message
Message belongsTo User
**note: User and Conversation are related to a many2many relationship connected
by a pivot table
Run Code Online (Sandbox Code Playgroud)
我试过了
$user->load(['conversations' => function($q)
{
$q->orderBy('created_at', 'desc');
$q->with('Messages')->orderBy('created_at','desc');
}]);
Run Code Online (Sandbox Code Playgroud)
它几乎工作但它没有得到最后的消息.相反,它会加载属于该对话的所有消息.
来自上述代码的不需要的结果
array (size=7)
'id' => int 90
'name' => string 'haha,hehe' (length=9)
'created_at' => string '2014-07-03 15:04:04' (length=19)
'updated_at' => …Run Code Online (Sandbox Code Playgroud) 码
Entry::updateOrCreate([
'intern_id'=>$intern['id'],
'created_at'=>Carbon::parse($input['date'])
],[
'company_id'=>$intern['supervisor']['company']['id'],
'content'=>$input['text']
]);
Run Code Online (Sandbox Code Playgroud)
我正在使用此代码尝试更新/创建新记录。假设首先匹配intern_id并匹配create_at列。如果找到,它将创建一个新的。但是,似乎总是在创建一个新值,当它创建一个新值时,company_idand intern_id列设置为0,而不是原始值。
注意:intern_id还是created_at不是PK列。注意2:created_at是日期类型,而不是日期时间
php ×3
laravel ×2
laravel-4 ×2
long-polling ×2
ajax ×1
array-merge ×1
arraylist ×1
browser ×1
concurrency ×1
duplicates ×1
eloquent ×1
java ×1
laravel-5 ×1
mysql ×1
tcp-ip ×1