onMessage never called on server

#1

I followed bits of https://github.com/crossbario/autobahn-python/tree/master/examples/twisted/websocket/echo_wsgi and read about how to make a Twisted web websockets Resource. But onMessage on the server is never called.

I can send messages from the server to the browser (from with onOpen). But I can’t seem to send messages from the browser to the server. Any ideas?

Here’s my server.py

from klein import Klein

from twisted.python import log

from autobahn.twisted.websocket import WebSocketServerFactory

from autobahn.twisted.websocket import WebSocketServerProtocol

from autobahn.twisted.resource import WebSocketResource

class EchoServerProtocol(WebSocketServerProtocol):

def onOpen(self):

    print 'open'

    self.sendMessage('hey')

def onMessage(self, payload, isBinary=False):

    print 'got message', payload, isBinary

    log.msg('heeeeeelo?')

    self.sendMessage(payload, isBinary)

def onClose(self, wasClean, code, reason):

    print 'close', wasClean, code, reason

class Application(object):

app = Klein()

def __init__(self, ws_url):

    wsFactory = WebSocketServerFactory(ws_url)

    wsFactory.protocol = EchoServerProtocol

    self.wsResource = WebSocketResource(wsFactory)

@app.route('/')

def index(self, request):

    return open('index.html', 'rb').read()

@app.route('/test')

def test(self, request):

    return open('test.html', 'rb').read()

@app.route('/ws')

def ws(self, request):

    print 'inside /ws'

    return self.wsResource

if name == ‘main’:

port = 5000

host = '127.0.0.1'

ws_url = u'ws://{host}:{port}'.format(**locals())

app = Application(ws_url)

app.app.run(host, port)

``

and here’s test.html

``

Here’s my versions:

autobahn==0.14.1

backports.ssl-match-hostname==3.5.0.1

cached-property==1.3.0

docker-compose==1.7.1

docker-py==1.8.1

dockerpty==0.4.1

docopt==0.6.2

enum34==1.1.6

functools32==3.2.3.post2

ipaddress==1.0.16

jsonschema==2.5.1

klein==15.3.1

PyYAML==3.11

requests==2.7.0

six==1.10.0

texttable==0.8.4

Twisted==16.3.0

txaio==2.5.1

websocket-client==0.37.0

Werkzeug==0.11.10

wheel==0.24.0

zope.interface==4.2.0

``

And my browser is Chrome Version 51.0.2704.103 (64-bit)

0 Likes

#2

I defined dataReceived on the EchoServerProtocol to see what data was being received, and modified the messages being sent so the code looks more like this now:

class EchoServerProtocol(WebSocketServerProtocol):

def onOpen(self):

print ‘open’

self.sendMessage(‘hello from python onOpen’)

def dataReceived(self, data):

print ‘dataReceived: {0!r}’.format(data)

return WebSocketServerProtocol.dataReceived(self, data)

def onMessage(self, payload, isBinary=False):

print ‘got message’, payload, isBinary

self.sendMessage(payload, isBinary)

self.sendMessage(‘hello from python onMessage’)

def onClose(self, wasClean, code, reason):

print ‘close’, wasClean, code, reason

``

And JS:

sock.onopen = function() {

backoff = 1;

console.log("Connected to " + wsuri);

sock.send(‘hello from js’);

setTimeout(function() {

sock.send(‘hello from timeout’);

}, 1000);

}

``

According to Chrome, the data was sent:

But I never see any message data received in the server log:

$ python server.py

2016-07-14 18:06:42-0600 [-] Log opened.

2016-07-14 18:06:42-0600 [-] Site starting on 5000

2016-07-14 18:06:42-0600 [-] Starting factory <twisted.web.server.Site instance at 0x10ddf87e8>

2016-07-14 18:06:44-0600 [-] “127.0.0.1” - - [15/Jul/2016:00:06:43 +0000] “GET /test HTTP/1.1” 200 1230 “-” “Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36”

2016-07-14 18:06:44-0600 [-] inside /ws

2016-07-14 18:06:44-0600 [-] dataReceived: ‘GET /ws HTTP/1.1\r\nOrigin: http://127.0.0.1:5000\r\nUpgrade: websocket\r\nAccept-Language: en-US,en;q=0.8,ro;q=0.6,it;q=0.4\r\nAccept-Encoding: gzip, deflate, sdch\r\nSec-Websocket-Version: 13\r\nConnection: Upgrade\r\nSec-Websocket-Key: PeUdfsK2vmvFTfpvuLTcnQ==\r\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36\r\nHost: 127.0.0.1:5000\r\nPragma: no-cache\r\nCache-Control: no-cache\r\nSec-Websocket-Extensions: permessage-deflate; client_max_window_bits\r\n\r\n’

2016-07-14 18:06:44-0600 [-] open

``

···

On Thursday, July 14, 2016 at 4:12:14 PM UTC-6, Matt Haggard wrote:

I followed bits of https://github.com/crossbario/autobahn-python/tree/master/examples/twisted/websocket/echo_wsgi and read about how to make a Twisted web websockets Resource. But onMessage on the server is never called.

I can send messages from the server to the browser (from with onOpen). But I can’t seem to send messages from the browser to the server. Any ideas?

Here’s my server.py

from klein import Klein

from twisted.python import log

from autobahn.twisted.websocket import WebSocketServerFactory

from autobahn.twisted.websocket import WebSocketServerProtocol

from autobahn.twisted.resource import WebSocketResource

class EchoServerProtocol(WebSocketServerProtocol):

def onOpen(self):
    print 'open'
    self.sendMessage('hey')
def onMessage(self, payload, isBinary=False):
    print 'got message', payload, isBinary
    log.msg('heeeeeelo?')
    self.sendMessage(payload, isBinary)
def onClose(self, wasClean, code, reason):
    print 'close', wasClean, code, reason

class Application(object):

app = Klein()
def __init__(self, ws_url):
    wsFactory = WebSocketServerFactory(ws_url)
    wsFactory.protocol = EchoServerProtocol
    self.wsResource = WebSocketResource(wsFactory)
@app.route('/')
def index(self, request):
    return open('index.html', 'rb').read()
@app.route('/test')
def test(self, request):
    return open('test.html', 'rb').read()
@app.route('/ws')
def ws(self, request):
    print 'inside /ws'
    return self.wsResource

if name == ‘main’:

port = 5000
host = '127.0.0.1'
ws_url = u'ws://{host}:{port}'.format(**locals())
app = Application(ws_url)
app.app.run(host, port)

``

and here’s test.html

``

Here’s my versions:

autobahn==0.14.1

backports.ssl-match-hostname==3.5.0.1

cached-property==1.3.0

docker-compose==1.7.1

docker-py==1.8.1

dockerpty==0.4.1

docopt==0.6.2

enum34==1.1.6

functools32==3.2.3.post2

ipaddress==1.0.16

jsonschema==2.5.1

klein==15.3.1

PyYAML==3.11

requests==2.7.0

six==1.10.0

texttable==0.8.4

Twisted==16.3.0

txaio==2.5.1

websocket-client==0.37.0

Werkzeug==0.11.10

wheel==0.24.0

zope.interface==4.2.0

``

And my browser is Chrome Version 51.0.2704.103 (64-bit)

0 Likes

#3

And using tcpdump, I see that no messages go from the client to the server. What am I doing wrong?

tcpdump.txt (11.7 KB)

···

On Thursday, July 14, 2016 at 4:12:14 PM UTC-6, Matt Haggard wrote:

I followed bits of https://github.com/crossbario/autobahn-python/tree/master/examples/twisted/websocket/echo_wsgi and read about how to make a Twisted web websockets Resource. But onMessage on the server is never called.

I can send messages from the server to the browser (from with onOpen). But I can’t seem to send messages from the browser to the server. Any ideas?

Here’s my server.py

from klein import Klein

from twisted.python import log

from autobahn.twisted.websocket import WebSocketServerFactory

from autobahn.twisted.websocket import WebSocketServerProtocol

from autobahn.twisted.resource import WebSocketResource

class EchoServerProtocol(WebSocketServerProtocol):

def onOpen(self):
    print 'open'
    self.sendMessage('hey')
def onMessage(self, payload, isBinary=False):
    print 'got message', payload, isBinary
    log.msg('heeeeeelo?')
    self.sendMessage(payload, isBinary)
def onClose(self, wasClean, code, reason):
    print 'close', wasClean, code, reason

class Application(object):

app = Klein()
def __init__(self, ws_url):
    wsFactory = WebSocketServerFactory(ws_url)
    wsFactory.protocol = EchoServerProtocol
    self.wsResource = WebSocketResource(wsFactory)
@app.route('/')
def index(self, request):
    return open('index.html', 'rb').read()
@app.route('/test')
def test(self, request):
    return open('test.html', 'rb').read()
@app.route('/ws')
def ws(self, request):
    print 'inside /ws'
    return self.wsResource

if name == ‘main’:

port = 5000
host = '127.0.0.1'
ws_url = u'ws://{host}:{port}'.format(**locals())
app = Application(ws_url)
app.app.run(host, port)

``

and here’s test.html

``

Here’s my versions:

autobahn==0.14.1

backports.ssl-match-hostname==3.5.0.1

cached-property==1.3.0

docker-compose==1.7.1

docker-py==1.8.1

dockerpty==0.4.1

docopt==0.6.2

enum34==1.1.6

functools32==3.2.3.post2

ipaddress==1.0.16

jsonschema==2.5.1

klein==15.3.1

PyYAML==3.11

requests==2.7.0

six==1.10.0

texttable==0.8.4

Twisted==16.3.0

txaio==2.5.1

websocket-client==0.37.0

Werkzeug==0.11.10

wheel==0.24.0

zope.interface==4.2.0

``

And my browser is Chrome Version 51.0.2704.103 (64-bit)

0 Likes

#4

I have exact same problem on Chrome 51, 64bit OS.
I am running Autobahn 0.14.3 with twisted version of 16.3.0

···

On Friday, July 15, 2016 at 3:42:14 AM UTC+5:30, Matt Haggard wrote:

I followed bits of https://github.com/crossbario/autobahn-python/tree/master/examples/twisted/websocket/echo_wsgi and read about how to make a Twisted web websockets Resource. But onMessage on the server is never called.

I can send messages from the server to the browser (from with onOpen). But I can’t seem to send messages from the browser to the server. Any ideas?

Here’s my server.py

from klein import Klein

from twisted.python import log

from autobahn.twisted.websocket import WebSocketServerFactory

from autobahn.twisted.websocket import WebSocketServerProtocol

from autobahn.twisted.resource import WebSocketResource

class EchoServerProtocol(WebSocketServerProtocol):

def onOpen(self):
    print 'open'
    self.sendMessage('hey')
def onMessage(self, payload, isBinary=False):
    print 'got message', payload, isBinary
    log.msg('heeeeeelo?')
    self.sendMessage(payload, isBinary)
def onClose(self, wasClean, code, reason):
    print 'close', wasClean, code, reason

class Application(object):

app = Klein()
def __init__(self, ws_url):
    wsFactory = WebSocketServerFactory(ws_url)
    wsFactory.protocol = EchoServerProtocol
    self.wsResource = WebSocketResource(wsFactory)
@app.route('/')
def index(self, request):
    return open('index.html', 'rb').read()
@app.route('/test')
def test(self, request):
    return open('test.html', 'rb').read()
@app.route('/ws')
def ws(self, request):
    print 'inside /ws'
    return self.wsResource

if name == ‘main’:

port = 5000
host = '127.0.0.1'
ws_url = u'ws://{host}:{port}'.format(**locals())
app = Application(ws_url)
app.app.run(host, port)

``

and here’s test.html

``

Here’s my versions:

autobahn==0.14.1

backports.ssl-match-hostname==3.5.0.1

cached-property==1.3.0

docker-compose==1.7.1

docker-py==1.8.1

dockerpty==0.4.1

docopt==0.6.2

enum34==1.1.6

functools32==3.2.3.post2

ipaddress==1.0.16

jsonschema==2.5.1

klein==15.3.1

PyYAML==3.11

requests==2.7.0

six==1.10.0

texttable==0.8.4

Twisted==16.3.0

txaio==2.5.1

websocket-client==0.37.0

Werkzeug==0.11.10

wheel==0.24.0

zope.interface==4.2.0

``

And my browser is Chrome Version 51.0.2704.103 (64-bit)

0 Likes

#5

When I try broadcast example i.e. https://github.com/crossbario/autobahn-python/tree/master/examples/twisted/websocket/broadcast, the onMessage works fine.

I think there is some bug when the websocket factory is used as a websocket resource.

···

On Friday, July 15, 2016 at 5:54:03 PM UTC+5:30, Chetan Naik wrote:

I have exact same problem on Chrome 51, 64bit OS.
I am running Autobahn 0.14.3 with twisted version of 16.3.0

On Friday, July 15, 2016 at 3:42:14 AM UTC+5:30, Matt Haggard wrote:

I followed bits of https://github.com/crossbario/autobahn-python/tree/master/examples/twisted/websocket/echo_wsgi and read about how to make a Twisted web websockets Resource. But onMessage on the server is never called.

I can send messages from the server to the browser (from with onOpen). But I can’t seem to send messages from the browser to the server. Any ideas?

Here’s my server.py

from klein import Klein

from twisted.python import log

from autobahn.twisted.websocket import WebSocketServerFactory

from autobahn.twisted.websocket import WebSocketServerProtocol

from autobahn.twisted.resource import WebSocketResource

class EchoServerProtocol(WebSocketServerProtocol):

def onOpen(self):
    print 'open'
    self.sendMessage('hey')
def onMessage(self, payload, isBinary=False):
    print 'got message', payload, isBinary
    log.msg('heeeeeelo?')
    self.sendMessage(payload, isBinary)
def onClose(self, wasClean, code, reason):
    print 'close', wasClean, code, reason

class Application(object):

app = Klein()
def __init__(self, ws_url):
    wsFactory = WebSocketServerFactory(ws_url)
    wsFactory.protocol = EchoServerProtocol
    self.wsResource = WebSocketResource(wsFactory)
@app.route('/')
def index(self, request):
    return open('index.html', 'rb').read()
@app.route('/test')
def test(self, request):
    return open('test.html', 'rb').read()
@app.route('/ws')
def ws(self, request):
    print 'inside /ws'
    return self.wsResource

if name == ‘main’:

port = 5000
host = '127.0.0.1'
ws_url = u'ws://{host}:{port}'.format(**locals())
app = Application(ws_url)
app.app.run(host, port)

``

and here’s test.html

``

Here’s my versions:

autobahn==0.14.1

backports.ssl-match-hostname==3.5.0.1

cached-property==1.3.0

docker-compose==1.7.1

docker-py==1.8.1

dockerpty==0.4.1

docopt==0.6.2

enum34==1.1.6

functools32==3.2.3.post2

ipaddress==1.0.16

jsonschema==2.5.1

klein==15.3.1

PyYAML==3.11

requests==2.7.0

six==1.10.0

texttable==0.8.4

Twisted==16.3.0

txaio==2.5.1

websocket-client==0.37.0

Werkzeug==0.11.10

wheel==0.24.0

zope.interface==4.2.0

``

And my browser is Chrome Version 51.0.2704.103 (64-bit)

0 Likes

#6

Might be related to this issue: https://github.com/crossbario/autobahn-python/issues/701

···

On Thursday, July 14, 2016 at 3:12:14 PM UTC-7, Matt Haggard wrote:

I followed bits of https://github.com/crossbario/autobahn-python/tree/master/examples/twisted/websocket/echo_wsgi and read about how to make a Twisted web websockets Resource. But onMessage on the server is never called.

I can send messages from the server to the browser (from with onOpen). But I can’t seem to send messages from the browser to the server. Any ideas?

Here’s my server.py

from klein import Klein

from twisted.python import log

from autobahn.twisted.websocket import WebSocketServerFactory

from autobahn.twisted.websocket import WebSocketServerProtocol

from autobahn.twisted.resource import WebSocketResource

class EchoServerProtocol(WebSocketServerProtocol):

def onOpen(self):
    print 'open'
    self.sendMessage('hey')
def onMessage(self, payload, isBinary=False):
    print 'got message', payload, isBinary
    log.msg('heeeeeelo?')
    self.sendMessage(payload, isBinary)
def onClose(self, wasClean, code, reason):
    print 'close', wasClean, code, reason

class Application(object):

app = Klein()
def __init__(self, ws_url):
    wsFactory = WebSocketServerFactory(ws_url)
    wsFactory.protocol = EchoServerProtocol
    self.wsResource = WebSocketResource(wsFactory)
@app.route('/')
def index(self, request):
    return open('index.html', 'rb').read()
@app.route('/test')
def test(self, request):
    return open('test.html', 'rb').read()
@app.route('/ws')
def ws(self, request):
    print 'inside /ws'
    return self.wsResource

if name == ‘main’:

port = 5000
host = '127.0.0.1'
ws_url = u'ws://{host}:{port}'.format(**locals())
app = Application(ws_url)
app.app.run(host, port)

``

and here’s test.html

``

Here’s my versions:

autobahn==0.14.1

backports.ssl-match-hostname==3.5.0.1

cached-property==1.3.0

docker-compose==1.7.1

docker-py==1.8.1

dockerpty==0.4.1

docopt==0.6.2

enum34==1.1.6

functools32==3.2.3.post2

ipaddress==1.0.16

jsonschema==2.5.1

klein==15.3.1

PyYAML==3.11

requests==2.7.0

six==1.10.0

texttable==0.8.4

Twisted==16.3.0

txaio==2.5.1

websocket-client==0.37.0

Werkzeug==0.11.10

wheel==0.24.0

zope.interface==4.2.0

``

And my browser is Chrome Version 51.0.2704.103 (64-bit)

0 Likes

#7

Lance,

Yep, that was the problem. Using Twisted 16.2.0 works great :slight_smile:

Thank you!

Matt

···

On Friday, July 15, 2016 at 9:45:49 AM UTC-6, Lance Kurisaki wrote:

Might be related to this issue: https://github.com/crossbario/autobahn-python/issues/701

On Thursday, July 14, 2016 at 3:12:14 PM UTC-7, Matt Haggard wrote:

I followed bits of https://github.com/crossbario/autobahn-python/tree/master/examples/twisted/websocket/echo_wsgi and read about how to make a Twisted web websockets Resource. But onMessage on the server is never called.

I can send messages from the server to the browser (from with onOpen). But I can’t seem to send messages from the browser to the server. Any ideas?

Here’s my server.py

from klein import Klein

from twisted.python import log

from autobahn.twisted.websocket import WebSocketServerFactory

from autobahn.twisted.websocket import WebSocketServerProtocol

from autobahn.twisted.resource import WebSocketResource

class EchoServerProtocol(WebSocketServerProtocol):

def onOpen(self):
    print 'open'
    self.sendMessage('hey')
def onMessage(self, payload, isBinary=False):
    print 'got message', payload, isBinary
    log.msg('heeeeeelo?')
    self.sendMessage(payload, isBinary)
def onClose(self, wasClean, code, reason):
    print 'close', wasClean, code, reason

class Application(object):

app = Klein()
def __init__(self, ws_url):
    wsFactory = WebSocketServerFactory(ws_url)
    wsFactory.protocol = EchoServerProtocol
    self.wsResource = WebSocketResource(wsFactory)
@app.route('/')
def index(self, request):
    return open('index.html', 'rb').read()
@app.route('/test')
def test(self, request):
    return open('test.html', 'rb').read()
@app.route('/ws')
def ws(self, request):
    print 'inside /ws'
    return self.wsResource

if name == ‘main’:

port = 5000
host = '127.0.0.1'
ws_url = u'ws://{host}:{port}'.format(**locals())
app = Application(ws_url)
app.app.run(host, port)

``

and here’s test.html

``

Here’s my versions:

autobahn==0.14.1

backports.ssl-match-hostname==3.5.0.1

cached-property==1.3.0

docker-compose==1.7.1

docker-py==1.8.1

dockerpty==0.4.1

docopt==0.6.2

enum34==1.1.6

functools32==3.2.3.post2

ipaddress==1.0.16

jsonschema==2.5.1

klein==15.3.1

PyYAML==3.11

requests==2.7.0

six==1.10.0

texttable==0.8.4

Twisted==16.3.0

txaio==2.5.1

websocket-client==0.37.0

Werkzeug==0.11.10

wheel==0.24.0

zope.interface==4.2.0

``

And my browser is Chrome Version 51.0.2704.103 (64-bit)

0 Likes

#8

the latest master from github has rectified this issue.

https://github.com/crossbario/autobahn-python/commit/64693e4693eeeda50bc781bdce4d4dba3172ebf5

···

On Friday, July 15, 2016 at 10:03:46 PM UTC+5:30, Matt Haggard wrote:

Lance,

Yep, that was the problem. Using Twisted 16.2.0 works great :slight_smile:

Thank you!

Matt

On Friday, July 15, 2016 at 9:45:49 AM UTC-6, Lance Kurisaki wrote:

Might be related to this issue: https://github.com/crossbario/autobahn-python/issues/701

On Thursday, July 14, 2016 at 3:12:14 PM UTC-7, Matt Haggard wrote:

I followed bits of https://github.com/crossbario/autobahn-python/tree/master/examples/twisted/websocket/echo_wsgi and read about how to make a Twisted web websockets Resource. But onMessage on the server is never called.

I can send messages from the server to the browser (from with onOpen). But I can’t seem to send messages from the browser to the server. Any ideas?

Here’s my server.py

from klein import Klein

from twisted.python import log

from autobahn.twisted.websocket import WebSocketServerFactory

from autobahn.twisted.websocket import WebSocketServerProtocol

from autobahn.twisted.resource import WebSocketResource

class EchoServerProtocol(WebSocketServerProtocol):

def onOpen(self):
    print 'open'
    self.sendMessage('hey')
def onMessage(self, payload, isBinary=False):
    print 'got message', payload, isBinary
    log.msg('heeeeeelo?')
    self.sendMessage(payload, isBinary)
def onClose(self, wasClean, code, reason):
    print 'close', wasClean, code, reason

class Application(object):

app = Klein()
def __init__(self, ws_url):
    wsFactory = WebSocketServerFactory(ws_url)
    wsFactory.protocol = EchoServerProtocol
    self.wsResource = WebSocketResource(wsFactory)
@app.route('/')
def index(self, request):
    return open('index.html', 'rb').read()
@app.route('/test')
def test(self, request):
    return open('test.html', 'rb').read()
@app.route('/ws')
def ws(self, request):
    print 'inside /ws'
    return self.wsResource

if name == ‘main’:

port = 5000
host = '127.0.0.1'
ws_url = u'ws://{host}:{port}'.format(**locals())
app = Application(ws_url)
app.app.run(host, port)

``

and here’s test.html

``

Here’s my versions:

autobahn==0.14.1

backports.ssl-match-hostname==3.5.0.1

cached-property==1.3.0

docker-compose==1.7.1

docker-py==1.8.1

dockerpty==0.4.1

docopt==0.6.2

enum34==1.1.6

functools32==3.2.3.post2

ipaddress==1.0.16

jsonschema==2.5.1

klein==15.3.1

PyYAML==3.11

requests==2.7.0

six==1.10.0

texttable==0.8.4

Twisted==16.3.0

txaio==2.5.1

websocket-client==0.37.0

Werkzeug==0.11.10

wheel==0.24.0

zope.interface==4.2.0

``

And my browser is Chrome Version 51.0.2704.103 (64-bit)

0 Likes