2

I am very new to web development, so any tips regarding the following matter will be useful! So, the client written in javascript is supposed to communicate with the server written in python. I am trying to establish websocket connection between two PCs running UBUNTU and Windows OS They work perfectly fine when I run them using UBUNTU, using localhost. Also, everything works fine when the server is in UBUNTU and the client is in Windows. Only when the server is located in Windows and the client is in UBUNTU I keep running into the same error. 'Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT.

I tried turning off the firewall settings in Windows, but it didn't work.

Any input will be very appreciated!

Python Server

import asyncio
import websockets

async def hello(websocket, path):
    name = await websocket.recv()
    print(f"< {name}")

    greeting = f"Hello {name}!"

    await websocket.send(greeting)
    print(f"> {greeting}")

start_server = websockets.serve(hello, "localhost", 8765)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

Javascript Client

var ws = new WebSocket("ws://localhost:1337/");

ws.onopen = function(){
    console.log("Connection is Established");
    ws.send("Message to Send");
};

ws.onmessage = function(evt) {
    var received_msg = evt.data;
    console.log(received_msg);
};
2
  • Did you try clearing cookies? Just an idea Commented Aug 11, 2020 at 3:45
  • Thanks for the tip, but unfortunately that didn't work. I think it might have something to do with how UBUNTU handles outgoing websockets. I ran the server code from a raspberry PI, and UBUNTU client still couldn't connect. Commented Aug 11, 2020 at 5:45

1 Answer 1

1

Okay, I found what was wrong. Completely forgot that I had to change my router settings for port forwarding.

Sign up to request clarification or add additional context in comments.

2 Comments

As I remember, using "127.0.0.1" instead of "localhost" when starting the python server would also be required for connection between remote PCs
@AlexPredescu 127.0.0.1 won't work between remote PCs either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.