Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to install this? #177

Open
MarcTremblay1981 opened this issue Feb 22, 2023 · 6 comments
Open

How to install this? #177

MarcTremblay1981 opened this issue Feb 22, 2023 · 6 comments

Comments

@MarcTremblay1981
Copy link

MarcTremblay1981 commented Feb 22, 2023

Hi, I'm trying to get started with your project and I'm pulling my hair out just trying to "install" it. Is there a simple, complete ZIP archive that contains all the dependencies? I don't understand how to use Composer.

Executing the command in your ReadMe: composer require textalk/websocket
Returns this error: Root package 'textalk/websocket' cannot require itself in its composer.json

Composer is the most cryptic thing I've used yet. I don't understand why some projects don't just come in a ZIP like in the past 25 years. Why make it so complicated just to get started?

@sirn-se
Copy link
Contributor

sirn-se commented Feb 22, 2023

Learning Composer is well worth the effort. And it's not that complicated.

  1. Go to the directory where you're code resides.
  2. Run curl -s https://getcomposer.org/installer | php
  3. Run php composer.phar require phrity/websocket

You should now have a composer.json file containing dependencies, and a directory vendor with this repo and its dependencies. In your local file use require 'vendor/autoload.php' to get all classes loaded automatically. That's it.

You should not download this code yourself, and not run composer in the downloaded directory (that's why you get that error), but let Composer handle the dependencies for you.

@MarcTremblay1981
Copy link
Author

MarcTremblay1981 commented Feb 22, 2023

Step # 2 doesn't work...
-bash: PHP: command not found

FYI, I'm under CentOS 7 with PHP 8.2

Also, when you wrote "You should not download this code yourself, and not run composer in the downloaded directory" then what does step # 1 mean? I'm so confused! I've downloaded the GitHub files. I have the directories like examples, lib, tests and bunch of files like .gitignore, composer.json, COPYING.md, Makefile, phpunit.xml.dist, README.md. It's all there.

@sirn-se
Copy link
Contributor

sirn-se commented Feb 22, 2023

All the references to php above is for the PHP binary. Replace them, if necessary, with the search path you normally use when running PHP.

Composer will download the library, its dependencies, and create the autoloader for you.
So you only need to install Composer itself. Once in place, it will manage your projects dependencies.

@MarcTremblay1981
Copy link
Author

MarcTremblay1981 commented Feb 24, 2023

Alright, so what I did to install this is:

  1. Create an empty folder
  2. In bash, run composer require textalk/websocket in that empty folder
  3. chmod/chown -R all the files that step 2 produced

This seems to work. I can start the server and connect ONE (1) client script to the server and it sends/receives correctly.

However, the issue that I'm now running into is that I cannot seem to connect more than a single client at a time to it. The server script I'm using right now is: examples/echoserver.php

When I attempt to connect a second client to it, initially it seems to work (it outputs "Creating client" and then "Sending text" or whatever), but all commands time out and are never processed by the server script at all. Is there a reason why it does not accept more than 1 client at a time? How can we make it accept and serve 100 connections at a time? The client script I'm using is: examples/random_client.php

I can see in vendor/textalk/websocket/lib/Server.php that the accept() function arbitrarily blocks other incoming connections. Why?

/**
* Accept a single incoming request.
* Note that this operation will block accepting additional requests.
* @return bool True if listening.
*/
public function accept(): bool
{
    $this->disconnect();
    return (bool)$this->listening;
}

EDIT: After even more investigating, it seems like your WebSocket implementation does not use non blocking sockets (using the native stream_set_blocking() function)... Is there a reason for this? I notice that the current implementation uses fread() in vendor/textalk/websocket/lib/Connection.php which will literally block the whole app until at least 2 bytes come in from the most recently connected socket, making this impossible to use with more than 1 client at a time.

@MarcTremblay1981
Copy link
Author

Well, I came to the conclusion that this is very far from an usable multi-client WS and it has way too many dependencies than is really required. I went with the following for my project: It's a PHP 8.2 compatible multi-client WS server that works perfectly fine and it even supports WSS (WS over SSL so it's secure). Not sure how 152 files could ever beat this simple page of code, but it works flawlessly and it's super easy to tweak too. Hope it helps anyone: https://www.php.net/manual/en/function.stream-socket-server.php#125540

Good luck.

@sirn-se
Copy link
Contributor

sirn-se commented Feb 26, 2023

Well, this repo is a full implementation of the websocket protocol. It's a bit more to it than just I/O.
If you prefer to build your own implementation from scratch, this is not the code library you've been looking for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants