add long pool (test)
This commit is contained in:
parent
7914e927ed
commit
320b716a5f
|
@ -1,14 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace vision\messages;
|
||||
|
||||
/**
|
||||
* This is just an example.
|
||||
*/
|
||||
class AutoloadExample extends \yii\base\Widget
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return "Hello!";
|
||||
}
|
||||
}
|
|
@ -70,11 +70,31 @@ class MessageApiAction extends Action {
|
|||
}
|
||||
|
||||
|
||||
protected function pooling() {
|
||||
$last_id = \Yii::$app->request->get('last_id', false);
|
||||
if(!$last_id) {
|
||||
sleep(7);
|
||||
$this->sendJson(['status' => false, 'message' => 'No last id, info:' . print_r($last_id, 1)]);
|
||||
}
|
||||
$time_cancel = (int) ini_get('max_execution_time') - 1;
|
||||
$duration = $time_cancel < 25 ? $time_cancel : 25;
|
||||
$endTime = time() + $duration;
|
||||
while(time() < $endTime){
|
||||
$data = \Yii::$app->mymessages->checkMessage($last_id);
|
||||
if (count($data) > 0) {
|
||||
$this->sendJson($data);
|
||||
}
|
||||
sleep(7);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected function sendJson($data) {
|
||||
$response = Yii::$app->response;
|
||||
$response->format = \yii\web\Response::FORMAT_JSON;
|
||||
$response->data = $data;
|
||||
$response->send();
|
||||
flush();
|
||||
die();
|
||||
}
|
||||
|
||||
|
|
|
@ -59,6 +59,12 @@ class MyMessages extends Component {
|
|||
return $this->getMessages(\Yii::$app->user->id);
|
||||
}
|
||||
|
||||
|
||||
public function checkMessage($last_id){
|
||||
$whom_id = \Yii::$app->user->getId();
|
||||
return $this->getMessages($whom_id, false, 1, $last_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to getAllMessages.
|
||||
*
|
||||
|
@ -241,9 +247,17 @@ class MyMessages extends Component {
|
|||
->select(['msg.created_at', 'msg.id', 'msg.status', 'msg.message', "usr1.id as from_id", "usr1.$this->attributeNameUser as from_name", "usr2.id as whom_id", "usr2.$this->attributeNameUser as whom_name"])
|
||||
->from("$table_name as msg")
|
||||
->leftJoin("$this->userTableName as usr1", 'usr1.id = msg.from_id')
|
||||
->leftJoin("$this->userTableName as usr2", 'usr2.id = msg.whom_id')
|
||||
->where(['msg.whom_id' => $whom_id, 'msg.from_id' => $from_id])
|
||||
->orWhere(['msg.from_id' => $whom_id, 'msg.whom_id' => $from_id]);
|
||||
->leftJoin("$this->userTableName as usr2", 'usr2.id = msg.whom_id');
|
||||
|
||||
|
||||
if($from_id) {
|
||||
$query
|
||||
->where(['msg.whom_id' => $whom_id, 'msg.from_id' => $from_id])
|
||||
->orWhere(['msg.from_id' => $whom_id, 'msg.whom_id' => $from_id]);
|
||||
} else {
|
||||
$query->where(['msg.whom_id' => $whom_id]);
|
||||
}
|
||||
|
||||
|
||||
if($type) {
|
||||
$query->andWhere(['=', 'msg.status', $type]);
|
||||
|
@ -259,11 +273,13 @@ class MyMessages extends Component {
|
|||
$ids = Array();
|
||||
foreach($return as $m) {
|
||||
//$return[$m['from_name']][] = $m;
|
||||
$ids[] = $m['id'];
|
||||
if($m['whom_id'] == \Yii::$app->user->getId()) {
|
||||
$ids[] = $m['id'];
|
||||
}
|
||||
}
|
||||
|
||||
//change status to is_read
|
||||
if(count($ids) > 0 && $whom_id == \Yii::$app->user->id) {
|
||||
if(count($ids) > 0) {
|
||||
Messages::updateAll(['status' => Messages::STATUS_READ], ['in', 'id', $ids]);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,3 +1,13 @@
|
|||
function getUrl() {
|
||||
pathArray = location.href.split( '/' );
|
||||
protocol = pathArray[0];
|
||||
host = pathArray[2];
|
||||
var url = protocol + '//' + host + '/admin/battle/private-messages';
|
||||
return url;
|
||||
};
|
||||
|
||||
|
||||
//Работа с окном сообщением
|
||||
var messages = (function() {
|
||||
var di = {};
|
||||
|
||||
|
@ -16,7 +26,7 @@ var messages = (function() {
|
|||
self.form = self.mainBox.find('form.message-form').eq(0);
|
||||
self.inputText = self.form.find('[name="input_message"]');
|
||||
self.inputFromId = self.form.find('[name="message_id_user"]');
|
||||
self.lastId = 0;
|
||||
self.lastId = 1;
|
||||
};
|
||||
|
||||
init();
|
||||
|
@ -58,14 +68,6 @@ var messages = (function() {
|
|||
});
|
||||
};
|
||||
|
||||
function getUrl() {
|
||||
pathArray = location.href.split( '/' );
|
||||
protocol = pathArray[0];
|
||||
host = pathArray[2];
|
||||
var url = protocol + '//' + host + '/admin/battle/private-messages';
|
||||
return url;
|
||||
}
|
||||
|
||||
this.reInit = function() {
|
||||
init();
|
||||
};
|
||||
|
@ -85,12 +87,12 @@ var messages = (function() {
|
|||
|
||||
this.clearBox = function() {
|
||||
self.box.html('');
|
||||
self.lastId = 0;
|
||||
self.lastId = 1;
|
||||
};
|
||||
|
||||
this.createHtmlMessage = function(n) {
|
||||
var html = '';
|
||||
html += '<div class="message ' + (n['i_am_sender'] ? 'bubble-left' : 'bubble-right') + '">';
|
||||
html += '<div class="message ' + (n['i_am_sender'] ? 'bubble-right' : 'bubble-left') + '">';
|
||||
html += '<label class="message-user">' + n['from_name'] + '</label>';
|
||||
html += '<label class="message-timestamp">' + n['created_at'] +'</label>';
|
||||
html += '<p>' + n['message'] + '</p>';
|
||||
|
@ -98,16 +100,9 @@ var messages = (function() {
|
|||
return html;
|
||||
};
|
||||
|
||||
this.form.submit(function() {
|
||||
var text = self.inputText.val();
|
||||
var id = self.inputFromId.val();
|
||||
if(!id || !text) {
|
||||
return false;
|
||||
}
|
||||
sendMessage(id, text);
|
||||
return false;
|
||||
});
|
||||
|
||||
this.fromPooling = function(m) {
|
||||
self.log(m);
|
||||
};
|
||||
|
||||
this.updateBox = function(m) {
|
||||
var html = '';
|
||||
|
@ -124,6 +119,16 @@ var messages = (function() {
|
|||
self.box.animate({scrollTop: self.box.prop("scrollHeight")}, 500);
|
||||
};
|
||||
|
||||
this.form.submit(function() {
|
||||
var text = self.inputText.val();
|
||||
var id = self.inputFromId.val();
|
||||
if(!id || !text) {
|
||||
return false;
|
||||
}
|
||||
sendMessage(id, text);
|
||||
return false;
|
||||
});
|
||||
|
||||
this.mainBox.find('.contact').click(function(event){
|
||||
var user_id = $(this).data('user');
|
||||
if(user_id) {
|
||||
|
@ -135,5 +140,103 @@ var messages = (function() {
|
|||
});
|
||||
|
||||
di[id_block] = this;
|
||||
|
||||
//this.pools = new pooling(self.lastId);
|
||||
//this.pools.addListener('newData', this.fromPooling);
|
||||
|
||||
}
|
||||
})();
|
||||
})();
|
||||
|
||||
|
||||
|
||||
//Пуллинг, данные отдаются через события
|
||||
var pooling = (function() {
|
||||
var lastId = 0;
|
||||
var di = false;
|
||||
|
||||
return function(max_id) {
|
||||
var self = this;
|
||||
var activeTimeout = false;
|
||||
|
||||
if(max_id > lastId) {
|
||||
lastId = max_id;
|
||||
}
|
||||
|
||||
if(di != false) {
|
||||
return di;
|
||||
}
|
||||
|
||||
var pooling = function (){
|
||||
self.ajax = $.ajax({
|
||||
url:getUrl(),
|
||||
type:"GET",
|
||||
data:{last_id:lastId, action:'pooling'},
|
||||
cahce:false,
|
||||
timeout:10000,
|
||||
async:true,
|
||||
success:function(result){
|
||||
self.triggerEvent('newData', result);
|
||||
},
|
||||
complete:function() {
|
||||
if(activeTimeout) {
|
||||
setTimeout(pooling(lastId), 40000);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
this.start = function() {
|
||||
if(!activeTimeout) {
|
||||
pooling();
|
||||
activeTimeout = true;
|
||||
}
|
||||
};
|
||||
|
||||
this.stop = function() {
|
||||
if(activeTimeout) {
|
||||
if(self.ajax) {
|
||||
self.ajax.abort();
|
||||
}
|
||||
activeTimeout = false;
|
||||
}
|
||||
};
|
||||
|
||||
this.log = function(data) {
|
||||
console.log(data);
|
||||
};
|
||||
|
||||
di = this;
|
||||
this.listeners = {};
|
||||
|
||||
this.addListener = function(evt, callback) {
|
||||
|
||||
if ( !this.listeners.hasOwnProperty(evt) ) {
|
||||
this.listeners[evt] = [];
|
||||
}
|
||||
|
||||
this.listeners[evt].push(callback);
|
||||
};
|
||||
|
||||
this.removeListener = function(evt, callback) {
|
||||
if ( this.listeners.hasOwnProperty(evt) ) {
|
||||
var i,length;
|
||||
for (i = 0, length = this.listeners[evt].length; i < length; i += 1) {
|
||||
if ( this.listeners[evt][i] === callback) {
|
||||
this.listeners[evt].splice(i, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.triggerEvent = function(evt, args) {
|
||||
if ( this.listeners.hasOwnProperty(evt) ) {
|
||||
var i,length;
|
||||
for (i = 0, length = this.listeners[evt].length; i < length; i += 1) {
|
||||
this.listeners[evt][i](args);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.start();
|
||||
};
|
||||
})();
|
||||
|
|
Reference in a new issue