walkor / phpsocket.io

A server side alternative implementation of socket.io in PHP based on workerman.
2.3k stars 508 forks source link

Cannot communicate back <=> front #278

Closed QuentinBubu closed 2 years ago

QuentinBubu commented 2 years ago

Hi! I'm using the lib phpsocket io to make my back and frontend communicate with sockets, the problem is that I receive the connection well since it is displayed "Connected" and that appears every 2~3 seconds, but after that, I can't send or receive messages, can you help? Thanks in advance ^^

Server:

<?php

namespace Bubu\Sockets;

use Workerman\Worker;
use PHPSocketIO\Socket;
use PHPSocketIO\SocketIO;

class Server
{

    public static SocketIO $io;

    public static function serve()
    {
        echo 'Server start' . PHP_EOL;

        self::$io = new SocketIO($_ENV['SOCKET_PORT']);

        self::$io->on('connection', function (Socket $socket) {

            echo 'Connected' . PHP_EOL;

            $socket->on('hello', function ($data) use ($socket) {
                echo $data . PHP_EOL;
                $socket->emit('hello', 'world');
            });

        });

        Worker::runAll();
    }
}

Client:

    <form action="">
        <input type="text">
        <button type="button">Send</button>
    </form>

    <script defer>
        const socket = io('http://' + document.domain + ':3300');

        document.querySelector('button').addEventListener('click', function() {
            socket.emit('hello', 'world');
            console.log('click')
        });

        socket.on('hello', function() {
            console.log('OK');
        });

    </script>

cdn for socket.io: https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.5.1/socket.io.js

image

QuentinBubu commented 2 years ago

After some tests, the problem seems to be due to the socket.io cdn.

Is your library maintained up to date for the latest versions of socket.io?

QuentinBubu commented 2 years ago

Ok, solved, it's the version of socket io... An update would be nice, the lib is well done! It's a pity