paragi / PHP-websocket-client

142 stars 60 forks source link

send message from php to websocket #145 #25

Closed daniel89fg closed 1 year ago

daniel89fg commented 1 year ago

Hello.

I have a connection created from javascripts successfully towards my websocket server. I send messages and receive the perfect response. I am using the cboden/ratchet library.

Now what I need is to save a message in the database, to be able to send a message from php to the websocket server so that it sends it to the chat. I can't do this.

I've tried the sample code but I can't do anything. Can you guide me a bit?

Thank you.

try {
            $sp = new \Paragi\PhpWebsocket\Client('localhost?idphonenumber=' . $phone->idphonenumber, 9235);
            $sp->write("hello server");
            echo "Server responded with: " . $sp->read();
        } catch (\Paragi\PhpWebsocket\ConnectionException $e) {
            echo "Something gets wrong " . $e->getMessage();
        }
paragi commented 1 year ago

I would start by hardcoding the address for a test. Are you sure that localhost is translated?

daniel89fg commented 1 year ago

Yes, my connection from javascripts is (localhost:9235), I can connect and send messages to the server and I receive the response without problem.

What I can't get is to connect the client from php to the same server.

Trismegiste commented 1 year ago

'localhost?idphonenumber='... is not a valid host.

Query parameters ('?id...) should go into the $path parameter in the constructor: https://github.com/paragi/PHP-websocket-client/blob/a40a6bf0525a1e76950d19b41201ccccb80bf9cd/src/Client.php#L86

That differs from the WebSocket javascript class which takes a full URL (host + query parameters)

daniel89fg commented 1 year ago

With or without parameters, it does not make the connection. I am using docker and port 9235 is open.

Trismegiste commented 1 year ago

Ratchet server runs in the same docker container as the php script client ? If not, what is the network_mode ? host ?

I have a project which runs on docker with Ratchet with PHP client and javascript client (with text & binary socket) https://github.com/Trismegiste/eclipse-wiki

paragi commented 1 year ago

Host and path is not necessarily the same thing. You want to specify the path, not the host.

The implementation might be a little clumsy - I don't remember why host is not a part of path...?

Try something like this:

path: '127.0.0.1?idphonenumber=12345678'

( 'localhost' is not translated to 127.0.0.1 )

daniel89fg commented 1 year ago

The Ratchet server is in a Docker container called "wasocket".

And the chat container for the client in a separate one called "facturascripts".

The network of both containers is not set, from what I understand it will be the default.

daniel89fg commented 1 year ago

I still can't get it to work

try {
            $headers = '';
            $error_string = '';
            $sp = new \Paragi\PhpWebsocket\Client(
                'localhost',
                9235,
                $headers,
                $error_string,
            10,
            false,
            false,
                '127.0.0.1?idphonenumber=' . $phone->idphonenumber);
            $sp->write("hello server");
            echo "Server responded with: " . $sp->read();
        } catch (\Paragi\PhpWebsocket\ConnectionException $e) {
            echo "Something gets wrong " . $e->getMessage();
        }
daniel89fg commented 1 year ago

This is my docker-compose.yml

version: '3'

services:

  facturascripts:
    container_name: facturascripts
    build: ./php7.4
    restart: always
    ports:
      - 80:80
    volumes:
      - ./facturascripts:/var/www/html

  wasocket:
    build: ./php7.4-wasocket
    container_name: wasocket
    ports:
      - 9235:9235
    volumes:
      - ./wasocket:/var/www

networks:
  proxy:
    external: true
daniel89fg commented 1 year ago

I got it, it connects now. Thanks for the support and help.