Skip to content

Commit

Permalink
feat: add Python websocket extension and test case
Browse files Browse the repository at this point in the history
  • Loading branch information
sunxilin committed Jan 7, 2025
1 parent 48fef22 commit ff1174a
Show file tree
Hide file tree
Showing 25 changed files with 404 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/example_extensions/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ group("example_extensions") {
if (ten_enable_nodejs_binding) {
deps += [
"http_server_extension_nodejs",
"websocket_server_extension_nodejs",
"websocket_server_nodejs",
]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import("//build/ten_runtime/feature/publish.gni")
import("//build/ten_runtime/glob.gni")
import("//build/ten_runtime/options.gni")

ten_package("websocket_server_extension_nodejs") {
ten_package("websocket_server_nodejs") {
package_kind = "extension"

resources = [
Expand All @@ -24,9 +24,9 @@ ten_package("websocket_server_extension_nodejs") {
}

if (ten_enable_ten_manager) {
ten_package_publish("upload_websocket_server_extension_nodejs_to_server") {
ten_package_publish("upload_websocket_server_nodejs_to_server") {
base_dir = rebase_path(
"${root_out_dir}/ten_packages/extension/websocket_server_extension_nodejs")
deps = [ ":websocket_server_extension_nodejs" ]
"${root_out_dir}/ten_packages/extension/websocket_server_nodejs")
deps = [ ":websocket_server_nodejs" ]
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"type": "extension",
"name": "websocket_server_extension_nodejs",
"name": "websocket_server_nodejs",
"version": "0.6.0",
"dependencies": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class WebsocketServerExtension extends Extension {
});

this.httpServer = server;


const wss = new WebSocketServer({ server });

Expand Down Expand Up @@ -82,7 +82,7 @@ class WebsocketServerExtension extends Extension {
}
}

@RegisterAddonAsExtension("websocket_server_extension_nodejs")
@RegisterAddonAsExtension("websocket_server_nodejs")
class WebsocketServerExtensionAddon extends Addon {
async onInit(_tenEnv: TenEnv): Promise<void> {
console.log("WS server addon onInit");
Expand Down
31 changes: 31 additions & 0 deletions packages/example_extensions/websocket_server_python/BUILD.gn
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 packages/example_extensions/websocket_server_python/LICENSE
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.
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 packages/example_extensions/websocket_server_python/main.py
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 packages/example_extensions/websocket_server_python/manifest.json
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": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
websockets==10.4
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ten_package_test_prepare_app("websocket_server_nodejs_app") {
"//core/src/ten_manager",
"//packages/core_apps/default_app_nodejs:upload_default_app_nodejs_to_server",
"//packages/example_extensions/http_server_extension_nodejs:upload_http_server_extension_nodejs_to_server",
"//packages/example_extensions/websocket_server_extension_nodejs:upload_websocket_server_extension_nodejs_to_server",
"//packages/example_extensions/websocket_server_nodejs:upload_websocket_server_nodejs_to_server",
]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
{
"type": "extension",
"name": "websocket_server_extension_nodejs",
"name": "websocket_server_nodejs",
"version": "0.6.0"
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"nodes": [
{
"type": "extension",
"name": "websocket_server_extension_nodejs",
"addon": "websocket_server_extension_nodejs",
"name": "websocket_server_nodejs",
"addon": "websocket_server_nodejs",
"extension_group": "default_extension_group",
"property": {
"server_port": 8007
Expand Down
1 change: 1 addition & 0 deletions tests/ten_runtime/integration/python/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ group("python") {
"standalone_test_python",
"two_async_exts_one_group_python",
"two_async_exts_python",
"websocket_server_python",
]

if (ten_enable_python_binding && ten_enable_go_binding) {
Expand Down
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.
Loading

0 comments on commit ff1174a

Please sign in to comment.