Beginner TCPServer / websocket question

Hi, I am experimenting with using TCPServer on my Photon and trying to make a client connection from my laptop.

I have the sample code below from: TCPServer | Reference | Particle

I am using WiFi.localIP() to get the local IP of my Photon.

I am trying with different websocket testing clients (Postman, websocketking.com) by entering the IP and port (eg wss://192.168.22.74:443), but am unable to make a connection. I have tried by having both devices connected to my home Netgear router’s WiFi, and also on my smartphone hotspot.

I do see in the Particle device console that the attempt is made (it publishes “Connected” and “Echo”), but the connection does not succeed. On Postman it says “Parse Error: Expected HTTP/” and with websocketking it just says “websocket connection to (websocket address) failed.”

What am I missing here? Thank you.

TCPServer server = TCPServer(443);
TCPClient client;
char myIpAddress[24];

 
void setup() {

  // start listening for clients
  server.begin();

  // Make sure your Serial Terminal app is closed before powering your device

  Serial.begin(9600);

  // Wait for a USB serial connection for up to 30 seconds
  waitFor(Serial.isConnected, 30000);

}

 
void loop() {

    IPAddress myIp = WiFi.localIP(); // get the IP of the photon which is handed out via DHCP
    
    Serial.println(myIp);    // prints the device's IP address

if (client.connected()) {

    // echo all available bytes back to the client

        Particle.publish("Connected");
        while (client.available()) 
        {
            Particle.publish("Echo");
            server.write(client.read());
        }

  } else {
    // if no client is yet connected, check for a new connection
    client = server.available();
}
}

The local IP is not directly accessible from outside that subnet.
To access a local IP from outside you’d need to setup port-forwarding in your router.

1 Like

@ScruffR thank you for the quick response. I’m showing my ignorance here, but if both my laptop and the Photon are connected to the same router and their ip addresses start with 192.168.22.xxx, is this not sharing the same subnet? Is port forwarding still required?

1.LAN → Local Area Network (your photon, laptop and router/ADSL modem)
2.WAN → Wide Area network ( your router/ADSL modem, entire internet )
3. Port forwarding → allowing you to see your local devices in WAN.
4. wss != ws → your photon is not gonna work with nothing which is https just simple http.
5. If you want to try ws locally, JavaScript has native WS API which is worth to try.

Best,

1 Like

@policenauts, that is correct, but I’d suspect that even when your laptop is connected to the local network the service you are invoking to do issue the request (websocketking
@ 3.72.140.173) is most likely not running locally on your laptop but remotely.
You are sending your data from your browser to the remote servers, which in turn forms and sends (or rather tries to send) the request to your destination local IP.

1 Like

Hmm, I remain stumped - I’m getting the same Parse Error: Expected HTTP/ error when using both curl and Postman (no browser). I’ve tried both ws:// and wss:// (I get an SSL error with the latter, to be expected) and multiple different port numbers.

I am seeing the Photon receive the request each time via the Particle.publish() so it seems it’s routing the request to the local IP correctly on whatever network I’m using, but the connection itself doesn’t succeed. Is there something I’m missing on the TCPServer side?

The target of a ws:// URL is expected to be a WebSocket server. It starts out as a http connection, then switches to full-duplex TCP connection. Since the Photon doesn’t implement the http part, it won’t actually establish the connection.

You could implement the WebSocket protocol on top of TCPServer, but TCPServer does not do it automatically.

3 Likes

Thanks everyone for the help, I was apparently equating TCPServer with websockets and I’ve learned this isn’t the case!

I need to be able to communicate from the browser (which doesn’t seem possible using a raw TCP socket connection), so instead I was able to get the Websockets library working by importing some of the files manually from the github, and so far it seems to be working with the Photon as the websocket server.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.