cleaning and fix chat_wamp
This commit is contained in:
parent
7061b239be
commit
bf0c716f75
@ -1,8 +1,10 @@
|
|||||||
from clientwamp import ClientWamp
|
from iotronic.wamp.clientwamp import ClientWamp
|
||||||
from sys import stdin
|
from sys import stdin
|
||||||
|
import inspect
|
||||||
|
|
||||||
c=ClientWamp('localhost','8181','s4t')
|
c=ClientWamp('localhost','8181','s4t')
|
||||||
c.send('Hello!')
|
c.send('board.connection','Hello from the chat wamp!')
|
||||||
|
print 'USING',inspect.getfile(c.__class__)
|
||||||
while True:
|
while True:
|
||||||
userinput = stdin.readline()
|
userinput = stdin.readline()
|
||||||
c.send(str(userinput))
|
c.send('board.connection',str(userinput))
|
@ -1,90 +0,0 @@
|
|||||||
from autobahn.twisted.wamp import ApplicationRunner
|
|
||||||
from autobahn.twisted.wamp import ApplicationSession
|
|
||||||
from twisted.internet.defer import inlineCallbacks
|
|
||||||
import multiprocessing
|
|
||||||
from autobahn.twisted.util import sleep
|
|
||||||
|
|
||||||
msg_queue=None
|
|
||||||
|
|
||||||
class Publisher(ApplicationSession):
|
|
||||||
|
|
||||||
@inlineCallbacks
|
|
||||||
def onJoin(self, details):
|
|
||||||
print("Publisher session ready")
|
|
||||||
self.publish(u'board.connection', 'counter')
|
|
||||||
'''
|
|
||||||
global msg_queue
|
|
||||||
while True:
|
|
||||||
if not msg_queue.empty():
|
|
||||||
msg=msg_queue.get()
|
|
||||||
self.publish(u'board.connection', msg)
|
|
||||||
yield sleep(1)
|
|
||||||
'''
|
|
||||||
|
|
||||||
class Subscriber(ApplicationSession):
|
|
||||||
@inlineCallbacks
|
|
||||||
def onJoin(self, details):
|
|
||||||
print("Subscriber session ready")
|
|
||||||
|
|
||||||
def oncounter(count):
|
|
||||||
print("event received: {0}", count)
|
|
||||||
try:
|
|
||||||
yield self.subscribe(oncounter, u'board.connection')
|
|
||||||
print("subscribed to topic")
|
|
||||||
except Exception as e:
|
|
||||||
print("could not subscribe to topic: {0}".format(e))
|
|
||||||
|
|
||||||
self.publish(u'board.connection', 'counter')
|
|
||||||
global msg_queue
|
|
||||||
while True:
|
|
||||||
if not msg_queue.empty():
|
|
||||||
msg=msg_queue.get()
|
|
||||||
self.publish(u'board.connection', msg)
|
|
||||||
yield sleep(0.01)
|
|
||||||
|
|
||||||
|
|
||||||
class PublisherClient:
|
|
||||||
def __init__(self,ip,port,realm):
|
|
||||||
self.ip=unicode(ip)
|
|
||||||
self.port=unicode(port)
|
|
||||||
self.realm=unicode(realm)
|
|
||||||
self._url = "ws://"+self.ip+":"+self.port+"/ws"
|
|
||||||
self.runner = ApplicationRunner(url=unicode(self._url), realm=self.realm,
|
|
||||||
#debug=True, debug_wamp=True, debug_app=True
|
|
||||||
)
|
|
||||||
|
|
||||||
def start(self):
|
|
||||||
# Pass start_reactor=False to all runner.run() calls
|
|
||||||
self.runner.run(Publisher, start_reactor=False)
|
|
||||||
|
|
||||||
|
|
||||||
class SubscriberClient:
|
|
||||||
def __init__(self,ip,port,realm):
|
|
||||||
self.ip=unicode(ip)
|
|
||||||
self.port=unicode(port)
|
|
||||||
self.realm=unicode(realm)
|
|
||||||
self._url = "ws://"+self.ip+":"+self.port+"/ws"
|
|
||||||
self.runner = ApplicationRunner(url=unicode(self._url), realm=self.realm,
|
|
||||||
#debug=True, debug_wamp=True, debug_app=True
|
|
||||||
)
|
|
||||||
|
|
||||||
def start(self):
|
|
||||||
# Pass start_reactor=False to all runner.run() calls
|
|
||||||
self.runner.run(Subscriber, start_reactor=False)
|
|
||||||
|
|
||||||
class ClientWamp:
|
|
||||||
|
|
||||||
def __init__(self,ip,port,realm):
|
|
||||||
server = SubscriberClient(ip,port,realm)
|
|
||||||
sendMessage = PublisherClient(ip,port,realm)
|
|
||||||
server.start()
|
|
||||||
sendMessage.start()
|
|
||||||
|
|
||||||
from twisted.internet import reactor
|
|
||||||
global msg_queue
|
|
||||||
msg_queue = multiprocessing.Queue()
|
|
||||||
multi = multiprocessing.Process(target=reactor.run, args=())
|
|
||||||
multi.start()
|
|
||||||
|
|
||||||
def send(self,msg):
|
|
||||||
msg_queue.put(msg)
|
|
Loading…
Reference in New Issue
Block a user