Good morning, even if it is evening.
<hr />I am playing "Just for fun" with Python websockets and asyncio sending some coordinates<br /> to be shown into a p5.js canvas as colored dots connected with colored lines.
<hr />Locally I can run my "server.py" and have ws://localhost:8765 uri and port accessible to get data into Web page.
<hr />Now, I need to run my "server.py" on PythonAnywhere to send same data to my Web page,<br /> but as I can see, I could not access the uri and port as ws://user-name.pythonanywhere.com:8765.
<hr />What can I do?
<hr />Here is my "server.py" code.
import asyncio
from random import randint
from websockets.asyncio.server import serve
async def generate_coordinates():
# Generate random x and y coordinates within the given ranges
x = randint(100, 1100)
y = randint(100, 700)
return [x, y]
async def echo(websocket):
try:
while True:
message = await generate_coordinates()
await websocket.send(f"{message}")
await asyncio.sleep(0.05)
except Exception as e:
print(e)
async def main():
async with serve(echo, "0.0.0.0", 8765) as server:
try:
await server.serve_forever()
except Exception as e:
print(e)
if __name__ == "__main__":
asyncio.run(main())
Here is how coordinates are shown in my Web page over Websockets.