1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
import os, time
from wslink import register as exportRpc
from wslink.websocket import LinkProtocol
from wslink import schedule_callback
MESSAGE_LIST = [
"Nice to meet you",
"What's your name?",
"Mine is wslink.py",
]
class PubSubAPI(LinkProtocol):
def __init__(self, **kwargs):
super(PubSubAPI, self).__init__()
self.topic = "wslink.communication.channel"
self.msgIdx = 0
self.keepTalking = True
self.frequency = 5 # 5 seconds
self.startTalking()
def saySomething(self):
if not self.keepTalking:
return
if self.msgIdx + 1 < len(MESSAGE_LIST):
self.msgIdx += 1
else:
self.msgIdx = 0
self.publish(self.topic, MESSAGE_LIST[self.msgIdx])
if self.keepTalking:
schedule_callback(self.frequency, lambda: self.saySomething())
@exportRpc("wslink.say.hello")
def sayHello(self, message):
print("sayHello", message)
msgToPost = "py server: %s" % message
self.publish(self.topic, msgToPost)
return msgToPost
@exportRpc("wslink.start.talking")
def startTalking(self):
self.keepTalking = True
self.saySomething()
@exportRpc("wslink.stop.talking")
def stopTalking(self):
self.keepTalking = False
self.msgIdx = 0
|