How to call sendMessage from the outside WebSocketClientProtocol

#1

I have the WebSocketClientProtocol. Also i have my custom class based on AsyncNotify (http://www.divillo.com/). I need send message to Web Socket server via WebSocketClientFactory when myAsyncNotify.gotNotify(,,_) fired.

Help please. Sorry for my English.

0 Likes

#2

My code:

from autobahn.twisted.websocket import WebSocketClientProtocol, WebSocketClientFactory
from twisted.internet import defer
from AsyncNotify import AsyncNotify

class SyslogPgAsyncListener(AsyncNotify):

def gotNotify(self, pid, notify):
   
    msg =  "got asynchronous notification '%s' from process id '%s'" % (notify, pid)

class SyslogViewerPgNotifer(WebSocketClientProtocol):

def onConnect(self, response):
    print("Server connected: {0}".format(response.peer))

def onOpen(self):
    print("WebSocket connection open.")
   
    #~ http://www.postgresql.org/docs/9.3/static/sql-listen.html
    #~ http://divillo.com/
    #~ https://gist.github.com/pfote/7804540
    pglistener = SyslogPgAsyncListener(dsn)    
    pglistener_run = pglistener.run()
    pglistener.addNotify('qweasd22')
   
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')))

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

if name == ‘main’:

import sys

from twisted.python import log
from twisted.internet import reactor

log.startLogging(sys.stdout)

dsn = '...'

factory = WebSocketClientFactory("ws://localhost:9000", debug = False)
factory.protocol = SyslogViewerPgNotifer

reactor.connectTCP("127.0.0.1", 9000, factory)
reactor.run()

код…

``

0 Likes

#3

try this:

class SyslogPgAsyncListener(AsyncNotify):

     def gotNotify(self, pid, notify):

         msg = "got asynchronous notification '%s' from process id '%s'" % (notify, pid)

class SyslogViewerPgNotifer(WebSocketClientProtocol):

     def onConnect(self, response):
         print("Server connected: {0}".format(response.peer))

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

         #~ http://www.postgresql.org/docs/9.3/static/sql-listen.html
         #~ http://divillo.com/
         #~ https://gist.github.com/pfote/7804540
         pglistener = SyslogPgAsyncListener(dsn)
         pglistener_run = pglistener.run()
         pglistener.addNotify('qweasd22')

     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')))

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

factory._proto = None

···

Am 25.08.2014 15:13, schrieb Михаил Калинин:

My code:

>
fromautobahn.twisted.websocket
importWebSocketClientProtocol,WebSocketClientFactory
fromtwisted.internet importdefer
fromAsyncNotifyimportAsyncNotify

classSyslogPgAsyncListener(AsyncNotify):

defgotNotify(self,pid,notify):

         msg ="got asynchronous notification '%s' from process id
'%s'"%(notify,pid)

classSyslogViewerPgNotifer(WebSocketClientProtocol):

defonConnect(self,response):
print("Server connected: {0}".format(response.peer))

defonOpen(self):
print("WebSocket connection open.")

#~ http://www.postgresql.org/docs/9.3/static/sql-listen.html
#~ http://divillo.com/
#~ https://gist.github.com/pfote/7804540
         pglistener =SyslogPgAsyncListener(dsn)
         pglistener_run =pglistener.run()
         pglistener.addNotify('qweasd22')

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

defonClose(self,wasClean,code,reason):
print("WebSocket connection closed: {0}".format(reason))

if__name__ =='__main__':

importsys

fromtwisted.python importlog
fromtwisted.internet importreactor

     log.startLogging(sys.stdout)

     dsn ='...'

     factory =WebSocketClientFactory("ws://localhost:9000",debug =False)
     factory.protocol =SyslogViewerPgNotifer

     reactor.connectTCP("127.0.0.1",9000,factory)
     reactor.run()
код...

>

--
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>.
To post to this group, send email to autob...@googlegroups.com
<mailto:autob...@googlegroups.com>.
To view this discussion on the web visit
https://groups.google.com/d/msgid/autobahnws/d65dbf5d-b3ec-4dd9-9fa9-a70148d7e064%40googlegroups.com
<https://groups.google.com/d/msgid/autobahnws/d65dbf5d-b3ec-4dd9-9fa9-a70148d7e064%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

0 Likes

#4

sorry, hit send too quick.

try this:

class SyslogPgAsyncListener(AsyncNotify):

     def __init__(self, wsClientFactory):
         self._wsClientFactory = wsClientFactory

     def gotNotify(self, pid, notify):
         if self._wsClientFactory._proto:
            self._wsClientFactory._proto.sendMessage(...)

class SyslogViewerPgNotifer(WebSocketClientProtocol):

     def onOpen(self):
         self.factory._proto = self
         ...

     def onClose(self, wasClean, code, reason):
         self.factory._proto = None

factory._proto = None

···

Am 25.08.2014 15:13, schrieb Михаил Калинин:

My code:

>
fromautobahn.twisted.websocket
importWebSocketClientProtocol,WebSocketClientFactory
fromtwisted.internet importdefer
fromAsyncNotifyimportAsyncNotify

classSyslogPgAsyncListener(AsyncNotify):

defgotNotify(self,pid,notify):

         msg ="got asynchronous notification '%s' from process id
'%s'"%(notify,pid)

classSyslogViewerPgNotifer(WebSocketClientProtocol):

defonConnect(self,response):
print("Server connected: {0}".format(response.peer))

defonOpen(self):
print("WebSocket connection open.")

#~ http://www.postgresql.org/docs/9.3/static/sql-listen.html
#~ http://divillo.com/
#~ https://gist.github.com/pfote/7804540
         pglistener =SyslogPgAsyncListener(dsn)
         pglistener_run =pglistener.run()
         pglistener.addNotify('qweasd22')

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

defonClose(self,wasClean,code,reason):
print("WebSocket connection closed: {0}".format(reason))

if__name__ =='__main__':

importsys

fromtwisted.python importlog
fromtwisted.internet importreactor

     log.startLogging(sys.stdout)

     dsn ='...'

     factory =WebSocketClientFactory("ws://localhost:9000",debug =False)
     factory.protocol =SyslogViewerPgNotifer

     reactor.connectTCP("127.0.0.1",9000,factory)
     reactor.run()
код...

>

--
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>.
To post to this group, send email to autob...@googlegroups.com
<mailto:autob...@googlegroups.com>.
To view this discussion on the web visit
https://groups.google.com/d/msgid/autobahnws/d65dbf5d-b3ec-4dd9-9fa9-a70148d7e064%40googlegroups.com
<https://groups.google.com/d/msgid/autobahnws/d65dbf5d-b3ec-4dd9-9fa9-a70148d7e064%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

0 Likes

#5

Sorry, what is wsClientFactory?

0 Likes

#6

Say, please. What is wsClientFactory?

In help(AsyncNotify) write:

init(self, dsn)
The dsn is passed here. This class requires the psycopg2 driver.

wsClientFactory – dsn ?

···

понедельник, 25 августа 2014 г., 17:53:50 UTC+4 пользователь Tobias Oberstein написал:

sorry, hit send too quick.

try this:

class SyslogPgAsyncListener(AsyncNotify):

 def __init__(self, wsClientFactory):

     self._wsClientFactory = wsClientFactory



 def gotNotify(self, pid, notify):

     if self._wsClientFactory._proto:

        self._wsClientFactory._proto.sendMessage(...)

class SyslogViewerPgNotifer(WebSocketClientProtocol):

 def onOpen(self):

     self.factory._proto = self

     ...



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

     self.factory._proto = None

factory._proto = None

Am 25.08.2014 15:13, schrieb Михаил Калинин:

My code:

fromautobahn.twisted.websocket

importWebSocketClientProtocol,WebSocketClientFactory

fromtwisted.internet importdefer

fromAsyncNotifyimportAsyncNotify

classSyslogPgAsyncListener(AsyncNotify):

defgotNotify(self,pid,notify):

     msg ="got asynchronous notification '%s' from process id

‘%s’"%(notify,pid)

classSyslogViewerPgNotifer(WebSocketClientProtocol):

defonConnect(self,response):

print(“Server connected: {0}”.format(response.peer))

defonOpen(self):

print(“WebSocket connection open.”)

#~ http://www.postgresql.org/docs/9.3/static/sql-listen.html

#~ http://divillo.com/

#~ https://gist.github.com/pfote/7804540

     pglistener =SyslogPgAsyncListener(dsn)
     pglistener_run =pglistener.run()
     pglistener.addNotify('qweasd22')

defonMessage(self,payload,isBinary):

ifisBinary:

print(“Binary message received: {0} bytes”.format(len(payload)))

else:

print(“Text message received: {0}”.format(payload.decode(‘utf8’)))

defonClose(self,wasClean,code,reason):

print(“WebSocket connection closed: {0}”.format(reason))

if__name__ ==‘main’:

importsys

fromtwisted.python importlog

fromtwisted.internet importreactor

 log.startLogging(sys.stdout)
 dsn ='...'
 factory =WebSocketClientFactory("ws://localhost:9000",debug =False)
 factory.protocol =SyslogViewerPgNotifer
 reactor.connectTCP("127.0.0.1",9000,factory)
 reactor.run()

код…

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+unsub...@googlegroups.com.

To post to this group, send email to auto...@googlegroups.com

mailto:autob...@googlegroups.com.

To view this discussion on the web visit

https://groups.google.com/d/msgid/autobahnws/d65dbf5d-b3ec-4dd9-9fa9-a70148d7e064%40googlegroups.com

<https://groups.google.com/d/msgid/autobahnws/d65dbf5d-b3ec-4dd9-9fa9-a70148d7e064%40googlegroups.com?utm_medium=email&utm_source=footer>.

For more options, visit https://groups.google.com/d/optout.

0 Likes

#7

Say, please. What is wsClientFactory?

In help(AsyncNotify) write:

  > __init__(self, dsn)
  > The dsn is passed here. This class requires the psycopg2 driver.

wsClientFactory -- dsn ?

No:

wsClientFactory = WebSocketClientFactory()

You need to forward a reference to this to the _instance_ of

SyslogPgAsyncListener

being created, to be able to bridge PG asynchronous notifications to WebSocket.

···

Am 27.08.2014 09:06, schrieb Михаил Калинин:

понедельник, 25 августа 2014 г., 17:53:50 UTC+4 пользователь Tobias > Oberstein написал:

    sorry, hit send too quick.

    try this:

    class SyslogPgAsyncListener(AsyncNotify):

          def __init__(self, wsClientFactory):
              self._wsClientFactory = wsClientFactory

          def gotNotify(self, pid, notify):
              if self._wsClientFactory._proto:
                 self._wsClientFactory._proto.sendMessage(...)

    class SyslogViewerPgNotifer(WebSocketClientProtocol):

          def onOpen(self):
              self.factory._proto = self
              ...

          def onClose(self, wasClean, code, reason):
              self.factory._proto = None

    factory._proto = None

    Am 25.08.2014 15:13, schrieb Михаил Калинин:
     > My code:
     >
     > >
     > fromautobahn.twisted.websocket
     > importWebSocketClientProtocol,WebSocketClientFactory
     > fromtwisted.internet importdefer
     > fromAsyncNotifyimportAsyncNotify
     >
     > classSyslogPgAsyncListener(AsyncNotify):
     >
     > defgotNotify(self,pid,notify):
     >
     > msg ="got asynchronous notification '%s' from process id
     > '%s'"%(notify,pid)
     >
     > classSyslogViewerPgNotifer(WebSocketClientProtocol):
     >
     > defonConnect(self,response):
     > print("Server connected: {0}".format(response.peer))
     >
     > defonOpen(self):
     > print("WebSocket connection open.")
     >
     > #~ http://www.postgresql.org/docs/9.3/static/sql-listen.html
    <http://www.postgresql.org/docs/9.3/static/sql-listen.html>
     > #~ http://divillo.com/
     > #~ https://gist.github.com/pfote/7804540
    <https://gist.github.com/pfote/7804540>
     > pglistener =SyslogPgAsyncListener(dsn)
     > pglistener_run =pglistener.run()
     > pglistener.addNotify('qweasd22')
     >
     > defonMessage(self,payload,isBinary):
     > ifisBinary:
     > print("Binary message received: {0} bytes".format(len(payload)))
     > else:
     > print("Text message received: {0}".format(payload.decode('utf8')))
     >
     > defonClose(self,wasClean,code,reason):
     > print("WebSocket connection closed: {0}".format(reason))
     >
     > if__name__ =='__main__':
     >
     > importsys
     >
     > fromtwisted.python importlog
     > fromtwisted.internet importreactor
     >
     > log.startLogging(sys.stdout)
     >
     > dsn ='...'
     >
     > factory =WebSocketClientFactory("ws://localhost:9000",debug
    =False)
     > factory.protocol =SyslogViewerPgNotifer
     >
     > reactor.connectTCP("127.0.0.1",9000,factory)
     > reactor.run()
     > код...
     >
     > >
     >
     > --
     > 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 autobah...@googlegroups.com <javascript:>
     > <mailto:autobahnws+...@googlegroups.com <javascript:>>.
     > To post to this group, send email to auto...@googlegroups.com
    <javascript:>
     > <mailto:auto...@googlegroups.com <javascript:>>.
     > To view this discussion on the web visit
     >
    https://groups.google.com/d/msgid/autobahnws/d65dbf5d-b3ec-4dd9-9fa9-a70148d7e064%40googlegroups.com
    <https://groups.google.com/d/msgid/autobahnws/d65dbf5d-b3ec-4dd9-9fa9-a70148d7e064%40googlegroups.com>

     >
    <https://groups.google.com/d/msgid/autobahnws/d65dbf5d-b3ec-4dd9-9fa9-a70148d7e064%40googlegroups.com?utm_medium=email&utm_source=footer
    <https://groups.google.com/d/msgid/autobahnws/d65dbf5d-b3ec-4dd9-9fa9-a70148d7e064%40googlegroups.com?utm_medium=email&utm_source=footer>>.

     > For more options, visit https://groups.google.com/d/optout
    <https://groups.google.com/d/optout>.

--
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>.
To post to this group, send email to autob...@googlegroups.com
<mailto:autob...@googlegroups.com>.
To view this discussion on the web visit
https://groups.google.com/d/msgid/autobahnws/ad1c5a7b-8be9-4e14-9f6d-2617a8401f90%40googlegroups.com
<https://groups.google.com/d/msgid/autobahnws/ad1c5a7b-8be9-4e14-9f6d-2617a8401f90%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

0 Likes