| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent d79be3a commit e3a2233
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -324,6 +324,44 @@ class RadioStreaming: | |||
| 324 | 324 | self.coroutine.cancel() | |
| 325 | 325 | ``` | |
| 326 | 326 | ||
| 327 | + ### PyUserWidget WebSocket Echo test | ||
| 328 | + | ||
| 329 | + ```python | ||
| 330 | + import unreal_engine as ue | ||
| 331 | + import ue_asyncio | ||
| 332 | + import asyncio | ||
| 333 | + import aiohttp | ||
| 334 | + | ||
| 335 | + class Echo: | ||
| 336 | + | ||
| 337 | + def construct(self): | ||
| 338 | + self.coroutine = asyncio.ensure_future(self.ws_connect('ws://echo.websocket.org')) | ||
| 339 | + self.coroutine.add_done_callback(self.check_exception) | ||
| 340 | + self.ws = None | ||
| 341 | + | ||
| 342 | + | ||
| 343 | + def button_clicked(self): | ||
| 344 | + if self.ws: | ||
| 345 | + self.ws.send_str(self.uobject.Input.GetText()) | ||
| 346 | + | ||
| 347 | + def check_exception(self, coro): | ||
| 348 | + if coro.cancelled(): | ||
| 349 | + return | ||
| 350 | + exc = coro.exception() | ||
| 351 | + if exc: | ||
| 352 | + raise exc | ||
| 353 | + | ||
| 354 | + async def ws_connect(self, url): | ||
| 355 | + session = aiohttp.ClientSession() | ||
| 356 | + async with session.ws_connect(url) as ws: | ||
| 357 | + self.ws = ws | ||
| 358 | + async for msg in ws: | ||
| 359 | + orig_text = self.uobject.WSData.GetText() | ||
| 360 | + self.uobject.WSData.SetText(orig_text + '\n' + msg.data) | ||
| 361 | + await ws.close() | ||
| 362 | + | ||
| 363 | + ``` | ||
| 364 | + | ||
| 327 | 365 | ## Additional 'transient' loop engines | |
| 328 | 366 | ||
| 329 | 367 | ## Note: concurrency vs parallelism | |
| Back | FazBrowse Home | New Git URL |
0 commit comments