r/ComputerCraft • • Nov 30 '22

websockets issue

im using python as my websocket server (websockets package)

when my turtle tries to connect i get no close frame recived or sent on the python side and could not connect on my turtle

python code:

import asyncio

import websockets


async def handler(websocket, path):
    print(path)
    data = await websocket.recv()

    reply = f"Data recieved as:  {data}!"

    await websocket.send(reply)


if __name__ == "__main__":
    asyncio.get_event_loop().run_until_complete(
        websockets.serve(handler, "172.22.1.5", 9090)
    )
    asyncio.get_event_loop().run_forever()

lua code:

local t,e = http.websocket(ws://turtle-hive.hive:9090/)
print(t)
print(e)

any help is greatly apricated

0 Upvotes

5 comments sorted by

2

u/fatboychummy Nov 30 '22

Please show us your code. We can do absolutely nothing to help you otherwise.

1

u/hiiambobthebob Nov 30 '22

let me edit

1

u/fatboychummy Nov 30 '22

Please copy your code directly rather than rewriting it into reddit. You have made a mistake on the first line and missed the quotes surrounding "ws://..."

Also, are you hosting this locally? turtle-hive.hive is not a known host for me. You may need to enable local ip ranges in the config and/or connect directly via your server's local ip.

Also, can you post what the error is on the computercraft side?

1

u/hiiambobthebob Nov 30 '22

figured it out apparently the websockets library dosnt work and you need to use aiohttp,

but thanks for the help!

1

u/Life-Poem4535 Feb 18 '25

A bit late, but hope I can help,

The Python websockets server automatically negotiates the permessage-deflate extension by default. If the ComputerCraft client doesn’t fully support these extensions or doesn’t understand them, it may drop the connection right after receiving the server’s handshake response.

Just open it like this:

async with websockets.serve( handler, host=globals.config_data.websocket.host, port=int(globals.config_data.websocket.port), create_protocol=CustomWebSocketServerProtocol, # your custom protocol (if needed) compression=None # Disable compression to avoid extension issues ):