ratchetphp / Ratchet

Asynchronous WebSocket server
http://socketo.me
MIT License
6.29k stars 747 forks source link

PHP ratchet server and python cliente: is it possible? #237

Closed fryederich closed 10 years ago

fryederich commented 10 years ago

I'm a beginner, there is a php-ratchet server using WampServiceInterface and I'd like to make a python client that subscribes to a topic on this server. It is possible?

cboden commented 10 years ago

Yup. Any WebSocket server and client that speak the standard RFC6455 protocol can work together.

fryederich commented 10 years ago

Thank you cboden (also for adding label). Some details: python 3.3 with asyncio and autobahn 0.9.1, Because server uses WampServiceInterface, I've written a wamp client script following this example https://github.com/tavendo/AutobahnPython/blob/master/examples/asyncio/wamp/beginner/client.py The connection to server is established but it drops after the handshake. The output is: [('debug', True, 'WampWebSocketClientFactory'), ('debugCodePaths', False, 'WampWebSocketClientFactory'), ('logOctets', True, 'WampWebSocketClientFactory'), ('logFrames', True, 'WampWebSocketClientFactory'), ('trackTimings', False, 'WampWebSocketClientFactory'), ('allowHixie76', False, 'WampWebSocketClientFactory'), ('utf8validateIncoming', True, 'WampWebSocketClientFactory'), ('applyMask', True, 'WampWebSocketClientFactory'), ('maxFramePayloadSize', 0, 'WampWebSocketClientFactory'), ('maxMessagePayloadSize', 0, 'WampWebSocketClientFactory'), ('autoFragmentSize', 0, 'WampWebSocketClientFactory'), ('failByDrop', True, 'WampWebSocketClientFactory'), ('echoCloseCodeReason', False, 'WampWebSocketClientFactory'), ('openHandshakeTimeout', 5, 'WampWebSocketClientFactory'), ('closeHandshakeTimeout', 1, 'WampWebSocketClientFactory'), ('tcpNoDelay', True, 'WampWebSocketClientFactory'), ('autoPingInterval', 0, 'WampWebSocketClientFactory'), ('autoPingTimeout', 0, 'WampWebSocketClientFactory'), ('autoPingSize', 4, 'WampWebSocketClientFactory'), ('version', 18, 'WampWebSocketClientFactory'), ('acceptMaskedServerFrames', False, 'WampWebSocketClientFactory'), ('maskClientFrames', True, 'WampWebSocketClientFactory'), ('serverConnectionDropTimeout', 1, 'WampWebSocketClientFactory'), ('perMessageCompressionOffers', [], 'WampWebSocketClientFactory'), ('perMessageCompressionAccept', <function WebSocketClientFactory.resetProtocolOptions.. at 0x00000000031619D8>, 'WampWebSocketClientFactory')] connection to 192.168.100.218:44444 established TX Octets to 192.168.100.218:44444 : sync = False, octets = b'474554202f20485454502f312e310d0a557365722d4167656e743a204175746f6261686e507974686f6e2f302e392e310d0a486f73743a206c6f63616c686f73743a38300d0a557067726164653a20576562536f636b65740d0a436f6e6e656374696f6e3a20557067726164650d0a507261676d613a206e6f2d63616368650d0a43616368652d436f6e74726f6c3a206e6f2d63616368650d0a5365632d576562536f636b65742d4b65793a204f4d753955674b565442697375665741796246332b413d3d0d0a5365632d576562536f636b65742d50726f746f636f6c3a2077616d702e322e6a736f6e2e626174636865642c77616d702e322e6a736f6e0d0a5365632d576562536f636b65742d56657273696f6e3a2031330d0a0d0a' GET / HTTP/1.1

User-Agent: AutobahnPython/0.9.1

Host: localhost:80

Upgrade: WebSocket

Connection: Upgrade

Pragma: no-cache

Cache-Control: no-cache

Sec-WebSocket-Key: OMu9UgKVTBisufWAybF3+A==

Sec-WebSocket-Protocol: wamp.2.json.batched,wamp.2.json

Sec-WebSocket-Version: 13

RX Octets from 192.168.100.218:44444 : octets = b'485454502f312e312031303120537769746368696e672050726f746f636f6c730d0a557067726164653a20776562736f636b65740d0a436f6e6e656374696f6e3a20557067726164650d0a5365632d576562536f636b65742d4163636570743a2049332f372f7745784b626d65705975796835444177384d683936593d0d0a582d506f77657265642d42793a20526174636865742f302e322e370d0a0d0a81265b302c2235343235363164383232613866222c312c22526174636865745c2f302e322e37225d' received HTTP response:

b'HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: I3/7/wExKbmepYuyh5DAw8Mh96Y=\r\nX-Powered-By: Ratchet/0.2.7\r\n\r\n'

received HTTP status line in opening handshake : HTTP/1.1 101 Switching Protocols received HTTP headers in opening handshake : {'connection': 'Upgrade', 'upgrade': 'websocket', 'sec-websocket-accept': 'I3/7/wExKbmepYuyh5DAw8Mh96Y=', 'x-powered-by': 'Ratchet/0.2.7'} WAMP-over-WebSocket transport lost: wasClean = False, code = 1006, reason = 'connection was closed uncleanly (I failed the WebSocket connection by dropping the TCP connection)' Traceback (most recent call last): File "C:\Python33\lib\site-packages\autobahn-0.9.1-py3.3.egg\autobahn\wamp\websocket.py", line 75, in onClose self._session.onClose(wasClean) AttributeError: 'WampWebSocketClientProtocol' object has no attribute '_session' connection to 192.168.100.218:44444 lost

cboden commented 10 years ago

It looks like the version of Autobahn you're using only speaks version 2 of the WAMP protocol.

Sec-WebSocket-Protocol: wamp.2.json.batched,wamp.2.json

Ratchet speaks version 1. Can you try using a version of Autobahn with the 0.8.x tag?

fryederich commented 10 years ago

Yes, it works!!!! thank!

But I need python 3.3: wamp v1 can be used with Twisted only, and Twisted is only for python 2.7. In your opinion, opening a python33 websocket connection and re-implementing the wamp1 specification is hard (basically I have to subscribe to a topic and receive events)? Any other suggestion?

cboden commented 10 years ago

You could attempt to write the protocol parser for your client - it's basic JSON. Alternatively you could take a look at Thruway - it supports WAMPv2 server using Ratchet as the underlying transport.