-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add data plane commands to az webpubsub #3318
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6f2f916
Update data plane data
zackliu c2ab1fa
Fix linter style
zackliu 624eb26
Specify dependencies
zackliu 4d51f42
Remove test
zackliu ddb59b8
Update
zackliu 61ff90b
More updates
zackliu 9b1e208
Fix linter
zackliu 3200e49
Some updates according to comments
zackliu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
# -------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for license information. | ||
# -------------------------------------------------------------------------------------------- | ||
# pylint: disable=line-too-long | ||
|
||
import asyncio | ||
import sys | ||
import threading | ||
import json | ||
import websockets | ||
from .vendored_sdks.azure_messaging_webpubsubservice import ( | ||
build_authentication_token | ||
) | ||
|
||
|
||
HELP_MESSAGE = """ | ||
----------Usage----------- | ||
help : Print help messages | ||
joingroup <group-name> : Join the connection to group | ||
leavegroup <group-name> : Leave the connection from group | ||
sendtogroup <group-name> <message> : Send message to group | ||
event <event-name> <message> : Send event to event handler | ||
-------------------------- | ||
""" | ||
|
||
|
||
async def connect(url): | ||
async with websockets.connect(url, subprotocols=['json.webpubsub.azure.v1']) as ws: | ||
|
||
eprint(HELP_MESSAGE) | ||
publisher = Publisher(ws) | ||
publisher.daemon = True | ||
publisher.start() | ||
while True: | ||
print(await ws.recv()) | ||
|
||
|
||
def start_client(client, resource_group_name, webpubsub_name, hub_name): | ||
keys = client.list_keys(resource_group_name, webpubsub_name) | ||
connection_string = keys.primary_connection_string | ||
token = build_authentication_token(connection_string, hub_name, roles=['webpubsub.sendToGroup', 'webpubsub.joinLeaveGroup']) | ||
asyncio.get_event_loop().run_until_complete(connect(token['url'])) | ||
|
||
|
||
def eprint(*args, **kwargs): | ||
print(*args, file=sys.stderr, **kwargs) | ||
|
||
|
||
class Publisher(threading.Thread): | ||
def __init__(self, ws): | ||
threading.Thread.__init__(self) | ||
self.ws = ws | ||
self.id = 0 | ||
|
||
def run(self): | ||
new_loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(new_loop) | ||
while True: | ||
input_line = sys.stdin.readline().strip() | ||
asyncio.get_event_loop().run_until_complete(self._parse(input_line)) | ||
|
||
def join(self, timeout=None): | ||
super().join() | ||
|
||
async def _parse(self, input_line): | ||
if input_line: | ||
if input_line.strip() == 'help': | ||
eprint(HELP_MESSAGE) | ||
return | ||
|
||
arr = input_line.split(maxsplit=1) | ||
if len(arr) != 2: | ||
eprint('Invalid input "{}", use help to show usage'.format(input_line)) | ||
return | ||
|
||
command = arr[0] | ||
if command.lower() == 'joingroup': | ||
group = arr[1] | ||
payload = json.dumps({ | ||
'type': 'joinGroup', | ||
'group': group, | ||
'ackId': self._get_ack_id() | ||
}) | ||
await self.ws.send(payload) | ||
|
||
elif command.lower() == 'leavegroup': | ||
group = arr[1] | ||
payload = json.dumps({ | ||
'type': 'leaveGroup', | ||
'group': group, | ||
'ackId': self._get_ack_id() | ||
}) | ||
await self.ws.send(payload) | ||
|
||
elif command.lower() == 'sendtogroup': | ||
arr = arr[1].split(maxsplit=1) | ||
group = arr[0] | ||
data = arr[1] | ||
payload = json.dumps({ | ||
'type': 'sendToGroup', | ||
'group': group, | ||
'data': data, | ||
'ackId': self._get_ack_id() | ||
}) | ||
await self.ws.send(payload) | ||
|
||
elif command.lower() == 'event': | ||
arr = arr[1].split(maxsplit=1) | ||
event = arr[0] | ||
data = arr[1] | ||
payload = json.dumps({ | ||
'type': 'event', | ||
'event': event, | ||
'data': data | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm curious about why event payload doesn't need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From current webpubsub's spec, event doesn't support |
||
}) | ||
await self.ws.send(payload) | ||
|
||
else: | ||
eprint('Invalid input "{}", use help to show usage'.format(input_line)) | ||
|
||
def _get_ack_id(self): | ||
self.id = self.id + 1 | ||
return self.id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a user is typing messages and ws received something, it will breaking the input line in terminal display.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, but it seems not easy to make it excellence. I think it's acceptable when it's used as a debugging tool.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Maybe it's not hard. You can make a switch between typing context and display context by enter key. When user in typing context, you can hold the received data form ws. And print them after switch to display context.