def test_auth_configured(self): with self.assertRaises(TypeError): hub = HubConnectionBuilder()\ .with_url(self.server_url, options={ "verify_ssl": False, "headers": { "mycustomheader": "mycustomheadervalue" } }) hub.has_auth_configured = True hub.options["access_token_factory"] = "" conn = hub.build()
def test_enable_trace(self): hub = HubConnectionBuilder()\ .with_url(self.server_url, options={"verify_ssl":False})\ .configure_logging(logging.WARNING, socket_trace=True)\ .with_automatic_reconnect({ "type": "raw", "keep_alive_interval": 10, "reconnect_interval": 5, "max_attempts": 5 })\ .build() hub.on_open(self.on_open) hub.on_close(self.on_close) hub.start() self.assertTrue(websocket.isEnabledForDebug()) websocket.enableTrace(False) hub.stop()
def _setUp(self, msgpack=False): builder = HubConnectionBuilder()\ .with_url(self.server_url, options={ "verify_ssl": False, "access_token_factory": self.login, "headers": { "mycustomheader": "mycustomheadervalue" } }) if msgpack: builder.with_hub_protocol(MessagePackHubProtocol()) builder.configure_logging(logging.WARNING)\ .with_automatic_reconnect({ "type": "raw", "keep_alive_interval": 10, "reconnect_interval": 5, "max_attempts": 5 }) self.connection = builder.build() self.connection.on("ReceiveMessage", self.receive_message) self.connection.on_open(self.on_open) self.connection.on_close(self.on_close) self._lock = threading.Lock() self.assertTrue(self._lock.acquire(timeout=30)) self.connection.start()
def get_connection(self, msgpack=False): builder = HubConnectionBuilder()\ .with_url(self.server_url, options={"verify_ssl":False})\ .configure_logging(logging.ERROR)\ .with_automatic_reconnect({ "type": "raw", "keep_alive_interval": 10, "reconnect_interval": 5, "max_attempts": 5 }) if msgpack: builder.with_hub_protocol(MessagePackHubProtocol()) hub = builder.build() hub.on_open(self.on_open) hub.on_close(self.on_close) return hub
def test_bad_auth_function(self): with self.assertRaises(TypeError): self.connection = HubConnectionBuilder()\ .with_url(self.server_url, options={ "verify_ssl": False, "access_token_factory": 1234, "headers": { "mycustomheader": "mycustomheadervalue" } })
def test_reconnect_interval(self): connection = HubConnectionBuilder()\ .with_url(self.server_url, options={"verify_ssl": False})\ .configure_logging(logging.ERROR)\ .with_automatic_reconnect({ "type": "interval", "intervals": [1, 2, 4, 45, 6, 7, 8, 9, 10], "keep_alive_interval": 3 })\ .build() self.reconnect_test(connection)
def test_raw_reconnection(self): connection = HubConnectionBuilder()\ .with_url(self.server_url, options={"verify_ssl": False})\ .configure_logging(logging.ERROR)\ .with_automatic_reconnect({ "type": "raw", "keep_alive_interval": 10, "max_attempts": 4 })\ .build() self.reconnect_test(connection)
def test_start(self): connection = HubConnectionBuilder()\ .with_url(self.server_url, options={"verify_ssl": False})\ .configure_logging(logging.ERROR)\ .build() _lock = threading.Lock() self.assertTrue(_lock.acquire(timeout=30)) connection.on_open(lambda: _lock.release()) connection.on_close(lambda: _lock.release()) result = connection.start() self.assertTrue(result) self.assertTrue(_lock.acquire(timeout=30)) # Released on open result = connection.start() self.assertFalse(result) connection.stop()
def test_reconnect_interval_config(self): connection = HubConnectionBuilder()\ .with_url(self.server_url, options={"verify_ssl": False})\ .configure_logging(logging.ERROR)\ .with_automatic_reconnect({ "type": "interval", "intervals": [1, 2, 4, 45, 6, 7, 8, 9, 10] })\ .build() _lock = threading.Lock() connection.on_open(lambda: _lock.release()) connection.on_close(lambda: _lock.release()) self.assertTrue(_lock.acquire(timeout=10)) connection.start() self.assertTrue(_lock.acquire(timeout=10)) connection.stop() self.assertTrue(_lock.acquire(timeout=10)) del _lock
def _test_send(self, msgpack=False): builder = HubConnectionBuilder()\ .with_url(self.server_url, options={ "verify_ssl": False, "access_token_factory": self.login, }) if msgpack: builder.with_hub_protocol(MessagePackHubProtocol()) builder.configure_logging(logging.ERROR) self.connection = builder.build() self.connection.on_open(self.on_open) self.connection.on_close(self.on_close) self.assertRaises(requests.exceptions.ConnectionError, lambda: self.connection.start())
def _get_client(self) -> BaseHubConnection: """Create SignalR client, register message callbacks""" if self._client is None: self._logger.info('Creating websocket client') self._client = (HubConnectionBuilder().with_url( self._url + '/v1/events').with_automatic_reconnect({ "type": "raw", "keep_alive_interval": 10, "reconnect_interval": 5, "max_attempts": 5, })).build() self._client.on_open(self._on_connect) self._client.on_error(self._on_error) self._client.on('operations', self._on_operation_message) self._client.on('bigmaps', self._on_big_map_message) return self._client
def input_with_default(input_text, default_value): value = input(input_text.format(default_value)) return default_value if value is None or value.strip() == "" else value server_url = input_with_default('Enter your server url(default: {0}): ', "wss://localhost:5001/chatHub") username = input_with_default('Enter your username (default: {0}): ', "mandrewcito") handler = logging.StreamHandler() handler.setLevel(logging.DEBUG) hub_connection = HubConnectionBuilder()\ .with_url(server_url, options={"verify_ssl": False}) \ .configure_logging(logging.DEBUG, socket_trace=True, handler=handler) \ .with_automatic_reconnect({ "type": "interval", "keep_alive_interval": 10, "intervals": [1, 3, 5, 6, 7, 87, 3] }).build() hub_connection.on_open(lambda: print( "connection opened and handshake received ready to send messages")) hub_connection.on_close(lambda: print("connection closed")) hub_connection.on("ReceiveMessage", print) hub_connection.start() message = None # Do login while message != "exit()":
server_url = input_with_default('Enter your server url(default: {0}): ', "localhost:7071/api") username = input_with_default('Enter your username (default: {0}): ', "mandrewcito") handler = logging.StreamHandler() handler.setLevel(logging.DEBUG) formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) hub_connection = HubConnectionBuilder() \ .with_url("ws://"+server_url, options={ "verify_ssl": False, "skip_negotiation": False, "headers": { } }) \ .configure_logging(logging.DEBUG, socket_trace=True, handler=handler) \ .build() hub_connection.on_open(lambda: print( "connection opened and handshake received ready to send messages")) hub_connection.on_close(lambda: print("connection closed")) hub_connection.on("newMessage", print) hub_connection.start() message = None # Do login
def input_with_default(input_text, default_value): value = input(input_text.format(default_value)) return default_value if value is None or value.strip() == "" else value server_url = input_with_default('Enter your server url(default: {0}): ', "wss://localhost:5001/chatHub") handler = logging.StreamHandler() handler.setLevel(logging.DEBUG) hub_connection = HubConnectionBuilder()\ .with_url(server_url, options={"verify_ssl": False}) \ .configure_logging(logging.DEBUG, socket_trace=True, handler=handler) \ .with_automatic_reconnect({ "type": "interval", "keep_alive_interval": 10, "intervals": [1, 3, 5, 6, 7, 87, 3] }).build() hub_connection.on_open(lambda: print( "connection opened and handshake received ready to send messages")) hub_connection.on_close(lambda: print( "connection closed>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<")) hub_connection.on_error(lambda err: print("errrrrrrrrrrrrrrrrrrr")) hub_connection.on("ThrowExceptionCall", lambda x: print(f">>>{x}")) hub_connection.start() message = None # Do login
import threading import logging import sys import time sys.path.append("./") from aiosignalrcore.hub_connection_builder import HubConnectionBuilder connection = HubConnectionBuilder()\ .with_url("wss://localhost:5001/chathub", options={"verify_ssl": False})\ .configure_logging(logging.ERROR)\ .build() _lock = threading.Lock() connection.on_open(lambda: _lock.release()) connection.on_close(lambda: _lock.release()) connection.on("ReceiveMessage", lambda _: _lock.release()) (_lock.acquire(timeout=30)) # Released on open connection.start() (_lock.acquire(timeout=30)) # Released on ReOpen connection.send("DisconnectMe", []) time.sleep(30)
def signalr_core_example_login(url, user, username_password): response = requests.post(url, json={"username": user, "password": username_password}, verify=False) return response.json()["token"] login_url = input_with_default('Enter your server login url({0}):', "https://localhost:5001/users/authenticate") server_url = input_with_default('Enter your server url(default: {0}): ', "wss://localhost:5001/authHub") username = input_with_default('Enter your username (default: {0}): ', "test") password = input_with_default('Enter your password (default: {0}): ', "test") hub_connection = HubConnectionBuilder()\ .configure_logging(logging_level=logging.DEBUG)\ .with_url(server_url, options={ "access_token_factory": lambda: signalr_core_example_login(login_url, username, password), "verify_ssl": False }).with_automatic_reconnect({ "type": "interval", "keep_alive_interval": 10, "intervals": [1, 3, 5, 6, 7, 87, 3] })\ .build() hub_connection.on_open(lambda: print("connection opened and handshake received ready to send messages")) hub_connection.on_close(lambda: print("connection closed")) hub_connection.on("ReceiveSystemMessage", print) hub_connection.on("ReceiveChatMessage", print) hub_connection.on("ReceiveDirectMessage", print) hub_connection.start() message = None
login_url = input_with_default('Enter your server login url({0}):', "http://*****:*****@mandrewcito.com") password = input_with_default('Enter your password (default: {0}): ', "Abc123.--123?") hub_connection = HubConnectionBuilder()\ .with_url(server_url, options={ "access_token_factory": lambda: signalr_core_example_login(login_url, username, password) }).with_automatic_reconnect({ "type": "interval", "keep_alive_interval": 10, "intervals": [1, 3, 5, 6, 7, 87, 3] })\ .build() hub_connection.on("ReceiveSystemMessage", print_message) hub_connection.on("ReceiveChatMessage", print_message) hub_connection.on("ReceiveDirectMessage", print_message) hub_connection.start() message = None while message != "exit()": message = raw_input(">> ") if message is not None and message is not "" and message is not "exit()": hub_connection.send("Send", [message]) hub_connection.stop()
def test_no_reconnect(self): connection = HubConnectionBuilder()\ .with_url(self.server_url, options={"verify_ssl": False})\ .configure_logging(logging.ERROR)\ .build() _lock = threading.Lock() _lock.acquire(timeout=10) connection.on_open(lambda: _lock.release()) connection.on("ReceiveMessage", lambda _: _lock.release()) connection.start() self.assertTrue(_lock.acquire(timeout=10)) # Released on ReOpen connection.send("DisconnectMe", []) self.assertTrue(_lock.acquire(timeout=10)) time.sleep(10) self.assertRaises(HubConnectionError, lambda: connection.send("DisconnectMe", [])) connection.stop() del _lock
def test_bad_url(self): with self.assertRaises(ValueError): self.connection = HubConnectionBuilder()\ .with_url("")
import time from aiosignalrcore.hub_connection_builder import HubConnectionBuilder def input_with_default(input_text, default_value): value = input(input_text.format(default_value)) return default_value if value is None or value.strip() == "" else value server_url = input_with_default('Enter your server url(default: {0}): ', "ws://localhost:62342/chathub") username = input_with_default('Enter your username (default: {0}): ', "mandrewcito") hub_connection = HubConnectionBuilder()\ .with_url(server_url)\ .configure_logging(logging.DEBUG)\ .build() hub_connection.on_open(lambda: print( "connection opened and handshake received ready to send messages")) hub_connection.on_close(lambda: reconnect) def reconnect(): print("connection closed") time.sleep(20) print("try reconnect") hub_connection.start() hub_connection.on("ReceiveMessage", print)
def test_bad_options(self): with self.assertRaises(TypeError): self.connection = HubConnectionBuilder()\ .with_url(self.server_url, options=["ssl", True])
from aiosignalrcore.subject import Subject def input_with_default(input_text, default_value): value = input(input_text.format(default_value)) return default_value if value is None or value.strip() == "" else value server_url = input_with_default('Enter your server url(default: {0}): ', "wss://localhost:5001/chatHub") hub_connection = HubConnectionBuilder()\ .with_url(server_url, options={"verify_ssl": False}) \ .configure_logging(logging.DEBUG) \ .with_automatic_reconnect({ "type": "interval", "keep_alive_interval": 10, "intervals": [1, 3, 5, 6, 7, 87, 3] })\ .build() hub_connection.start() time.sleep(10) def bye(error, x): if error: print("error {0}".format(x)) else: print("complete! ") global hub_connection hub_connection.stop()
import sys sys.path.append("./") from aiosignalrcore.hub_connection_builder import HubConnectionBuilder import logging def input_with_default(input_text, default_value): value = input(input_text.format(default_value)) return default_value if value is None or value.strip() == "" else value server_url = input_with_default('Enter your server url(default: {0}): ', "wss://localhost:5001/chatHub") hub_connection = HubConnectionBuilder().with_url(server_url, options={"verify_ssl": False}) \ .configure_logging(logging.DEBUG, socket_trace=True) \ .build() hub_connection.start() time.sleep(10) end = False def bye(error, x): global end end = True if error: print("error {0}".format(x)) else: print("complete! ") global hub_connection