return object in route - php

I need to implement MinecraftQuery's class (https://github.com/xPaw/PHP-Minecraft-Query/blob/master/MinecraftQuery.class.php)
So far I have the PHP side working.
This resides in HomeController.
public function mcQuery($ip, $port)
{
$Query = new MinecraftQuery();
try
{
$host = $this->getHost($ip, $port);
$Query->Connect( $host["ip"], $host["port"] );
return $Query;
}
catch( MinecraftQueryException $e )
{
return false;
}
}
public function getHost($address, $port)
{
$result = dns_get_record("_minecraft._tcp.$address", DNS_SRV);
if ($result) {
$priority = 0;
$valid = 0;
foreach ($result as $v) {
$type = $v['type'];
$pri = $v['pri'];
$targetPort = $v['port'];
$target = $v['target'];
if ($type=="SRV") {
if ($valid==0 || $pri <= $priority) {
$address = $target;
$port = $targetPort;
$priority = $pri;
$valid = 1;
}
}
}
} else {
$address = gethostbyname($address.'.');
if(filter_var($address, FILTER_VALIDATE_IP) != $address) {
throw new \Exception("Not a valid ip address: " . $address . "\n");
}
}
return [ 'ip' => $address, 'port' => $port ];
}
}
This works - by itself anyways, but the next problem is actually returning the response to the route so I can use it via javascript
This is all I have...
Route::get('/servers', function(){
$ip = Input::get('ip');
$port = Input::get('port');
$home = App::make('HomeController');
$info = $home->mcQuery($ip, $port);
return $info
});
This returns an error, though. When I visit http://mysite.dev/servers?ip=lorem.someip.com&port=25564
I get...
The Response content must be a string or object implementing __toString(), "object" given.
However, doing a dd($info) instead returns...
object(MinecraftQuery)#130 (3) {
["Socket":"MinecraftQuery":private]=>
resource(48) of type (Unknown)
["Players":"MinecraftQuery":private]=>
NULL
["Info":"MinecraftQuery":private]=>
array(10) {
["HostName"]=>
string(37) "Some Name!"
["GameType"]=>
string(3) "SMP"
["Version"]=>
string(5) "1.7.9"
// ...
}
}
I've never seen an array with :private before and Im assuming that is apart of the problem. But this is the object I am trying to return.
Oh, and for the last bit, this is how I'm trying to get this data..
serversObj.each(function(index, el) {
// serverObj is like 4 divs
var serverIp = $(this).data('ip');
var serverPort = $(this).data('port');
var serverStatus = $(this).find('.status');
$.ajax({
url: '/servers',
type: 'GET',
data: {ip: serverIp, port: serverPort},
})
.done(function(data) {
console.log("success: \n" + data);
})
.fail(function(ex) {
console.log(ex);
return false;
});
});
But of course returns a 500 server error unless I do dd() in the route instead.
So how can I get the correct data to return?

You may try this:
return Response::json($info->GetInfo());
In the done method try:
done(function(data) {
var object = $.parseJSON(data);
console.log(object);
})

Looking at the class, it seems that you want the GetInfo() method. This example should work:
$Query = new MinecraftQuery();
$host = $this->getHost($ip, $port);
$Query->Connect( $host["ip"], $host["port"] );
var_dump($Query->GetInfo());

Related

Dialogflow ES PHP Client Library 500 (Internal Server Error)

Dialogflow and PHP newbie here.
I have created and implemented a mini button-based chatbot using Dialogflow ES PHP client library in one of my client's WordPress site.
Everything worked as expected in my local environment, but when I pushed my codes to the staging site, I got a 500 (Internal Server Error) in my DevTool. The Dialogflow console still got my initial intent from staging and responded accordingly with logs without any error, however.
This is the error shown in my server error.log
PHP Fatal error:
Uncaught Google\ApiCore\ApiException: {
"message": "Input text not set.",
"code": 3,
"status": "INVALID_ARGUMENT",
"details": []
}
thrown in /var/www/my-staging-domain.com/public/vendor/google/gax/src/ApiException.php on line 267, referer: https://my-staging-domain.com/
AJAX call frontend JS file
async function sendDataToDialogflow(text, sessionID) {
let result;
try {
result = await $.ajax({
url: /dialogflow.php,
type: "POST",
data: {
query: text,
lang: "en",
sessionId: sessionID,
},
success: function(res, status, xhr) {
setDialogflowResponse(res);
},
error: function(error) {
console.warn(error);
}
});
return result;
} catch (error) {
console.log(error);
}
}
My backend dialogflow.php file setting
<?php
namespace Google\Cloud\Samples\Dialogflow;
namespace Google\Protobuf;
use Google\Cloud\Dialogflow\V2\SessionsClient;
use Google\Cloud\Dialogflow\V2\TextInput;
use Google\Cloud\Dialogflow\V2\QueryInput;
use Google\Cloud\Dialogflow\V2\Context;
require $_SERVER['DOCUMENT_ROOT']. '/vendor/autoload.php';
if (isset($_POST) && isset($_POST["query"])) {
$query = $_POST["query"];
$lang = $_POST["lang"];
$sessionId = $_POST["sessionId"];
if (isset($_POST['query'])) {
detect_intent_texts($projectId,$query,$sessionId);
}
}
function detect_intent_texts($projectId, $text, $sessionId, $context = '', $parameters = '', $languageCode = 'en-US')
{
// -------------- new session --------------
$credential = array('credentials' => $_SERVER['DOCUMENT_ROOT'].'/client-secret.json');
$sessionsClient = new SessionsClient($credential);
$session = $sessionsClient->sessionName($projectId, $sessionId ?: uniqid());
//printf('Session path: %s' . PHP_EOL, $session);
// -------------- create text input --------------
$textInput = new TextInput();
$textInput->setText($text);
$textInput->setLanguageCode($languageCode);
if($context !== '') {
// -------------- create context struct --------------
$contextStruct = new Struct();
$contextStruct->setFields($context['parameters']);
// -------------- create context input --------------
$contextInput = new Context();
$contextInput->setLifespanCount($context['lifespan']);
$contextInput->setName($context['name']);
$contextInput->setParameters($contextStruct);
}
if($parameters !== '') {
$paramStruct = new Struct();
$paramStruct->setFields($parameters['parameters']);
}
// -------------- create query input --------------
$queryInput = new QueryInput();
$queryInput->setText($textInput);
// -------------- get response and relevant info --------------
$response = ($parameters ? $sessionsClient->detectIntent($session, $queryInput) : $sessionsClient->detectIntent($session, $queryInput));
$responseId = $response->getResponseId();
$queryResult = $response->getQueryResult();
$fields = $queryResult->getParameters()->getFields();
$jsonString = $queryResult->serializeToJsonString();
$queryText = $queryResult->getQueryText();
$intent = $queryResult->getIntent();
$displayName = $intent->getDisplayName();
$confidence = $queryResult->getIntentDetectionConfidence();
$fulfillmentText = $queryResult->getFulfillmentText();
$fulfillmentMessages = json_decode($queryResult->getFulfillmentMessages()[0]->serializeToJsonString(), true);
function get_field_value($field)
{
$kind = $field->getKind();
if ($kind == "string_value")
return $field->getStringValue();
else if ($kind == "number_value")
return $field->getNumberValue();
else if ($kind == "bool_value")
return $field->getBoolValue();
else if ($kind == "null_value")
return $field->getNullValue();
else if ($kind == "list_value") {
$list_values = $field->getListValue()->getValues();
$values = [];
foreach($list_values as $list_value)
$values[] = get_field_value($list_value);
return $values;
}
else if ($kind == "struct_value")
return $field->getStructValue();
}
$parameters = [];
foreach($fields as $key => $field) {
$parameters[$key] = get_field_value($field);
}
$returnResponse = array(
'responseId' => $responseId,
'fulfillmentText' => $fulfillmentText,
'fulfillmentMessages' => $fulfillmentMessages,
'queryText' => $queryText,
'parameters' => json_decode($jsonString)->parameters
);
echo json_encode($returnResponse);
$sessionsClient->close();
}
composer.json
{
"require": {
"google/cloud-dialogflow": "^0.29.0",
"google/protobuf": "^3.21",
"protobuf-php/protobuf": "^0.1.3",
"ext-bcmath": "*"
}
}
Any insights on this issue would be much appreciated!
I've updated staging from http to https, but this didn't fix the issue.
I've read through all the documents/questions on stackoverflow I could find, but in vain.

long polling ajax is not keeping realtime updates

i was doing long polling , but it can see in my network chrome my long polling in status pending that means is waiting for new data and changes the database , but when I try to insert one item to my database then , but when I try to insert one value show keep that long polling in real time i cant see real time updatings .. how can I fix it?
pusher controller codeigiter
public function pusher() {
header('Content-Type: application/json');
$user_id = $this->session->log['id'];
set_time_limit(0);
while (true) {
$firstCall = false;
if (isset($_GET['timestamp'])) {
$last_ajax_call = $_GET['timestamp'];
} else {
$last_ajax_call = time();
$firstCall = true;
}
clearstatcache();
$notificationsCount = $this->notification->checkForNotifications($last_ajax_call, $user_id);
$newData = (int) $notificationsCount > 0 ? true : false;
$notifications = [];
if ($newData) {
$dataSet = $this->notification->getNotifications($user_id, $last_ajax_call);
foreach($dataSet as $data) {
$notifications[] = $data;
$finalNotificationTime = $data['timestamp'];
}
$result = array('notifications' => $notifications, 'timestamp' => $finalNotificationTime);
$json = json_encode($result);
echo $json;
break;
} else {
if ($firstCall) {
$dataSet = $this->notification->getUnreadNotifications($user_id);
foreach($dataSet as $data) {
$notifications[] = $data;
}
$result = array('notifications' => $notifications, 'timestamp' => $last_ajax_call);
$json = json_encode($result);
echo $json;
break;
}
sleep(1);
session_write_close();
continue;
}
}
exit();
}
ajax
function check_notifications(timestamp) {
var queryString = {
'timestamp': timestamp
};
$.ajax({
type: 'GET',
url: URL_GET_NOTIFICATION,
data: queryString,
success: function (data) {
// put result data into "obj"
for (var i in data.notifications) {
notify(data.notifications[i].message, data.notifications[i].type, data.notifications[i].timestamp);
}
check_notifications(data.timestamp);
}
});
}
check_notifications();

PHP convert functions to MongoDB

Im trying to convert some of my PHP functions to mongodb, but cant figure it out, could someone help me out?
PHP function applications:
function applications($gangId) {
$applications = $this->db->query("SELECT * FROM `gang_applications` where `status`='avaliable' and `gangid`='$gangId'");
return ($applications ? $applications : false);
}
my attempt on function applications:
gangshema.methods.applications(thisid) {
// some sort of callback?
db.gang_applications.find({status:avaliable, gangid: thisid}, function(err, cursor) {
if (cursor != 0) {
console.log(cursor);
}
});
}
PHP function application_done
function application_done($applicationId) {
$applications = $this->db->query("SELECT * FROM `gang_applications` where `id`='$applicationId'")->row();
return ($applications->status == 'avaliable' ? false : true);
}
my attempt on function application_done
gangshema.methods.application_done(applicationid) {
db.gang_applications.find({id:applicationid}, function(err,cursor) {
// return ($applications->status == 'avaliable' ? false : true);
});
}
but my main consern is a function called accept_applications. I have no clue on how to do this part, including calling other functions for their response.
function accept_application($userid,$applicationId) {
$box = 'failure';
if (empty($applicationId)) {
$message = "applicationId is empty";
} elseif ($this->application_done($applicationId)) {
$message = "Already registred!";
} else {
$application = $this->getApplication($applicationId);
$test = true;
if(!($test)) {
$message = "false test";
} else {
$this->db->query("UPDATE `gang_applications` SET `status`='accepted', `by`='$userid' where `id`='$applicationId'");
$this->gangs->add_member($application->userid,'gang','member',$application->gangid);
$message = "Accept!";
}
}
return $message;
}
Using this at the beginning of the Node.js script:
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/test';
First snippet:
function applications($gangId) {
$applications = $this->db->query("SELECT * FROM `gang_applications` where `status`='avaliable' and `gangid`='$gangId'");
return ($applications ? $applications : false);
}
becomes:
var findApplications = function(db, gangId) {
var cursor = db.collection('gang_application').find({
"status": "available"
"gangId": gangId
});
var results = [];
cursor.each(function(err, doc) {
if (err) console.log("Error: " + err);
} else if (doc != null) {
console.log("Null document.");
} else {
results.push(doc);
}
});
return results;
};
Second snippet:
function application_done($applicationId) {
$applications = $this->db->query("SELECT * FROM `gang_applications` where `id`='$applicationId'")->row();
return ($applications->status == 'avaliable' ? false : true);
}
becomes:
function applications(gangId) {
db.gang_application
}
var applicationsDone = function(db, applicationId) {
var cursor = db.collection('gang_application').find({
"id": applicationId
});
var results = [];
cursor.each(function(err, doc) {
if (err) {
console.log("Error: " + err);
} else if (doc != null) {
console.log("Null document.");
} else {
results.push(doc.status);
}
});
return results;
};
Call both as follows:
MongoClient.connect(url, function(err, db) {
if (!db) {
console.log("Database did not connect.");
}
else {
findApplications(db, "102"); // Replace "102" with gangId
applicationsDone(db, "104"); // Replace "104" with applicationId
}
});
EDIT per comments, here's how to include a callback:
// Note extra `callback` parameter
var applicationsDone = function(db, applicationId, callback) {
var cursor = db.collection('gang_application').findOne({
"id": applicationId
});
cursor.each(function(err, doc) {
if (err) {
console.log("Error: " + err);
} else if (doc != null) {
console.log("Null document.");
} else {
return (doc.status == "available");
}
});
};
To call:
MongoClient.connect(url, function(err, db) {
if (!db) {
console.log("Database did not connect.");
}
else {
var callback = function(doc) { console.log(doc.status); };
applicationsDone(db, "104", callback);
}
});
EDIT Third snippet:
function accept_application($userid, $applicationId) {
$box = 'failure';
if (empty($applicationId)) {
$message = "applicationId is empty";
} elseif ($this->application_done($applicationId)) {
$message = "Already registred!";
} else {
$application = $this->getApplication($applicationId);
$test = true;
if(!($test)) {
$message = "false test";
} else {
$this->db->query("UPDATE `gang_applications` SET `status`='accepted', `by`='$userid' where `id`='$applicationId'");
$this->gangs->add_member($application->userid,'gang','member',$application->gangid);
$message = "Accept!";
}
}
return $message;
}
becomes this. Note some changes to the functions above to get this to work, such as returning an array of documents from applications, and using .findOne() for applicationDone():
function acceptApplication(userId, applicationId) {
if (!applicationId) return "No application ID";
if (applicationDone(db, applicationId)) return "Application already submitted.";
// Left out `if (!$test)`
db.gang_applications.update({
"id": applicationId,
"userId": userId
}, {
"status": "accepted"
}, upsert: false);
// $this->gangs->add_member($application->userid,'gang','member',$application->gangid);
return "Accepted!";
}

Execution of mySQL query in jQuery.Post method

I have created a javascript file that contains js to trigger an event onchange of a drop down list. The JS is below:
// /public/js/custom.js
jQuery(function($) {
$("#parent").change(function(event){
event.preventDefault();
var parentID = $('#parent').val();
var id = $('#id').val();
$.post("/menu/GetMenuChildren", {pID: parentID, thisID: id },
function(data){
if(data.response === true){
var $el = $("#Position");
$el.empty(); // remove old options
$.each(data.newOptions, function(key, value) {
$el.append($("<option></option>")
.attr("value", value).text(key));
});
} else {
// print error message
alert("something went wrong in Post");
}
}, 'json');
alert("After Post");
});
});
In my Controller.php I have an function GetMenuChildrenAction as shown below:
public function GetMenuChildrenAction()
{
$request = $this->getRequest();
$response = $this->getResponse();
if ($request->isPost())
{
$post_data = $request->getPost();
$parent_id = $post_data['pID'];
$id = $post_data['thisID'];
//$this->form->get('Position')->SetOptionValues(
$newOptions = $this->getMenuTable()->GetPositionsByID($parent_id, $id);
if(isset($newOptions))
{
$response->setContent(\Zend\Json\Json::encode(array('response' => true, 'newOptions' => $newOptions)));
}
else
{
$response->setContent(\Zend\Json\Json::encode(array('response' => false)));
}
}
return $response;
}
In the MenuTable.php there is a Method GetPositionsByID as shown below:
public function GetPositionsByID($parentID, $id)
{
if($parentID == 0)
{
$menu = $this->getMenu($this->id);
$parentID = $menu->parent_id;
}
if(isset($parentID))
{
$sql = "Select ID,Label from Menu Where parent_ID = " . $parentID . " and id > 1 and id <> " . $id . " Order by Position,Label";
try
{
$statement = $this->adapter->query($sql);
}
catch(Exception $e) {
console.log('Caught exception: ', $e->getMessage(), "\n");
}
$res = $statement->execute();
$rows = array();
$i = 0;
foreach ($res as $row) {
$i = $i + 1;
$rows[$i] = array (
$i => $row['Label']
);
}
return $rows;
}
return array();
}
It all seems to be hooked up correctly, when I debug the code, I get the this line:
$statement = $this->adapter->query($sql);
and then nothing happens. If I replace all the code in the GetPositionsByID method, and simply return an array like the following:
return array('1' => 'one', '2' => 'two');
it works great, however i want to get the data from the DB. Does anyone know why the execute would fail on this line?
$statement = $this->adapter->query($sql);
Thanks in advance
The issue was that the adapter was null.

PHP Socket Server Memory Leak

I have checked the memory whilst sending and receiving data over one connection, and I appear to be correctly clearing variables, as the memory returns to its previous value.
But for some reason if I make a new connection, then close the connection, memory is leaked. I believe the problem may be occurring when a socket is accepted.
I am using PHP 5.2.10
Hopefully one of you can find the time to have a play with the source and figure out where its gone wrong. Thanks in advance
<?php
Class SuperSocket
{
var $listen = array();
var $status_listening = FALSE;
var $sockets = array();
var $event_callbacks = array();
var $recvq = 1;
var $parent;
var $delay = 100; // 10,000th of a second
var $data_buffer = array();
function SuperSocket($listen = array('127.0.0.1:123'))
{
$listen = array_unique($listen);
foreach ($listen as $address)
{
list($address, $port) = explode(":", $address, 2);
$this->listen[] = array("ADDR" => trim($address), "PORT" => trim($port));
};
}
function start()
{
if ($this->status_listening)
{
return FALSE;
};
$this->sockets = array();
$cursocket = 0;
foreach ($this->listen as $listen)
{
if ($listen['ADDR'] == "*")
{
$this->sockets[$cursocket]['socket'] = socket_create_listen($listen['PORT']);
$listen['ADDR'] = FALSE;
}
else
{
$this->sockets[$cursocket]['socket'] = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
};
if ($this->sockets[$cursocket]['socket'] < 0)
{
return FALSE;
};
if (#socket_bind($this->sockets[$cursocket]['socket'], $listen['ADDR'], $listen['PORT']) < 0)
{
return FALSE;
};
if (socket_listen($this->sockets[$cursocket]['socket']) < 0)
{
return FALSE;
};
if (!socket_set_option($this->sockets[$cursocket]['socket'], SOL_SOCKET, SO_REUSEADDR, 1))
{
return FALSE;
};
if (!socket_set_nonblock($this->sockets[$cursocket]['socket']))
{
return FALSE;
};
$this->sockets[$cursocket]['info'] = array("ADDR" => $listen['ADDR'], "PORT" => $listen['PORT']);
$this->sockets[$cursocket]['channels'] = array();
$this->sockets[$cursocket]['id'] = $cursocket;
$cursocket++;
};
$this->status_listening = TRUE;
}
function new_socket_loop(&$socket)
{
$socket =& $this->sockets[$socket['id']];
if ($newchannel = #stream_socket_accept($socket['socket'], 0));//#socket_accept($socket['socket']))
{
socket_set_nonblock($newchannel);
$socket['channels'][]['socket'] = $newchannel;
$channel = array_pop(array_keys($socket['channels']));
$this->remote_address($newchannel, $remote_addr, $remote_port);
$socket['channels'][$channel]['info'] = array('ADDR' => $remote_addr, 'PORT' => $remote_port);
$event = $this->event("NEW_SOCKET_CHANNEL");
if ($event)
$event($socket['id'], $channel, $this);
};
}
function endswith($string, $test) {
$strlen = strlen($string);
$testlen = strlen($test);
if ($testlen > $strlen) return false;
return substr_compare($string, $test, -$testlen) === 0;
}
function recv_socket_loop(&$socket)
{
$socket =& $this->sockets[$socket['id']];
foreach ($socket['channels'] as $channel_id => $channel)
{
unset($buffer);#Flush buffer
$status = #socket_recv($channel['socket'], $buffer, $this->recvq, 0);
if ($status === 0 && $buffer === NULL)
{
$this->close($socket['id'], $channel_id);
}
elseif (!($status === FALSE && $buffer === NULL))
{
$sockid = $socket['id'];
if(!isset($this->data_buffer[$sockid]))
$this->data_buffer[$sockid]='';
if($buffer!="\r"&&$buffer!="\n")
{
//Putty ends with \r\n
$this->data_buffer[$sockid].=$buffer;
}
else if($buffer!="\n") //ignore the additional newline char \n
{
$event = $this->event("DATA_SOCKET_CHANNEL");
if ($event)
$event($socket['id'], $channel_id, $this->data_buffer[$sockid], $this);
unset($this->data_buffer[$sockid]);
}
};
}
}
function stop()
{
$this->closeall();
$this->status_listening = FALSE;
foreach ($this->sockets as $socket_id => $socket)
{
socket_shutdown($socket['socket']);
socket_close($socket['socket']);
};
$event = $this->event("SERVER_STOP");
if ($event)
$event($this);
}
function closeall($socket_id = NULL)
{
if ($socket_id === NULL)
{
foreach ($this->sockets as $socket_id => $socket)
{
foreach ($socket['channels'] as $channel_id => $channel)
{
$this->close($socket_id, $channel_id);
}
}
}
else
{
foreach ($this->sockets[$socket_id]['channels'] as $channel_id => $channel)
{
$this->close($socket_id, $channel_id);
};
};
}
function close($socket_id, $channel_id)
{
unset($this->data_buffer[$socket_id]); //clear the sockets data buffer
$arrOpt = array('l_onoff' => 1, 'l_linger' => 1);
#socket_shutdown($this->sockets[$socket_id]['channels'][$channel_id]['socket']);
#socket_close($this->sockets[$socket_id]['channels'][$channel_id]['socket']);
$event = $this->event("LOST_SOCKET_CHANNEL");
if ($event)
$event($socket_id, $channel_id, $this);
}
function loop()
{
while ($this->status_listening)
{
usleep($this->delay);
foreach ($this->sockets as $socket)
{
$this->new_socket_loop($socket);
$this->recv_socket_loop($socket);
};
$event = $this->event("END_SOCKET_CHANNEL");
if ($event)
$event($this);
};
}
function write($socket_id, $channel_id, $buffer)
{
#socket_write($this->sockets[$socket_id]['channels'][$channel_id]['socket'], $buffer);
#socket_write($this->sockets[$socket_id]['channels'][$channel_id]['socket'], 'Server memory usage: '.memory_get_usage().'/'.memory_get_peak_usage(true)."\r\n");
}
function get_channel_info($socket_id, $channel_id)
{
return $this->sockets[$socket_id]['channels'][$channel_id]['info'];
}
function get_socket_info($socket_id)
{
$socket_info = $this->sockets[$socket_id]['info'];
if (empty($socket_info['ADDR']))
{
$socket_info['ADDR'] = "*";
};
return $socket_info;
}
function get_raw_channel_socket($socket_id, $channel_id)
{
return $this->sockets[$socket_id]['channels'][$channel_id]['socket'];
}
function remote_address($channel_socket, &$ipaddress, &$port)
{
socket_getpeername($channel_socket, $ipaddress, $port);
}
function event($name)
{
if (isset($this->event_callbacks[$name]))
return $this->event_callbacks[$name];
}
function assign_callback($name, $function_name)
{
$this->event_callbacks[$name] = $function_name;
}
};
?>
Server.php
include("supersocket.class.php");
function startswith($string, $test) {
return strpos($string, $test, 0) === 0;
}
function newdata($socket_id, $channel_id, $buffer, &$server)
{
//$server->write($socket_id, $channel_id, ">".$buffer."\r\n");
if($buffer=="STOP")
{
$server->stop();
}
else if($buffer=="DATETIME")
{
$server->write($socket_id, $channel_id, ">".date("dmYHis")."\r\n");
}
else
{
$server->write($socket_id, $channel_id, ">BAD\r\n");
}
};
function newclient($socket_id, $channel_id, &$server)
{
$server->write($socket_id, $channel_id, "HEADER\n\r");
}
$socket = new SuperSocket(array('127.0.0.1:12345'));
$socket->assign_callback("DATA_SOCKET_CHANNEL", "newdata");
$socket->assign_callback("NEW_SOCKET_CHANNEL", "newclient");
$socket->start();
//set_time_limit(60*2);
set_time_limit(60*60*24*5); //5 days
$socket->loop();
Edit: sorry you might need to change the socket accept back to:
if ($newchannel = #socket_accept($socket['socket']))
then close the connection, memory is leaked
This is a tricky one - even the standard reference counting garbage collector only kicks in at intervals which are difficult to predict. Calling gc_collect_cycles() should trigger the gc though. Try calling that whenever you close a connection and see if it makes a difference.
If you're still seeing problems - then check if you've got the cyclic reference counter compiled in - if not, then get it.
The Channel array was never removed upon closing the connection, surprised no one picked up on this. Memory usage is now super tight.
unset($this->sockets[$socket_id]['channels'][$channel_id]);
But it does mean that any event for LOST_SOCKET_CHANNEL is pretty useless for the time being.
Will accept my own answer when stack over flow allows. Thanks for all your help ppl .. i guess..

Categories