1
我已經經歷了許多論壇和網站,但沒有找到任何可以解決我的問題的解決方案。如何通過python autobahn/twisted發送有效載荷給特定用戶
我有這個server.py文件:
from autobahn.twisted.websocket import WebSocketServerProtocol, \
WebSocketServerFactory
class MyServerProtocol(WebSocketServerProtocol):
def onConnect(self, request):
print("Client connecting: {0}".format(request.peer))
def onOpen(self):
print("WebSocket connection open.")
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')))
print("Text message received: {0}".format(self.peer))
# echo back message verbatim
self.sendMessage(payload, isBinary)
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)
factory = WebSocketServerFactory(u"ws://127.0.0.1:9000")
factory.protocol = MyServerProtocol
# factory.setProtocolOptions(maxConnections=2)
# note to self: if using putChild, the child must be bytes...
reactor.listenTCP(9000, factory)
reactor.run()
我想要做的就是裏面的onMessage我想從客戶端接收有效載荷,然後發送有效載荷到另一個客戶端,我不希望將有效負載回送給同一個客戶端。
目前我可以成功接收有效載荷。但是,如何將有效載荷發送給不同的客戶端?
我在許多網站上看到類似的問題,但沒有一個幫助。