Python Websocket server (Autobahn), Socket.io client

#1

Hello I’m trying to connect a nodeJS client:

var io = require(‘socket.io-client’);

var socket=io.connect('http://192.168.1.40:9001');
socket.on('connect', function () {  });
socket.emit('private message', { user: 'me', msg: 'whazzzup?' });

``

To a Python Autobahn Websocket server:

from autobahn.asyncio.websocket import WebSocketServerProtocol, \
    WebSocketServerFactory

class MyServerProtocol(WebSocketServerProtocol):

    def onConnect(self, request):
        print("Client connecting: {0}".format(request.peer))

    def onOpen(self):
        print("WebSocket connection open.")

    def onMessage(self, payload, isBinary):
        if isBinary:
            print("Binary message received: {0} bytes".format(len(payload)))
        else:
            print("Text message received: {0}".format(payload.decode('utf8')))

        # echo back message verbatim
        self.sendMessage(payload, isBinary)

    def onClose(self, wasClean, code, reason):
        #print "El code es"+code
        #print "La reason es:"+reason
        print("WebSocket connection closed: {0}".format(reason))

if __name__ == '__main__':

    try:
        import asyncio
    except ImportError:
        # Trollius >= 0.3 was renamed
        import trollius as asyncio

    factory = WebSocketServerFactory("ws://localhost:9001", debug=False)
    factory.protocol = MyServerProtocol

    loop = asyncio.get_event_loop()
    coro = loop.create_server(factory, '192.168.1.40', 9001)
    server = loop.run_until_complete(coro)

    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass
    finally:
        server.close()
        loop.close()

``

When I try to run the client I always get:

WebSocket connection closed: connection was closed uncleanly (None)

When I enable debug mode on the server I get:

connection from tcp:192.168.1.40:49011 lost

[('debug', True, 'WebSocketServerFactory'),
 ('debugCodePaths', False, 'WebSocketServerFactory'),
 ('logOctets', True, 'WebSocketServerFactory'),
 ('logFrames', True, 'WebSocketServerFactory'),
 ('trackTimings', False, 'WebSocketServerFactory'),
 ('allowHixie76', False, 'WebSocketServerFactory'),
 ('utf8validateIncoming', True, 'WebSocketServerFactory'),
 ('applyMask', True, 'WebSocketServerFactory'),
 ('maxFramePayloadSize', 0, 'WebSocketServerFactory'),
 ('maxMessagePayloadSize', 0, 'WebSocketServerFactory'),
 ('autoFragmentSize', 0, 'WebSocketServerFactory'),
 ('failByDrop', True, 'WebSocketServerFactory'),
 ('echoCloseCodeReason', False, 'WebSocketServerFactory'),
 ('openHandshakeTimeout', 5, 'WebSocketServerFactory'),
 ('closeHandshakeTimeout', 1, 'WebSocketServerFactory'),
 ('tcpNoDelay', True, 'WebSocketServerFactory'),
 ('autoPingInterval', 0, 'WebSocketServerFactory'),
 ('autoPingTimeout', 0, 'WebSocketServerFactory'),
 ('autoPingSize', 4, 'WebSocketServerFactory'),
 ('versions', [0, 8, 13], 'WebSocketServerFactory'),
 ('webStatus', True, 'WebSocketServerFactory'),
 ('requireMaskedClientFrames', True, 'WebSocketServerFactory'),
 ('maskServerFrames', False, 'WebSocketServerFactory'),
 ('perMessageCompressionAccept',
  <function <lambda> at 0x7fc46297cde8>,
  'WebSocketServerFactory'),
 ('serveFlashSocketPolicy', False, 'WebSocketServerFactory'),
 ('flashSocketPolicy',
  u'<cross-domain-policy>\n     <allow-access-from domain="*" to-ports="*" />\n</cross-domain-policy>\x00',
  'WebSocketServerFactory'),
 ('allowedOrigins', ['*'], 'WebSocketServerFactory'),
 ('allowedOriginsPatterns',
  [<_sre.SRE_Pattern object at 0x7fc46297ce40>],
  'WebSocketServerFactory'),
 ('maxConnections', 0, 'WebSocketServerFactory')]
connection accepted from peer tcp:192.168.1.40:49013
RX Octets from tcp:192.168.1.40:49012 : octets = 474554202f736f636b65742e696f2f3f45494f3d33267472616e73706f72743d706f6c6c696e6726743d313433303134323132363833362d343535266236343d3120485454502f312e310d0a557365722d4167656e743a206e6f64652d584d4c48747470526571756573740d0a4163636570743a202a2f2a0d0a486f73743a203139322e3136382e312e34303a393030310d0a436f6e6e656374696f6e3a20636c6f73650d0a0d0a
received HTTP request:

GET /socket.io/?EIO=3&transport=polling&t=1430142126836-455&b64=1 HTTP/1.1
User-Agent: node-XMLHttpRequest
Accept: */*
Host: 192.168.1.40:9001
Connection: close

received HTTP status line in opening handshake : GET /socket.io/?EIO=3&transport=polling&t=1430142126836-455&b64=1 HTTP/1.1
received HTTP headers in opening handshake : {u'host': u'192.168.1.40:9001', u'connection': u'close', u'accept': u'*/*', u'user-agent': u'node-XMLHttpRequest'}
TX Octets to tcp:192.168.1.40:49012 : sync = False, octets = 485454502f312e3120323030204f4b0d0a5365727665723a204175746f6261686e507974686f6e2f302e31302e330d0a436f6e74656e742d547970653a20746578742f68746d6c3b20636861727365743d5554462d380d0a436f6e74656e742d4c656e6774683a203832300d0a0d0a
TX Octets to tcp:192.168.1.40:49012 : sync = False, octets = 0a3c21444f43545950452068746d6c3e0a3c68746d6c3e0a2020203c686561643e0a2020202020200a2020202020203c7374796c653e0a202020202020202020626f6479207b0a202020202020202020202020636f6c6f723a20236666663b0a2020202020202020202020206261636b67726f756e642d636f6c6f723a20233032376561653b0a202020202020202020202020666f6e742d66616d696c793a20225365676f65205549222c20224c7563696461204772616e6465222c202248656c766574696361204e657565222c2048656c7665746963612c20417269616c2c2073616e732d73657269663b0a202020202020202020202020666f6e742d73697a653a20313670783b0a2020202020202020207d0a0a202020202020202020612c20613a766973697465642c20613a686f766572207b0a202020202020202020202020636f6c6f723a20236666663b0a2020202020202020207d0a2020202020203c2f7374796c653e0a2020203c2f686561643e0a2020203c626f64793e0a2020202020203c68313e4175746f6261686e507974686f6e20302e31302e333c2f68313e0a2020202020203c703e0a2020202020202020204920616d206e6f7420576562207365727665722c206275742061203c623e576562536f636b657420456e64706f696e743c2f623e2e0a2020202020203c2f703e0a2020202020203c703e0a202020202020202020596f752063616e2074616c6b20746f206d65207573696e6720746865203c6120687265663d22687474703a2f2f746f6f6c732e696574662e6f72672f68746d6c2f72666336343535223e576562536f636b65743c2f613e2070726f746f636f6c2e0a2020202020203c2f703e0a2020202020203c703e0a202020202020202020466f72206d6f726520696e666f726d6174696f6e2c20706c65617365207365653a0a2020202020202020203c756c3e0a2020202020202020202020203c6c693e3c6120687265663d22687474703a2f2f6175746f6261686e2e77732f707974686f6e223e4175746f6261686e507974686f6e3c2f613e3c2f6c693e0a2020202020202020203c2f756c3e0a2020202020203c2f703e0a2020203c2f626f64793e0a3c2f68746d6c3e0a
WebSocket connection closed: connection was closed uncleanly (None)
connection from tcp:192.168.1.40:49012 lost
RX Octets from tcp:192.168.1.40:49013 : octets = 474554202f736f636b65742e696f2f3f45494f3d33267472616e73706f72743d706f6c6c696e6726743d313433303134323132363833372d343536266236343d3120485454502f312e310d0a557365722d4167656e743a206e6f64652d584d4c48747470526571756573740d0a4163636570743a202a2f2a0d0a486f73743a203139322e3136382e312e34303a393030310d0a436f6e6e656374696f6e3a20636c6f73650d0a0d0a
received HTTP request:

GET /socket.io/?EIO=3&transport=polling&t=1430142126837-456&b64=1 HTTP/1.1
User-Agent: node-XMLHttpRequest
Accept: */*
Host: 192.168.1.40:9001
Connection: close

``

Is it possible to connect a socket.IO client to a Python Autobahn websocket server?

I have tried to set the websocket server at ws://0.0.0.0:9001 with the same result.

When printing the value of "code" in the onclose method I get 1006 error code.

I use Python 2.76, the latest Autobahn version andsocket.io-client=1.3.5

Thank you for your help and advice.

0 Likes