walkor / phpsocket.io

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

After v1.1.13, phpsocket.io cannot be connected when a browser use WebSocket first #274

Closed troublezzb closed 2 years ago

troublezzb commented 2 years ago

phpsocket.io v1.1.13 socket.io-client v2.4.0 (Chrome 98.0.4758.102 64bit)

io(window.g.socketUrl, {transports: ['websocket', 'polling']})

The following error occurred in the browser:

WebSocket connection to 'ws://192.168.255.1:4052/socket.io/?EIO=3&transport=websocket' failed:

My Thoughts: I think this judgment is not handled well. src/Engine/Engine.php (commit 25243e320bf6b43f11b96974d1a54241dcd07bce)

        if ($transport === 'websocket' && empty($sid)) {
            return call_user_func($fn, self::ERROR_UNKNOWN_TRANSPORT, false, $req, $res);
        }

Interim Solution: @jichangfeng You can try:

  1. use phpsocket.io v1.1.12
  2. removing the 'transport' attribute.
io(window.g.socketUrl)
  1. watching the ‘connect_error’ event(more).
    
    const socket = io("https://example.com", {
    transports: ["websocket", "polling"] // use WebSocket first, if available
    });

socket.on("connect_error", () => { // revert to classic upgrade socket.io.opts.transports = ["polling", "websocket"]; });

walkor commented 2 years ago

phpsocket.io v1.1.14 removed the judgment. Please upgrade. Sorry for the inconvenience.

troublezzb commented 2 years ago

It works. Thanks for your prompt reply.

jichangfeng commented 2 years ago

Solved. Thanks。