How to create a simple server with 'websockets' or 'ws'

Question:
Whether I use package ‘websocket’ or ‘ws’, I can’t connect to my simple server on the Internet from PC.
my server:
image
and my client(dot net: using System.Net.WebSockets):


‘websocket’ does not work:

Replit Profile: https://replit.com/@userThx/nt#index.js

var ip = "nt.userthx.repl.co";
var url = $"wss://{ip}";

Don’t add :8081

I have little experience with c#.

1 Like

which port should i choice?
I have used several ports, including 5900 ,8099.It didn’t work,

In fact, I created a nodejs script in replit as a client and executed it with a shell, but the connection still failed. It seems that I have some questions in replit and not c#

ok, use the port :443 like wss://nt.userthx.repl.co:443 or don’t use a port like wss://nt.userthx.repl.co

I also made a WebSocket client in JavaScript too. I was able to connect to my WebSocket server repl without using a port and also using the port 443.

I don’t have experience with WebSocket in C# since I code in JavaScript. Sorry for not being helpful if this doesn’t work. I’m bad at explaining things.

In the server repl, add the following code to the very end

wss.listen(8081, () => {
  console.log("WebSocket listening on port 8081!");
});

and remove { port: 8081 } because it does literally nothing.

1 Like

So grateful,and so funny
I didn’t think at all to try it without ports
but it really dont need a port to link
I was tormented by it
replit should tell every rookie like me this info.
but it worked in node.js client ,and not working on my computer with c#

When I remove it as you suggested,i got an error: ’ TypeError: One and only one of the “port”, “server”, or “noServer” options must be specified’
there must be one parameter at least…

and another error is : ‘TypeError: wss.listen is not a function’
it means that the method you gave me doesn’t seem to exist…Do you know which ‘ws’ I’m using
maybe i I did it wrong somewhere. I’m just getting started with node.js in replit

Use port 443 (that’s the default for Replit)

1 Like

If it’s not working, what’s the error popping up?

1 Like

You don’t need to do a DNS lookup for the repl’s IP (just use the hostname for the websocket URL), and you should be using wss://, not ws://. Are you following some kind of tutorial? If so, you might want to find a different tutorial.

2 Likes

Oops! My bad. Try the following code:

const wes = require("ws");
const ws = new wes.Server({
  port: 8081
});

ws.on("connection", (wss) => {
  wss.on("error", console.error);
  wss.on("message", (msg) => console.log("Recieved: \"" + msg + "\""));
  wss.send("something");
});

return;
1 Like

That’s probably going to cause issues.

3 Likes

That’s funny … it seems that listen any port will do on the server.
Just don’t use the port on the client and use ’wws’.
It’s not just 80 or 443, others are also ok.
Why!

it seems that any port will do,just dont using port on client.

you are right
I should use the domain name, not the IP and should not use the port.In fact, it doesn’t matter which port the server listens on, all be ok that I try several casually. I didn’t read the tutorial, so my writing was very irregular. I just don’t understand why you all know that 443 is the default, but I got the official documents are written in 5900 and 8099. But it doesn’t matter, it seems that any port is unnecessary, it can be used.ffffuny.