Skip to content

Commit e3a2233

Browse files
author
Roberto De Ioris
authored
Update AsyncIOAndUnrealEngine.md
1 parent d79be3a commit e3a2233

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

tutorials/AsyncIOAndUnrealEngine.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,44 @@ class RadioStreaming:
324324
self.coroutine.cancel()
325325
```
326326

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+
327365
## Additional 'transient' loop engines
328366

329367
## Note: concurrency vs parallelism

0 commit comments

Comments
 (0)