Autobahn Python GTK Client --> issue: sending message triggered by a button

#1

Dear Autobahn - Fans / Contributors / Developers,

I’m working on a GTK-based client to receive/send messages via a websocket connection using autobahn (GTK is not important, PyQT, etc. is fine too)
Starting, stopping & echo of the websocket works, but I cannot manage to send a message triggered by a button in line:

===> self._websockethandler._factory.protocol.sendMessage(“Hi from Button!”)

I get the error message: TypeError: unbound method sendMessage() must be called with EchoClientProtocol

so it seems the class is not instantiated, but I’ve received the first echos before I pressed the button (so the connection is established and works)

I’d appreciate any hint how to solve this…

Thank you in advance!

···

My strucuture is like that:

class GuiApp(Gtk.Application):

class Handler:

def init(self, websockethandler):

self._websockethandler = websockethandler

def on_button_clicked(self, *args):

self._websockethandler._factory.protocol.sendMessage(“Hi from Button!”) <===== THIS DOESN’T WORK (PLEASE HELP)

def init(self):

Gtk.Application.init(self, application_id=“xxx”, flags=Gio.ApplicationFlags.FLAGS_NONE)

self.connect(“activate”, self.on_activate)

self._websockethandler = WebSocketHandler()

self._websockethandler.start_websocket(self, True, False)

def on_activate(self, data=None):

builder.connect_signals(self.Handler(self._websockethandler))

class WebSocketHandler():

class EchoClientProtocol(WebSocketClientProtocol):

def sendHello(self):

self.sendMessage(“Hello, world!”)

def onOpen(self):

self.sendHello()

def onMessage(self, msg, binary):

print "Got echo: " + msg

reactor.callLater(3, self.sendHello)

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

WebSocketClientProtocol.onClose(self, wasClean, code, reason)

def start_websocket(self, app, wss=False, debug=False):

url = …

self._factory = WebSocketClientFactory(url,

debug=debug,

debugCodePaths=debug)

self._factory.protocol = self.EchoClientProtocol

connectWS(self._factory)

def stop_websocket(self):

if name == “main”:

GuiApp()

0 Likes

#2

Hi,

Dear Autobahn - Fans / Contributors / Developers,

I'm working on a GTK-based client to receive/send messages via a
websocket connection using autobahn (GTK is not important, PyQT, etc. is
fine too)
Starting, stopping & echo of the websocket works, but I cannot manage to
send a message triggered by a button in line:
  ===> self._websockethandler._factory.protocol.sendMessage("Hi from
Button!")

This doesn't work, since WebSocketFactory.protocol is the _class_ of protocol instances created, but you need the _instance_.

Since with a client, there will be at most 1 active protocol instance per factory, one option is to do

WebSocketProtocol.onOpen:
self.factory.proto = self

WebSocketProtocol.onClose
self.factory.proto = None

And then
factory.proto.sendMessage()

···

Am 23.04.2014 18:47, schrieb Juee Wo:

===

The 2nd issue is that you need to run under a appropriate Twisted reactor for UI integration .. you might follow

http://twistedmatrix.com/documents/current/core/howto/choosing-reactor.html

Hope this helps,
/Tobias

I get the error message: TypeError: unbound method sendMessage() must be
called with EchoClientProtocol

so it seems the class is not instantiated, but I've received the first
echos before I pressed the button (so the connection is established and
works)
I'd appreciate any hint how to solve this...

Thank you in advance!

---------------------------------------------------------------
My strucuture is like that:

class GuiApp(Gtk.Application):

     class Handler:
         def __init__(self, websockethandler):
             self._websockethandler = websockethandler

         def on_button_clicked(self, *args):
             self._websockethandler._factory.protocol.sendMessage("Hi
from Button!") <===== THIS DOESN'T WORK (PLEASE HELP)

     def __init__(self):
         Gtk.Application.__init__(self, application_id="xxx",
flags=Gio.ApplicationFlags.FLAGS_NONE)
         self.connect("activate", self.on_activate)
         self._websockethandler = WebSocketHandler()
         self._websockethandler.start_websocket(self, True, False)

     def on_activate(self, data=None):
         ....
         builder.connect_signals(self.Handler(self._websockethandler))
         ....

class WebSocketHandler():

     class EchoClientProtocol(WebSocketClientProtocol):

         def sendHello(self):
             self.sendMessage("Hello, world!")

         def onOpen(self):
             self.sendHello()
         def onMessage(self, msg, binary):
             print "Got echo: " + msg
             reactor.callLater(3, self.sendHello)

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

     def start_websocket(self, app, wss=False, debug=False):
         url = ....
         self._factory = WebSocketClientFactory(url,
                                         debug=debug,
                                         debugCodePaths=debug)
         self._factory.protocol = self.EchoClientProtocol
         connectWS(self._factory)

     def stop_websocket(self):
         ...

if __name__ == "__main__":
     GuiApp()

--
You received this message because you are subscribed to the Google
Groups "Autobahn" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to autobahnws+...@googlegroups.com
<mailto:autobahnws+...@googlegroups.com>.
For more options, visit https://groups.google.com/d/optout.

0 Likes

#3

Dear Tobias,

thank you very much for your support!!!

Your hint solved the problem. I was aware about the instance/object issue, but I didn’t think about keeping the instance inside the factory … so that solved my problem and saved my day/week :slight_smile:

The reactor with GTK works, I’m using GTK+ 3.

Thank you very much again!!!

Best, Juergen

···
0 Likes