简体   繁体   中英

JS WebSocket won't connect to my AWS EC2 instance running Node.js 'ws' package

I have a Windows EC2 instance with Node.js installed, and the ws websocket npm package. My security group has a custom TCP connection set for port 8080 to allow all connections.

I'm just trying to connect to it from a browser, and can't. I've tried everything, searched all over SO and the internet in general, and I'm at my wits-end... please help!

Node script running on the server:

console.log("init started");

var WebSocket = require("ws");

var server = new WebSocket.Server({
    "host" : "private IP of instance", // also tried public IP, but it throws an error
    "port" : 8080
});

server.on("connection", function (client){
    console.log("client connection open");

    client.on("message", function (data){
        console.log("client message received");
    });
});

server.on("listening", function (){
    console.log("server listening");
});

server.on("error", function (e){
    console.log("server error", e);
});

console.log("init done");

When I run the above code from the command-line, I get 3 logs: "init started", "init done" and "server listening" (as I'd expect). However, when I run the below from a simple HTML page, in Firefox it tells me "Firefox can't establish a connection to the server at ws://public IP of instance:8080/." and I get no more logs on the server...

var sock = new WebSocket("ws://public IP of instance:8080/");

sock.onopen = function (){
    sock.send("test");
};

I'm sure this must be an issue with the way my EC2 instance is configured, but I just don't know what the problem could be - I've checked and re-checked my inbound rules - port 8080 is open to all TCP traffic! What am I missing?

Note: I tried using "localhost" as the host in both scripts, and ran my test HTML page on the server itself, and everything worked fine. The server script printed "client connection open" and "client message received" as expected.

Edit to add the error message I get when specifying the public IP address of the instance in the server script:

{ Error: listen EADDRNOTAVAIL PUBLICIP:8080
    at Object._errnoException (util.js:1022:11)
    at _exceptionWithHostPort (util.js:1044:20)
    at Server.setupListenHandle [as _listen2] (net.js:1334:19)
    at listenInCluster (net.js:1392:12)
    at doListen (net.js:1501:7)
    at _combinedTickCallback (internal/process/next_tick.js:141:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)
    at Function.Module.runMain (module.js:686:11)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3
  code: 'EADDRNOTAVAIL',
  errno: 'EADDRNOTAVAIL',
  syscall: 'listen',
  address: 'PUBLICIP',
  port: 8080 }

I'm sorry, I have no idea how, because I swear I triple-checked it, but I had my public IP address wrong.

Sometimes I hate myself o_O

Note: This post explains the relationship between the public and private addresses pretty well: https://stackoverflow.com/a/34712279/859833

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM