0

I'm currently testing a very basic socket example and even the basic test doesn't work. My Python code (server) is:

import socket
server = socket.socket(socket.AF_INET)
server.bind(("0.0.0.0", 5000))
server.listen(1)
conn, addr = server.accept()

My Javascript code (client) is:

var connection = new WebSocket("wss://<my-local-ip>:5000");

However, after starting my Python code and then running my Javascript code, the connection hangs on the WebSocket.CONNECTING state for around half a minute before giving the error:

WebSocket connection to 'wss://192.168.0.119:5000/' failed: WebSocket opening handshake timed out

I also tried replacing <my-local-ip> with my public IP (according to ipinfo.io/ip), but that gave a different error:

WebSocket connection to 'wss://216.58.126.4:5000/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

I've looked on the internet and all examples are structured exactly the same way as my test code so why is it not working?

jhpratt
  • 6,841
  • 16
  • 40
  • 50
hyper-neutrino
  • 5,272
  • 2
  • 29
  • 50
  • 3
    Probably this is why [https://stackoverflow.com/questions/4973622/difference-between-socket-and-websocket](https://stackoverflow.com/questions/4973622/difference-between-socket-and-websocket) – Gabriel B.R Feb 27 '18 at 01:14
  • In your first part, `websocket` need a HTTP-compatible handshake, but your server cannot provide this handshake. In short, the socket connection has been established but `websocket` connection hasn't. – Sraw Feb 27 '18 at 01:36

2 Answers2

1

I'm not sure as to the first part of your question. However, for the second part, you'll need to configure port forwarding via your router.

If you're not familiar with that, it'll basically tell the router to send any requests to port X to a certain local IP on port Y (X and Y are commonly the same). You'll want a static local IP, naturally, as otherwise you'd have to keep your router in sync with the dynamic IP of your computer.

For more info on port forwarding, see this post on SuperUser.

jhpratt
  • 6,841
  • 16
  • 40
  • 50
  • Port forwarding alone isn't always enough - connecting to the public IP from within the LAN won't use the port forward, unless the router is capable of NAT loopback (and it is enabled for that forwarding rule) – Jaromanda X Feb 27 '18 at 01:36
  • @JaromandaX I've never had an issue with loopback, and I certainly didn't configure anything for it. Could very well depend on the router. – jhpratt Feb 27 '18 at 01:41
  • Yup, port forwarding fixed my problem, thanks! It also let me set up what I was trying to do originally properly, so yay for that. – hyper-neutrino Feb 27 '18 at 17:51
1

A webSocket connection is not an ordinary TCP connection. To receive incoming webSocket connections, you need a webSocket server, not a plain TCP server.

You need a server that supports the webSocket connection protocol (all connections start with an HTTP connection), the webSocket security exchange and the webSocket data frame format.

With the plain TCP server you have, the underlying TCP socket connection is established, but after sending the first stage of the webSocket protocol, the client is waiting for the first response from your server and you aren't sending it. After a period of time waiting, the client disconnects because it's initial webSocket handshake failed.

So, in your python, you either need to get a library that already supports a webSocket server or you need to implement the full webSocket protocol yourself.

You can see how a webSocket connection works in this description of webSocket servers on MDN.

This may also be useful: What's the difference between WebSocket and plain socket communication?

jfriend00
  • 683,504
  • 96
  • 985
  • 979