@@ -54,49 +54,7 @@ a Hybrid Connections URL with SAS Tokens utilizing Websockets.
54
54
1 . Ensure your dependency `config.json` and `relaylib.py` are available in your path
55
55
56
56
57
- 2 . Import the dependencies into your `listener.py` script.
58
-
59
- ```python
60
- import asyncio
61
- import json
62
- import logging
63
- import relaylib
64
- import websockets
65
- ```
66
-
67
- 3 . Add the following code to the `listener.py` file. The main script should look like the following code:
68
-
69
- ```python
70
- async def run_application(config):
71
- serviceNamespace = config["namespace"]
72
- entityPath = config["path"]
73
- sasKeyName = config["keyrule"]
74
- sasKey = config["key"]
75
- serviceNamespace += ".servicebus.windows.net"
76
- # Configure logging
77
- logging.basicConfig(level=logging.INFO) # Enable DEBUG/INFO logging as appropriate
78
-
79
- try:
80
- logging.debug("Generating SAS Token for: %s", serviceNamespace)
81
- token = relaylib.createSasToken(serviceNamespace, entityPath, sasKeyName, sasKey)
82
- logging.debug("Generating WebSocket URI")
83
- wssUri = relaylib.createListenUrl(serviceNamespace, entityPath, token)
84
- async with websockets.connect(wssUri) as websocket:
85
- logging.info("Listening for messages on Azure Relay WebSocket...")
86
- while True:
87
- message = await websocket.recv()
88
- logging.info("Received message: %s", message)
89
- except KeyboardInterrupt:
90
- logging.info("Exiting listener.")
91
-
92
- if __name__ == "__main__":
93
- # Load configuration from JSON file
94
- with open("config.json") as config_file:
95
- config = json.load(config_file)
96
-
97
- asyncio.run(run_application(config))
98
- ```
99
- Here's what your `listener.py` file should look like:
57
+ 2 . Here's what your `listener.py` file should look like:
100
58
101
59
```python
102
60
import asyncio
@@ -106,31 +64,31 @@ a Hybrid Connections URL with SAS Tokens utilizing Websockets.
106
64
import websockets
107
65
108
66
async def run_application(config):
109
- serviceNamespace = config["namespace"]
110
- entityPath = config["path"]
111
- sasKeyName = config["keyrule"]
112
- sasKey = config["key"]
113
- serviceNamespace += ".servicebus.windows.net"
114
- # Configure logging
115
- logging.basicConfig(level=logging.INFO) # Enable DEBUG/INFO logging as appropriate
116
-
117
- try:
118
- logging.debug("Generating SAS Token for: %s", serviceNamespace)
119
- token = relaylib.createSasToken(serviceNamespace, entityPath, sasKeyName, sasKey)
120
- logging.debug("Generating WebSocket URI")
121
- wssUri = relaylib.createListenUrl(serviceNamespace, entityPath, token)
122
- async with websockets.connect(wssUri) as websocket:
123
- logging.info("Listening for messages on Azure Relay WebSocket...")
124
- while True:
125
- message = await websocket.recv()
126
- logging.info("Received message: %s", message)
127
- except KeyboardInterrupt:
67
+ serviceNamespace = config["namespace"]
68
+ entityPath = config["path"]
69
+ sasKeyName = config["keyrule"]
70
+ sasKey = config["key"]
71
+ serviceNamespace += ".servicebus.windows.net"
72
+ # Configure logging
73
+ logging.basicConfig(level=logging.INFO) # Enable DEBUG/INFO logging as appropriate
74
+
75
+ try:
76
+ logging.debug("Generating SAS Token for: %s", serviceNamespace)
77
+ token = relaylib.createSasToken(serviceNamespace, entityPath, sasKeyName, sasKey)
78
+ logging.debug("Generating WebSocket URI")
79
+ wssUri = relaylib.createListenUrl(serviceNamespace, entityPath, token)
80
+ async with websockets.connect(wssUri) as websocket:
81
+ logging.info("Listening for messages on Azure Relay WebSocket...")
82
+ while True:
83
+ message = await websocket.recv()
84
+ logging.info("Received message: %s", message)
85
+ except KeyboardInterrupt:
128
86
logging.info("Exiting listener.")
129
87
130
88
if __name__ == "__main__":
131
- # Load configuration from JSON file
132
- with open("config.json") as config_file:
133
- config = json.load(config_file)
89
+ # Load configuration from JSON file
90
+ with open("config.json") as config_file:
91
+ config = json.load(config_file)
134
92
135
- asyncio.run(run_application(config))
93
+ asyncio.run(run_application(config))
136
94
```
0 commit comments