How to call sendMessage from the outside WebSocketClientProtocol

405 views
Skip to first unread message

Михаил Калинин

unread,
Aug 25, 2014, 9:04:23 AM8/25/14
to autob...@googlegroups.com
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.

Михаил Калинин

unread,
Aug 25, 2014, 9:13:27 AM8/25/14
to autob...@googlegroups.com
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()
 
код...

Tobias Oberstein

unread,
Aug 25, 2014, 9:51:10 AM8/25/14
to autob...@googlegroups.com
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):
> --
> 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.

Tobias Oberstein

unread,
Aug 25, 2014, 9:53:50 AM8/25/14
to autob...@googlegroups.com
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 ='...'
>

Михаил Калинин

unread,
Aug 26, 2014, 9:31:50 AM8/26/14
to autob...@googlegroups.com
Sorry, what is wsClientFactory?

Михаил Калинин

unread,
Aug 27, 2014, 3:06:27 AM8/27/14
to autob...@googlegroups.com
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 написал:

Tobias Oberstein

unread,
Aug 27, 2014, 9:08:35 AM8/27/14
to autob...@googlegroups.com
Am 27.08.2014 09:06, schrieb Михаил Калинин:
> 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.
> > an email to autobahnws+...@googlegroups.com <javascript:>
> > <mailto:autobahnws+...@googlegroups.com <javascript:>>.
> > To post to this group, send email to autob...@googlegroups.com
> <javascript:>
> > <mailto:autob...@googlegroups.com <javascript:>>.
> <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/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>.
Reply all
Reply to author
Forward
0 new messages