-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Python websocket extension and test case
- Loading branch information
Showing
25 changed files
with
404 additions
and
12 deletions.
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
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...ket_server_extension_nodejs/manifest.json → ...ons/websocket_server_nodejs/manifest.json
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
31 changes: 31 additions & 0 deletions
31
packages/example_extensions/websocket_server_python/BUILD.gn
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,31 @@ | ||
# | ||
# This file is part of TEN Framework, an open source project. | ||
# Licensed under the Apache License, Version 2.0. | ||
# See the LICENSE file for more information. | ||
# | ||
import("//build/feature/ten_package.gni") | ||
import("//build/ten_runtime/feature/publish.gni") | ||
import("//build/ten_runtime/glob.gni") | ||
import("//build/ten_runtime/options.gni") | ||
|
||
ten_package("websocket_server_python") { | ||
package_kind = "extension" | ||
|
||
resources = [ | ||
"__init__.py", | ||
"main.py", | ||
"manifest.json", | ||
"property.json", | ||
"requirements.txt", | ||
] | ||
|
||
deps = [ "//core/src/ten_runtime" ] | ||
} | ||
|
||
if (ten_enable_ten_manager) { | ||
ten_package_publish("upload_websocket_server_python_to_server") { | ||
base_dir = rebase_path( | ||
"${root_out_dir}/ten_packages/extension/websocket_server_python") | ||
deps = [ ":websocket_server_python" ] | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/example_extensions/websocket_server_python/LICENSE
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,13 @@ | ||
Copyright © 2025 Agora | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. |
3 changes: 3 additions & 0 deletions
3
packages/example_extensions/websocket_server_python/__init__.py
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,3 @@ | ||
from . import main | ||
|
||
print("websocket server extension loaded") |
70 changes: 70 additions & 0 deletions
70
packages/example_extensions/websocket_server_python/main.py
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,70 @@ | ||
# | ||
# This file is part of TEN Framework, an open source project. | ||
# Licensed under the Apache License, Version 2.0. | ||
# See the LICENSE file for more information. | ||
# | ||
import websockets | ||
from ten import ( | ||
Addon, | ||
AsyncExtension, | ||
register_addon_as_extension, | ||
TenEnv, | ||
Cmd, | ||
CmdResult, | ||
StatusCode, | ||
AsyncTenEnv, | ||
) | ||
|
||
|
||
class WebsocketServerExtension(AsyncExtension): | ||
def __init__(self, name: str) -> None: | ||
super().__init__(name) | ||
self.name = name | ||
|
||
async def on_init(self, ten_env: AsyncTenEnv) -> None: | ||
self.ten_env = ten_env | ||
|
||
async def echo(self, websocket, path): | ||
async for message in websocket: | ||
print(f"Received message: {message}") | ||
# Echo the message back to the client | ||
await websocket.send(f"Server received: {message}") | ||
|
||
async def on_start(self, ten_env: AsyncTenEnv) -> None: | ||
ten_env.log_debug("on_start") | ||
|
||
try: | ||
self.server_port = await ten_env.get_property_int("server_port") | ||
except Exception as e: | ||
ten_env.log_error( | ||
"Could not read 'server_port' from properties." + str(e) | ||
) | ||
self.server_port = 8002 | ||
|
||
self.server = websockets.serve(self.echo, "localhost", self.server_port) | ||
self.ten_env.log_debug( | ||
f"Websocket server started on port {self.server_port}" | ||
) | ||
|
||
await self.server | ||
print("Websocket server started.") | ||
|
||
async def on_deinit(self, ten_env: AsyncTenEnv) -> None: | ||
ten_env.log_debug("on_deinit") | ||
|
||
async def on_cmd(self, ten_env: AsyncTenEnv, cmd: Cmd) -> None: | ||
ten_env.log_debug("on_cmd") | ||
|
||
# Not supported command. | ||
await ten_env.return_result(CmdResult.create(StatusCode.ERROR), cmd) | ||
|
||
async def on_stop(self, ten_env: AsyncTenEnv) -> None: | ||
ten_env.log_debug("on_stop") | ||
self.server.ws_server.close() | ||
|
||
|
||
@register_addon_as_extension("websocket_server_python") | ||
class DefaultExtensionAddon(Addon): | ||
def on_create_instance(self, ten_env: TenEnv, name: str, context) -> None: | ||
print("on_create_instance") | ||
ten_env.on_create_instance_done(WebsocketServerExtension(name), context) |
13 changes: 13 additions & 0 deletions
13
packages/example_extensions/websocket_server_python/manifest.json
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,13 @@ | ||
{ | ||
"type": "extension", | ||
"name": "websocket_server_python", | ||
"version": "0.1.0", | ||
"dependencies": [ | ||
{ | ||
"type": "system", | ||
"name": "ten_runtime_python", | ||
"version": "0.6.0" | ||
} | ||
], | ||
"api": {} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/example_extensions/websocket_server_python/property.json
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 @@ | ||
{} |
1 change: 1 addition & 0 deletions
1
packages/example_extensions/websocket_server_python/requirements.txt
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 @@ | ||
websockets==10.4 |
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
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
43 changes: 43 additions & 0 deletions
43
tests/ten_runtime/integration/python/websocket_server_python/BUILD.gn
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,43 @@ | ||
# | ||
# Copyright © 2025 Agora | ||
# This file is part of TEN Framework, an open source project. | ||
# Licensed under the Apache License, Version 2.0, with certain conditions. | ||
# Refer to the "LICENSE" file in the root directory for more information. | ||
# | ||
import("//build/ten_runtime/feature/test.gni") | ||
import("//build/ten_runtime/ten.gni") | ||
|
||
ten_package_test_prepare_app("websocket_server_python_app") { | ||
src_app = "default_app_python" | ||
src_app_language = "python" | ||
generated_app_src_root_dir_name = "websocket_server_python_app" | ||
|
||
replace_files_after_install_app = [ | ||
"websocket_server_python_app/manifest.json", | ||
"websocket_server_python_app/property.json", | ||
] | ||
|
||
if (ten_enable_ten_manager) { | ||
deps = [ | ||
"//core/src/ten_manager", | ||
"//packages/core_apps/default_app_python:upload_default_app_python_to_server", | ||
"//packages/example_extensions/websocket_server_python:upload_websocket_server_python_to_server", | ||
] | ||
} | ||
} | ||
|
||
ten_package_test_prepare_auxiliary_resources( | ||
"websocket_server_python_test_files") { | ||
resources = [ | ||
"//tests/ten_runtime/integration/common=>common", | ||
"__init__.py", | ||
"test_case.py", | ||
] | ||
} | ||
|
||
group("websocket_server_python") { | ||
deps = [ | ||
":websocket_server_python_app", | ||
":websocket_server_python_test_files", | ||
] | ||
} |
Empty file.
Oops, something went wrong.