test-client cleanup
This commit is contained in:
parent
b829cae940
commit
317b5a8dae
46
ws_client.py
46
ws_client.py
|
@ -1,11 +1,12 @@
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
import sys
|
||||||
|
|
||||||
from twisted.internet import endpoints
|
from twisted.internet import endpoints
|
||||||
from twisted.internet.defer import (
|
from twisted.internet.defer import (
|
||||||
Deferred,
|
Deferred,
|
||||||
inlineCallbacks,
|
inlineCallbacks,
|
||||||
)
|
)
|
||||||
from twisted.internet.task import react
|
from twisted.internet.task import react, deferLater
|
||||||
from twisted.internet.error import (
|
from twisted.internet.error import (
|
||||||
ConnectionDone,
|
ConnectionDone,
|
||||||
)
|
)
|
||||||
|
@ -26,49 +27,64 @@ from autobahn.websocket import types
|
||||||
class RelayEchoClient(WebSocketClientProtocol):
|
class RelayEchoClient(WebSocketClientProtocol):
|
||||||
|
|
||||||
def onOpen(self):
|
def onOpen(self):
|
||||||
self.data = b""
|
self._received = b""
|
||||||
self.sendMessage(u"please relay {}".format(self.factory.token).encode("ascii"), True)
|
self.sendMessage(
|
||||||
|
u"please relay {} for side {}".format(
|
||||||
def onConnecting(self, details):
|
self.factory.token,
|
||||||
return types.ConnectingRequest(
|
self.factory.side,
|
||||||
protocols=["transit_relay"],
|
).encode("ascii"),
|
||||||
|
True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# def onConnecting(self, details):
|
||||||
|
# return types.ConnectingRequest(
|
||||||
|
# protocols=["binary"],
|
||||||
|
# )
|
||||||
|
|
||||||
def onMessage(self, data, isBinary):
|
def onMessage(self, data, isBinary):
|
||||||
print(">onMessage: {} bytes".format(len(data)))
|
print(">onMessage: {} bytes".format(len(data)))
|
||||||
print(data, isBinary)
|
print(data, isBinary)
|
||||||
if data == b"ok\n":
|
if data == b"ok\n":
|
||||||
self.factory.ready.callback(None)
|
self.factory.ready.callback(None)
|
||||||
else:
|
else:
|
||||||
self.data += data
|
self._received += data
|
||||||
return True
|
# return False
|
||||||
|
|
||||||
def onClose(self, wasClean, code, reason):
|
def onClose(self, wasClean, code, reason):
|
||||||
print(">onClose", wasClean, code, reason)
|
print(">onClose", wasClean, code, reason)
|
||||||
self.factory.done.callback(reason)
|
self.factory.done.callback(reason)
|
||||||
|
if not self.factory.ready.called:
|
||||||
|
self.factory.ready.errback(RuntimeError(reason))
|
||||||
|
|
||||||
|
|
||||||
@react
|
@react
|
||||||
@inlineCallbacks
|
@inlineCallbacks
|
||||||
def main(reactor):
|
def main(reactor):
|
||||||
|
will_send_message = len(sys.argv) > 1
|
||||||
ep = endpoints.clientFromString(reactor, "tcp:localhost:4002")
|
ep = endpoints.clientFromString(reactor, "tcp:localhost:4002")
|
||||||
f = WebSocketClientFactory("ws://127.0.0.1:4002/")
|
f = WebSocketClientFactory("ws://127.0.0.1:4002/")
|
||||||
|
f.reactor = reactor
|
||||||
f.protocol = RelayEchoClient
|
f.protocol = RelayEchoClient
|
||||||
|
## f.protocols = ["binary"]
|
||||||
# NB: write our own factory, probably..
|
# NB: write our own factory, probably..
|
||||||
f.token = "a" * 64
|
f.token = "a" * 64
|
||||||
|
f.side = "0" * 16 if will_send_message else "1" * 16
|
||||||
f.done = Deferred()
|
f.done = Deferred()
|
||||||
f.ready = Deferred()
|
f.ready = Deferred()
|
||||||
proto = yield ep.connect(f)
|
proto = yield ep.connect(f)
|
||||||
# proto_d = ep.connect(f)
|
# proto_d = ep.connect(f)
|
||||||
# print("proto_d", proto_d)
|
# print("proto_d", proto_d)
|
||||||
# proto = yield proto_d
|
# proto = yield proto_d
|
||||||
print("proto", proto, f.done)
|
print("proto", proto)
|
||||||
yield f.ready
|
yield f.ready
|
||||||
print("ready")
|
print("ready")
|
||||||
import sys
|
if will_send_message:
|
||||||
if len(sys.argv) > 2:
|
for _ in range(5):
|
||||||
proto.sendMessage(b"it's a message", True)
|
print("sending message")
|
||||||
|
proto.sendMessage(b"it's a message", True)
|
||||||
|
yield deferLater(reactor, 0.2)
|
||||||
yield proto.sendClose()
|
yield proto.sendClose()
|
||||||
|
print("closing")
|
||||||
yield f.done
|
yield f.done
|
||||||
print("relayed {} bytes:".format(len(proto.data)))
|
print("relayed {} bytes:".format(len(proto._received)))
|
||||||
print(proto.data.decode("utf8"))
|
print(proto._received.decode("utf8"))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user