Elephant.io Client not working

喜夏-厌秋 提交于 2020-01-24 23:46:46

问题


I've a socket.io based Websocket-Server which should get notifications from a php application. I found http://elephant.io/ which seems to be the right library for doing this. It seems that elephant.io is optimized for composer because i can't find any example for using it without them and by trying it on my own I get endless errors of includes files which are missing.

So I installed composer the first time and the elephant.io packet in the folder lib\composer so all files of this library are located in lib\composer\vendor\wisembly. The example from http://elephant.io/#usage is not working because the constructor of the Client doesn't accept these parameters. It has only two arguments and the first one must be a instance of ElephantIO\EngineInterface so I tried the following:

error_reporting(E_ALL);
ini_set('display_errors', true);

require_once __DIR__ . '/lib/composer/vendor/autoload.php';

use ElephantIO\Client as Elephant;
use ElephantIO\Engine\SocketIO as Version1X;

$elephant = new Elephant(new Version1X('http://mywsserver.com:8000'));

$elephant->init();
$elephant->send(
    ElephantIOClient::TYPE_EVENT,
    null,
    null,
    json_encode(array('name' => 'foo', 'args' => 'bar'))
);
$elephant->close();

echo 'tryin to send `bar` to the event `foo`';

But its also not working:

Fatal error: Class 'ElephantIO\Engine\SocketIO' not found in C:\inetpub\wwwroot\test.php on line 11

I'm new to composer but for me it seems that this system should be handling all the including-stuff by including the autoloader-file.

I just want to send notifications from a php-app to a socket.io server, it seems very hard to do that. The only alternative I found is https://github.com/rase-/socket.io-php-emitter but this one required redis. I wouldn't like this because I'm using Windows and my application wouldn't profit from redis because I have only one server.

Isn't there an easy way to do this? I used phpws before I'm using socket.io, this worked perfectly, but socket.io seems to be a better solution instead of the .NET library I used as server before, so I would prefer to keep on socket.io.


回答1:


I used ElephantIO before and what I did was create my own php autoloader. composer should have done it but can't see where you went wrong from the code. Anyway this was my autoloader.php

require_once("src/Client.php");
require_once("src/EngineInterface.php");
require_once("src/AbstractPayload.php");
require_once("src/Exception/SocketException.php");
require_once("src/Exception/MalformedUrlException.php");
require_once("src/Exception/ServerConnectionFailureException.php");
require_once("src/Exception/UnsupportedActionException.php");
require_once("src/Exception/UnsupportedTransportException.php");

require_once("src/Engine/AbstractSocketIO.php");
require_once("src/Engine/SocketIO/Session.php");
require_once("src/Engine/SocketIO/Version0X.php");
require_once("src/Engine/SocketIO/Version1X.php");
require_once("src/Payload/Decoder.php");
require_once("src/Payload/Encoder.php");



回答2:


I fixed this by using:

use ElephantIO\Engine\SocketIO\Version1X as Version1X;

instead of:

use ElephantIO\Engine\SocketIO as Version1X;

and then:

$Elephant   =   new Elephant(new Version1X("http://localhost:8000"));
var_dump($Elephant);

result:

object(ElephantIO\Client)#8 (2) { ["engine":"ElephantIO\Client":private]=> object(ElephantIO\Engine\SocketIO\Version1X)#9 (4) { ["url":protected]=> array(6) { ["scheme"]=> string(4) "http" ["host"]=> string(9) "localhost" ["query"]=> array(0) { } ["path"]=> string(9) "socket.io" ["port"]=> int(8000) ["secured"]=> bool(false) } ["session":protected]=> NULL ["options":protected]=> array(7) { ["context"]=> array(0) { } ["debug"]=> bool(false) ["wait"]=> int(100000) ["timeout"]=> string(2) "60" ["version"]=> int(2) ["use_b64"]=> bool(false) ["transport"]=> string(7) "polling" } ["stream":protected]=> NULL } ["logger":"ElephantIO\Client":private]=> NULL }




回答3:


You need to review a little bit your conception of PHP Namespaces, as Mfaspk's answer suggested it.

We cannot have an autoloader in the package, as it is a library, not an application per se ; it will be included with other libraries. So, an autoloader in your own application should be taking care of the autoloading, not the library (otherwise, if each library would need to register their own autoloading system, it would be kinda an unmaintainable mess)

BTW, the doc on the site is not up to date (we should take care of this stuff with a little bit of time...). As the README is mentionning it, there is a simple exemple showing a basic how-to : https://github.com/Wisembly/elephant.io/tree/master/example/socket.io/1.x/emitter

Sorry for the delay, I'm not checking often enough stackoverflow, specially on the subject of Elephant.io :)



来源:https://stackoverflow.com/questions/27799591/elephant-io-client-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!