I am trying to configure the Binary.com API, but with some difficulties.
I need to get the message back, but that's not the right way.
In function, if I ask to return the array, it does not return. But if I send a print, the result is displayed on the screen.
See:
require __DIR__ . '/vendor/autoload.php';
$loop = \React\EventLoop\Factory::create();
$connector = new \Ratchet\Client\Connector($loop);
$GLOBALS['app_id'] = XXX;
class Bot {
public static function authorize($loop, $connector, $token, $ret = null) {
if (!is_array($ret)) {
$connector('wss://ws.binaryws.com/websockets/v3?app_id=' . $GLOBALS['app_id'])->then(function ($conn) use ($loop, $token) {
$conn->send("{\"authorize\": \"" . $token . "\"}");
$conn->on('message', function (\Ratchet\RFC6455\Messaging\MessageInterface $msg) use ($conn) {
$conn->close();
Bot::authorize(null, null, json_decode($msg, true));
});
}, function ($e) {
Bot::authorize(null, null, $e->getMessage());
$loop->stop();
});
$loop->run();
}
print_r($ret); //Data is displayed on screen
return $ret;
}
}
$bot = Bot::authorize($loop, $connector, 'a1-tokenxxx');
print_r($bot); //Data is not displayed on screen
See the comments.
Why is this happening?
What is the correct way to return promise data?
Related
I am fairly new to Laravel. This may have an obvious solution but I can't seem to find it so far. Therefore I am asking for help.
Question In Short:
I use Illuminate\Http\Request session ($request->session()) to store some data I get from BigCommerce API. But I can't get them when I need the data.
Context:
I am building a sample app/boilerplate app for BigCommerce platform using Laravel/React. I have built the app using official documentation, semi-official posts released by BigCommerce team and sample codebase provided by them as well.
App works fine with local credentials from a specific store because they are given as environment variables to the app. However I can't read store_hash (which is necessary to fetch data from BigCommerce) and access token. Both I have put in $request->session() object.
I will paste the AppController.php code below, also code is publicly available here:
In the makeBigCommerceAPIRequest method below (as you can see my debugging efforts :)) I can get $this->getAppClientId(), but I can't get anything from $request->session()->get('store_hash') or $request->session()->get('access_token') which returns from $this->getAccessToken($request).
I have tried putting store hash into a global variable, but it didn't work.
From everything I have experienced so far, $request is not working as expected.
Any help appriciated, thanks in advance.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use mysql_xdevapi\Exception;
use Oseintow\Bigcommerce\Bigcommerce;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Bigcommerce\Api\Client as BigcommerceClient;
use Illuminate\Support\Facades\Storage;
use App\Config; //Database Connection
use Bigcommerce\Api\Connection;
class AppController extends Controller
{
protected $bigcommerce;
private $client_id;
private $client_secret;
private $access_token;
private $storehash;
private $redirect_uri;
public function __construct(Bigcommerce $bigcommerce)
{
$this->bigcommerce = $bigcommerce;
$this->client_id = \config('app.clientId');
$this->client_secret = \config('app.clientSecret');
$this->redirect_uri = \config('app.authCallback');
}
public function getAppClientId()
{
if (\config('app.appEnv') === 'local') {
return \config('app.localClientId');
} else {
return \config('app.clientId');
}
}
public function getAppSecret()
{
if (\config('app.appEnv') === 'local') {
return \config('app.localClientSecret');
} else {
return \config('app.clientSecret');
}
}
public function getAccessToken(Request $request)
{
if (\config('app.appEnv') === 'local') {
return \config('app.localAccessToken');
} else {
return $request->session()->get('access_token');
}
}
public function getStoreHash(Request $request)
{
if (\config('app.appEnv') === 'local') {
return \config('app.localStoreHash');
} else {
return $request->session()->get('store_hash');
}
}
public function error(Request $request)
{
$errorMessage = "Internal Application Error";
if ($request->session()->has('error_message')) {
$errorMessage = $request->session()->get('error_message');
}
echo '<h4>An issue has occurred:</h4> <p>' . $errorMessage . '</p> Go back to home';
}
public function load(Request $request)
{
$signedPayload = $request->get('signed_payload');
if (!empty($signedPayload)) {
echo "hello";
$verifiedSignedRequestData = $this->verifySignedRequest($signedPayload);
if ($verifiedSignedRequestData !== null) {
echo "positive return";
$request->session()->put('user_id', $verifiedSignedRequestData['user']['id']);
$request->session()->put('user_email', $verifiedSignedRequestData['user']['email']);
$request->session()->put('owner_id', $verifiedSignedRequestData['owner']['id']);
$request->session()->put('owner_email', $verifiedSignedRequestData['owner']['email']);
$request->session()->put('store_hash', $verifiedSignedRequestData['context']);
echo $request->session()->get('store_hash');
$this->storehash = $verifiedSignedRequestData['context'];
echo ' store hash is at the moment : ' . $this->storehash . ' .....';
} else {
return "The signed request from BigCommerce could not be validated.";
// return redirect()->action([AppController::class, 'error'])->with('error_message', 'The signed request from BigCommerce could not be validated.');
}
} else {
return "The signed request from BigCommerce was empty.";
// return redirect()->action([AppController::class, 'error'])->with('error_message', 'The signed request from BigCommerce was empty.');
}
return redirect(\config('app.appUrl'));
}
public function install(Request $request)
{
// Make sure all required query params have been passed
if (!$request->has('code') || !$request->has('scope') || !$request->has('context')) {
echo 'Not enough information was passed to install this app.';
// return redirect()->action('MainController#error')->with('error_message', 'Not enough information was passed to install this app.');
}
try {
$client = new Client();
$result = $client->request('POST', 'https://login.bigcommerce.com/oauth2/token', [
'json' => [
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'redirect_uri' => $this->redirect_uri,
'grant_type' => 'authorization_code',
'code' => $request->input('code'),
'scope' => $request->input('scope'),
'context' => $request->input('context'),
]
]);
$statusCode = $result->getStatusCode();
$data = json_decode($result->getBody(), true);
if ($statusCode == 200) {
$request->session()->put('store_hash', $data['context']);
$request->session()->put('access_token', $data['access_token']);
$request->session()->put('user_id', $data['user']['id']);
$request->session()->put('user_email', $data['user']['email']);
// $configValue = Config::select('*')->where('storehash', $data['context'])->get()->toArray();
// if (count($configValue) != 0) {
// $id = $configValue[0]['id'];
// $configObj = Config::find($id);
// $configObj->access_token = $data['access_token'];
// $configObj->save();
// } else {
// $configObj = new Config;
// $configObj->email = $data['user']['email'];
// $configObj->storehash = $data['context'];
// $configObj->access_token = $data['access_token'];
// $configObj->save();
// }
// If the merchant installed the app via an external link, redirect back to the
// BC installation success page for this app
if ($request->has('external_install')) {
return redirect('https://login.bigcommerce.com/app/' . $this->getAppClientId() . '/install/succeeded');
}
}
return redirect(\config('app.appUrl'));
} catch (RequestException $e) {
$statusCode = $e->getResponse()->getStatusCode();
echo $statusCode;
$errorMessage = "An error occurred.";
if ($e->hasResponse()) {
if ($statusCode != 500) {
echo "some error other than 500";
// $errorMessage = Psr7\str($e->getResponse());
}
}
// If the merchant installed the app via an external link, redirect back to the
// BC installation failure page for this app
if ($request->has('external_install')) {
return redirect('https://login.bigcommerce.com/app/' . $this->getAppClientId() . '/install/failed');
} else {
echo "fail";
// return redirect()->action('MainController#error')->with('error_message', $errorMessage);
}
}
// return view('index');
}
public function verifySignedRequest($signedRequest)
{
list($encodedData, $encodedSignature) = explode('.', $signedRequest, 2);
// decode the data
$signature = base64_decode($encodedSignature);
$jsonStr = base64_decode($encodedData);
echo $jsonStr;
$data = json_decode($jsonStr, true);
// confirm the signature
$expectedSignature = hash_hmac('sha256', $jsonStr, $this->client_secret, $raw = false);
if (!hash_equals($expectedSignature, $signature)) {
error_log('Bad signed request from BigCommerce!');
return null;
}
return $data;
}
public function makeBigCommerceAPIRequest(Request $request, $endpoint)
{
echo ' ...... trying to make an apiRequest now : with storehash : ' . $this->storehash . ' .............';
echo '...........................................';
echo 'other variables at the moment :::: ............... client ID :' . $this->getAppClientId() . '...................... token : ' . $this->getAccessToken($request) . '...............';
$requestConfig = [
'headers' => [
'X-Auth-Client' => $this->getAppClientId(),
'X-Auth-Token' => $this->getAccessToken($request),
'Content-Type' => 'application/json',
]
];
if ($request->method() === 'PUT') {
$requestConfig['body'] = $request->getContent();
}
$client = new Client();
$result = $client->request($request->method(), 'https://api.bigcommerce.com/' . $this->storehash . '/' . $endpoint, $requestConfig);
return $result;
}
public function proxyBigCommerceAPIRequest(Request $request, $endpoint)
{
if (strrpos($endpoint, 'v2') !== false) {
// For v2 endpoints, add a .json to the end of each endpoint, to normalize against the v3 API standards
$endpoint .= '.json';
}
echo ' asadssada ...... trying to make an apiRequest now : with storehash : ' . $this->storehash . ' .............' . $request->session()->get('store_hash') . ' ............ ';
$result = $this->makeBigCommerceAPIRequest($request, $endpoint);
return response($result->getBody(), $result->getStatusCode())->header('Content-Type', 'application/json');
}
}
Thanks for the detailed info. Though it would help to annotate the debug lines with confirmation of what they output, I am making the assumption that you have narrowed the problem down to the session storage and retrieval lines.
$request->session()->put() and get() are the correct ways to access session.
I would therefore suggest investigating Session configuration: https://laravel.com/docs/8.x/session#configuration
If using file-based sessions, confirm that there are no permissions errors, perhaps. Alternatively try and different session storage mechanism.
I am trying to make a donation through paypal using the this URL. When I login to PayPal and make a payment, I get the following message
I do have the backend code and here is what it looks like.
<?php
use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Api\Payer;
use PayPal\Api\Details;
use PayPal\Api\Amount;
use PayPal\Api\Transaction;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\PaymentExecution;
require __DIR__ . DIRECTORY_SEPARATOR . '../vendor/autoload.php';
/**
* Edri PayPal Pyment
*/
class Edri_PayPal_Payment
{
private $api;
private $payer;
private $details;
private $amount;
private $transaction;
private $payment;
private $redirectUrls;
function __construct()
{
$this->api = $this->setup_PayPal_Api();
}
private function debug($val)
{
echo '<pre>';
var_dump($val);
echo '</pre>';
}
private function setup_PayPal_Api()
{
$api = new ApiContext(
new OAuthTokenCredential(
'MyPayPalClientID',
'MyClientSecret'
)
);
$api->setConfig(array(
'mode' => 'live',
'http.ConnectionTimeOut' => 30,
'Log.LogEnabled' => false,
'Log.FileName' => '',
'Log.LogLevel' => 'FINE',
'validation.level' => 'log'
));
return $api;
}
private function setupPayer()
{
$this->payer = new Payer();
$this->payer->setPayment_method('paypal');
}
private function setupDetails($amount)
{
$this->details = new Details();
$this->details->setShipping('0.00')
->setTax('0.00')
->setsubTotal($amount);
}
private function setupAmount($amount)
{
$this->amount = new Amount();
$this->amount->setCurrency('EUR')
->setTotal($amount)
->setDetails($this->details);
}
private function setupTransaction($amount)
{
$this->transaction = new Transaction();
$this->transaction->setAmount($this->amount)
->setDescription('Make a donation of €' . $amount . ' to EDRi');
}
private function setupPayment()
{
$this->payment = new Payment();
$this->payment->setIntent('sale')
->setPayer($this->payer)
->setTransactions(array($this->transaction))
->setRedirectUrls($this->redirectUrls);
}
private function setupRedirectUrls()
{
$this->redirectUrls = new RedirectUrls();
$this->redirectUrls->setReturnUrl('https://edri.org/payment?pppa=true')
->setCancelUrl('https://edri.org/payment?pppa=false');
}
public function prepare_payment ($paymentCredtials) {
$amount = str_replace(',', '', number_format($paymentCredtials['edriPayment_amount'], 2));
$this->setupPayer();
$this->setupDetails($amount);
$this->setupAmount($amount);
$this->setupTransaction($amount);
$this->setupRedirectUrls();
$this->setupPayment();
try {
$this->payment->create($this->api);
$paymentID = $this->payment->getId();
} catch (Exception $e) {
$this->log($e);
header('Location: https://edri.org/donation-oops');
return false;
}
return $paymentID;
}
private function log($log){
$file = __DIR__ . DIRECTORY_SEPARATOR . '../logs/paypal_log.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= $prefix . ' ' . date('m/d/Y h:i:s a', time()) . ' //// ' . "\n";
$current .= self::var_dump_str($log) . "\n";
// Write the contents back to the file
file_put_contents($file, $current);
}
public function execute_payment($paymentCredentials)
{
$this->debug($paymentCredentials);
$payment = Payment::get($paymentCredentials['paymentId'], $this->api);
$execution = new PaymentExecution();
$execution->setPayerId($paymentCredentials['PayerID']);
try {
echo $payment->execute($execution, $this->api);
} catch (Exception $e) {
$this->log($e);
header('Location: https://edri.org/donation-oops');
return false;
}
return $payment->state = 'approved' ? true : false;
}
public function kickoff_payment()
{
foreach ($this->payment->getLinks() as $link) {
if ($link->getRel() == 'approval_url') {
$redirectUrl = $link->getHref();
}
}
header('Location: ' . $redirectUrl);
}
}
I checked the logs, nothing is reported. API calls seems to be fine as well.
Any suggestion to make this work would be helpful.
There's a possibility that the var_dump() statements you have are violating the "no content output before header" rule and therefore, the user's payment flow stops with your code, before the header('Location: ' . $redirectUrl); can redirect to PayPal to finish the job.
Try removing those var_dumps, and doing error_log(printf($val, true)) instead, as not to interrupt the call to header().
Edit - From http://php.net/manual/en/function.header.php:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
<html>
<?php
/* This will give an error. Note the output
* above, which is before the header() call */
header('Location: http://www.example.com/');
exit;
?>
I am making an app that translates a word from one language to English and gets information about it (e.g. definition, use in a sentence, synonyms, sound representation)
What my function does:
Searches for the translation in the database. If it is found, we return it.
If it is not found we translate a word using google translate, or Yandex translate API.
If translation is found we download it's sound representation, save the translation to the database and add additional information from other API's
We return a json response with all of the information.
Now my controllers method is really big and I can't find a cleaner way to go about it.
Any help is appreciated.
public function store(Request $request)
{
$translated = $request->get('translated');
$translation = $this->translation->findBy('translated', $translated)->first();
if ($translation) {
return Response::json(['translation' => $this->translation->with(['examples', 'definitions', 'synonyms', 'images'])->find($translation->id)], ResponseCode::HTTP_CREATED);
}
$data = $request->all();
$data['translation'] = $this->translate($translated);
if ($translated == $data['translation']) {
Log::info('Translation not found: ' . $data['translation']);
return $this->translationNotFound();
}
$downloader = new Downloader(new GoogleSpeechDownloader());
$filename = $downloader->download($data['translation']);
if ($filename) $data['sound_name'] = $filename;
$translation = $this->translation->create($data);
$this->createDefinition($translation);
$this->createExample($translation);
$this->createSynonym($translation);
return Response::json(['translation' => $this->translation->with(['examples', 'definitions', 'synonyms', 'images'])->find($translation->id)], ResponseCode::HTTP_CREATED);
}
private function translationNotFound()
{
return Response::json(['error' => 'Vertimas nerastas.'], ResponseCode::HTTP_NOT_FOUND);
}
private function createDefinition($translation)
{
$definition = new Definition();
$definer = new Definer(new DictionaryApiDefiner());
try {
$definition->definition = $definer->getDefinition($translation->translation);
$definition->approved = true;
$translation->definitions()->save($definition);
} catch (\Exception $e) {
Log::alert('Definition for word ' . $translation->translation . ' not found.');
}
}
private function createExample($translation)
{
$example = new Example();
$exampler = new ExampleCreator(new YourDictionaryGouteParserExampler());
try {
$example->example = $exampler->getExample($translation->translation);
$example->approved = true;
$translation->examples()->save($example);
} catch (\Exception $e) {
Log::alert('Example for word ' . $translation->translation . ' not found.');
}
}
private function createSynonym($translation)
{
$creator = new SynonymCreator(new BigHugeLabsSynonymCreator());
foreach ($creator->getSynonyms($translation->translation) as $s) {
$synonym = new Synonym();
$synonym->synonym = $s;
$synonym->approved = true;
$translation->synonyms()->save($synonym);
}
}
private function translate($translated)
{
$translator = new Translator(new GoogleTranslator());
try {
return $translator->translate($translated);
} catch (\Exception $e) {
Log::critical($e->getMessage());
}
$translator = new Translator(new YandexTranslator());
return $translator->translate($translated);
}
If you want cleaner code, just make a class for this job. Two classes for this two API's and in the controller make the check for the word, if not exist in the database, check in the other two API's, just split every action to method in the new two classes that you will make.
I am attempting to add logging for the envelope generated by a third party library. I am modifying the updateMetadataField() method below.
I am creating $client like so:
$client = new UpdateClient($UPDATE_END_POINT, $USER_AUTH_ARRAY);
I have tried both $this->client->__getLastRequest() and $this->__getLastRequest() with the same error as a result.
When the SoapClient is instantiated trace is set to true.
Error is
Fatal error: Call to undefined method UpdateClient::__getLastRequest()
So how do I correctly access the __getLastRequest() method?
$USER_AUTH_ARRAY = array(
'login'=>"foo",
'password'=>"bar",
'exceptions'=>0,
'trace'=>true,
'features' => SOAP_SINGLE_ELEMENT_ARRAYS
);
class UpdateClient {
private $client;
public function __construct($endpoint, $auth_array) {
$this->client = new SoapClient($endpoint, $auth_array);
}
public function updateMetadataField($uuid, $key, $value) {
$result = $this->client->updateMetadataField(array(
'assetUuid' => $uuid,
'key' => $key,
'value' => $value)
);
if(is_soap_fault($result)) {
return $result;
}
return $result->return . "\n\n" . $this->client->__getLastRequest();
} // updateMetadataField()
} // UpdateClient
UPDATE - adding calling code This code iterates over an array which maps our data to the remote fields.
What I am hoping to do is begin storing the envelope we send to aid in debugging.
$client = new UpdateClient($UPDATE_END_POINT, $USER_AUTH_ARRAY);
foreach ($widen_to_nool_meta_map as $widen => $nool) { // array defined in widen.php
if ($nool != '') {
// handle exceptions
if ($nool == 'asset_created') { // validate as date - note that Widen pulls exif data so we don't need to pass this
if (!strtotime($sa->$nool)) {
continue;
}
} else if ($nool == 'people_in_photo' || $nool == 'allow_sublicensing' || $nool == 'allowed_use_pr_gallery') {
// we store as 0/1 but GUI at Widen wants Yes/No
$sa->$nool = ($sa->$nool == '1') ? 'Yes' : 'No';
} else if ($nool == 'credit_requirements') {
$sa->$nool = $sa->credit_requirements()->label;
}
$result = $client->updateMetadataField($sa->widen_id, $widen, $sa->$nool);
if(is_soap_fault($result)) {
$sync_result = $sync_result . "\n" . $result->getMessage();
} else {
$sync_result = $sync_result . "\n" . print_r($result, 1);
}
} // nool field set
} // foreach mapped field
If you want to access UpdateClient::__getLastRequest() you have to expose that method on the UpdateClient class since the $client is a private variable. The correct way of calling it is $this->client->__getLastRequest().
Take a look at this working example, as you can see I'm consuming a free web service for testing purposes.
<?php
$USER_AUTH_ARRAY = array(
'exceptions'=>0,
'trace'=>true,
'features' => SOAP_SINGLE_ELEMENT_ARRAYS
);
class TestClient {
private $client;
public function __construct($endpoint, $auth_array) {
$this->client = new SoapClient($endpoint, $auth_array);
}
public function CelsiusToFahrenheit( $celsius ) {
$result = $this->client->CelsiusToFahrenheit(array(
'Celsius' => $celsius
)
);
if(is_soap_fault($result)) {
return $result;
}
return $result;
}
public function __getLastRequest() {
return $this->client->__getLastRequest();
}
}
try
{
$test = new TestClient( "http://www.w3schools.com/webservices/tempconvert.asmx?wsdl", $USER_AUTH_ARRAY);
echo "<pre>";
var_dump($test->CelsiusToFahrenheit( 0 ));
var_dump($test->__getLastRequest());
var_dump($test->CelsiusToFahrenheit( 20 ));
var_dump($test->__getLastRequest());
echo "</pre>";
}
catch (SoapFault $fault)
{
echo $fault->faultcode;
}
?>
I am making an html5 game www.titansoftime.com
I am using ratchet as a php websocket server solution. It works great! http://socketo.me/docs/push
I have done several standalone test using the php pthreads extension and have seen some very exciting results. It truly works and works well.. as long as websockets aren't in the mix.
Pthreads give php multithreading capabilities (it really does work and it's amazing). http://php.net/manual/en/book.pthreads.php
This is what I do:
/src/server.php
This is the file that launches the daemon.
<?php
session_start();
use Ratchet\Server\IoServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Pusher;
require __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../mysql.cls.php';
require_once __DIR__ . '/../game.cls.php';
require_once __DIR__ . '/../model.cls.php';
$mysql = new mysql;
$game = new game;
$loop = React\EventLoop\Factory::create();
$pusher = new MyApp\Pusher();
$loop->addPeriodicTimer(0.50, function() use($pusher){
$pusher->load();
});
$webSock = new React\Socket\Server($loop);
if ($loop instanceof \React\EventLoop\LibEventLoop) {
echo "\n HAS LibEvent";
}
$webSock->listen(8080, '0.0.0.0'); // Binding to 0.0.0.0 means remotes can connect
$webServer = new Ratchet\Server\IoServer(
new Ratchet\Http\HttpServer(
new Ratchet\WebSocket\WsServer($pusher)
),
$webSock
);
$loop->run();
This all works fine.
/src/MyApp/Pusher.php
This class pushes data to all connected users.
<?php
namespace MyApp;
use Ratchet\ConnectionInterface;
use Ratchet\MessageComponentInterface;
class AsyncThread extends \Thread{
public $client;
public function __construct($client){
$this->client = $client;
}
public function run(){
// do work on $this->client
$user = mysql::assoc('SELECT * from users WHERE connection_id = "'.$this->client->resourceId.'"');
// etc..
$this->client->send(json_encode(array('foo'=>'bar')));
}
}
class Pusher implements MessageComponentInterface{
public static $clients = array();
#load
public static function load(){
$client_count = count(self::$clients);
echo "\n\n\n".'Serving to '.$client_count.' clients. '.time();
$start = $istart = microtime(true);
if( !count(self::$clients) ){
if( !mysql_ping() ){
$game->connect();
}
}
$threads = array();
foreach( self::$clients as $key => $client ){
// HANDLE CLIENT
// This works just fine, the only problem is that if I have lets say 50 simultaneous users, the people near the end of the clients array will have to wait till the other users have been processed. This is not desirable
$client->send(json_encode('foo'=>'bar'));
// So I tried this:
$threads[$key] = new AsyncThread($client);
$threads[$key]->start();
// At this point the AsyncThread class will throw a fatal error complaining about not being able to serialize a closure.
// If I dont set "$this->data = $client;" in the thread constructor no error appears but now I cant use the data.
// Also regardless of whether or not I bind the data in the AsyncThread constructor,
// the connection disappears if I call "new AsyncThread($client)". I cannot explain this behavior.
}
}
public function onMessage(ConnectionInterface $from, $msg) {
global $game;
if( $msg ){
$data = json_decode($msg);
if( $data ){
switch( $data->task ){
#connect
case 'connect':
echo "\n".'New connection! ('.$from->resourceId.') '.$from->remoteAddress;
self::$clients[] = $from;
break;
default:
self::closeConnection($from);
echo "\nNO TASK CLOSING";
break;
}
}else{
echo "\n NO DATA";
self::closeConnection($from);
}
}else{
echo "\n NO MSG";
self::closeConnection($from);
}
}
public function closeConnection($conn){
global $game;
if( $conn ){
if( $conn->resourceId ){
$connid = $conn->resourceId;
$conn->close();
$new = array();
foreach( self::$clients as $client ){
if( $client->resourceId != $connid ){
$new[] = $client;
}
}
self::$clients = $new;
$game->query('UPDATE users set connection_id = 0 WHERE connection_id = "'.intval($connid).'" LIMIT 1');
echo "\n".'Connection '.$connid.' has disconnected';
}
}
}
public function onClose(ConnectionInterface $conn) {
echo "\nCLIENT DROPPED";
self::closeConnection($conn);
}
public function onOpen(ConnectionInterface $conn) {
}
public function onError(ConnectionInterface $conn, \Exception $e) {
echo "\nCLIENT ERRORED";
self::closeConnection($conn);
}
public function onSubscribe(ConnectionInterface $conn, $topic) {
}
public function onUnSubscribe(ConnectionInterface $conn, $topic) {
}
public function onCall(ConnectionInterface $conn, $id, $topic, array $params) {
}
public function onPublish(ConnectionInterface $conn, $topic, $event, array $exclude, array $eligible) {
}
}
This all works fine as long as I don't create a thread inside the event loop.
Am I going about this the wrong way or is php multithreading and websockets incompatible?
check this package https://github.com/huyanping/react-multi-process
Install
composer require jenner/react-multi-process
How to use it?
So simple like:
$loop = React\EventLoop\Factory::create();
$server = stream_socket_server('tcp://127.0.0.1:4020');
stream_set_blocking($server, 0);
$loop->addReadStream($server, function ($server) use ($loop) {
$conn = stream_socket_accept($server);
$data = "pid:" . getmypid() . PHP_EOL;
$loop->addWriteStream($conn, function ($conn) use (&$data, $loop) {
$written = fwrite($conn, $data);
if ($written === strlen($data)) {
fclose($conn);
$loop->removeStream($conn);
} else {
$data = substr($data, 0, $written);
}
});
});
// the second param is the sub process count
$master = new \React\Multi\Master($loop, 20);
$master->start();
An example using jenner/simple_fork like:
class IoServer {
/**
* #param int $count worker process count
* Run the application by entering the event loop
* #throws \RuntimeException If a loop was not previously specified
*/
public function run($count = 1) {
if (null === $this->loop) {
throw new \RuntimeException("A React Loop was not provided during instantiation");
}
if($count <= 1){
$this->loop->run();
}else{
$loop = $this->loop;
$master = new \Jenner\SimpleFork\FixedPool(function() use($loop) {
$this->loop->run();
}, $count);
$master->start();
$master->keep(true);
// or just
// $master = new \React\Multi\Master($this->loop, $count);
// $master->start();
}
}
}