R
R
ramovich2020-06-14 10:36:05
Python
ramovich, 2020-06-14 10:36:05

How to rewrite server example from node.js to python?

Hello. I need to make my Python program communicate with the Unreal Engine.

I decided to use sockets and found this instruction:

https://medium.com/@slonorib/how-to-connect-unreal...

They use socket.io, but the simplest server example is written in node.js (at the end of the article)

How to rewrite it in python? I never worked with socket.io under Python, in the documentation ( https://python-socketio.readthedocs.io/en/latest/) examples are for some reason connected with html pages (which I basically don’t need)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
ramovich, 2020-06-14
@ramovich

from aiohttp import web
import socketio
import asyncio
import time

sio = socketio.AsyncServer()
app = web.Application()
sio.attach(app)

async def sendmg(interval: int):
    while True:
        await sio.emit('newwind', '1')
        await asyncio.sleep(interval)

async def index(request):
    """Serve the client-side application."""
    with open('index.html') as f:
        return web.Response(text=f.read(), content_type='text/html')

@sio.event
async def connect(sid, environ):
    print("connect ", sid)
    
@sio.event
def disconnect(sid):
    print('disconnect ', sid)

app.router.add_static('/static', 'static')
app.router.add_get('/', index)
asyncio.ensure_future(sendmg(3))
web.run_app(app)
web.run_app(app)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question