Websockets: Are Rachet & autobahn from npm compati

2019-08-02 19:12发布

问题:

I'm experimenting with the Ratchet php library. In particular, I've been trying to integrate their push integration demo into a React application.

Their demo references a seemingly one-off version of the autobahn JS library, which works against my server implementation (a near duplicate of their demo).

However, when I try to use autobahn JS from npm everything melts down. It seems this implementes a newer version of the Web Sockets protocol?

The one-off version of autobahn passes wamp for the Sec-WebSocket-Protocol header, which Ratchet seems to support. The npm version of autobahn passes wamp.2.json, wamp.2.msgpack...

Digging through the Ratchet code I found this

If any component in a stack supports a WebSocket sub-protocol return each supported in an array

So I implemented Ratchet\WebSocket\WsServerInterface as such on my component

public function getSubProtocols()
{
    return ['wamp.2.json', 'wamp.2.msgpack'];
}

Now the connection attempt gets a little further along, but still blows up

failing transport due to protocol violation: unexpected message type 0

Then the client fails to close the connection

Uncaught DOMException: Failed to execute 'close' on 'WebSocket': The code must be either 1000, or between 3000 and 4999. 1002 is neither.

What's going on here; does Ratchet support an older version of the Web Socket protocol, or is there a way to get these two libraries talking successfully?

回答1:

The two libraries are not compatible, per this issue on GitHub.

Autobahn 0.9+ implements wampv2, we have to use autobahn 0.8, and I also don't find it on npm. Instead I find wamp1, but I haven't tested it yet.