2

I'm trying to create a websocket without any framework, in vanilla JavaScript.

I'm trying to send a basic text websocket, but it keeps throwing me the error net::ERR_CONNECTION_REFUSED

Here's the code on repl.it

I have a Content Security Policy set:

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'">

my websocket is set to ws://localhost/ at the 9000 port.

Anyway here's my script.js file:

let a = new WebSocket("ws://localhost:9000");
a.onopen = () => {
  setTimeout(() => {
    a.send("working?");
  }, 1000);
};
a.onmessage = (event) => {
  console.log(event.data);
}
and my srv.js (in nodejs) file:

let server = require('ws').Server;
let srv = new server({port: 9000});
srv.on('connection', ws => {
  ws.on('message', msg => {
    console.log(`Received message => ${msg}`)
  })
  ws.send('hey')
})
lolfail
  • 124
  • 2
  • 12
  • do you have `srv.on('connection', function connection(ws) { ... })` on the server side? – Get Off My Lawn Jun 06 '19 at 13:36
  • Also, sometimes there are issues when using `localhost:9000`, try using an actual ip `127.0.0.1:9000` – Get Off My Lawn Jun 06 '19 at 13:39
  • @GetOffMyLawn changed the question, and I added what you wrote, I added message handler too and I sent a message, writing the ip address instead of localhost made the error disappear, but It can't send the message somehow and it probably isn't even detecting messages anyway thanks – lolfail Jun 06 '19 at 14:41
  • it can't connect to it, i figured out the ip address with tracert in cmd but it looks like the ip isn't correct – lolfail Jun 06 '19 at 14:55
  • `CONNECTION_REFUSED` is an error that occurs when the specified port, in this case `9000`, is not opened. Have you properly started a node server that is listening on the specified port? Btw. I have tried your code on my local machine and it works to no surprise. – bajro Jun 06 '19 at 14:59
  • 1
    @Bajro I chose an open (and allowed) port but it still throws the connection refused error – lolfail Jun 06 '19 at 20:10
  • @CodeZer0 Are there any updates on this issue? I have changed ports, nmap scanned them and they are open, I've switched through ip to localhost. I have tried to get my friend to connect but it throws the connection refused – Sean Jun 13 '20 at 13:45
  • @Sean I answered my post there you might find the problem – lolfail Jun 15 '20 at 18:41

1 Answers1

1

I saw that this old question of mine hasn't been answered, after that i dig into websockets and figured out that the problem I think was that I didn't set 'Access-Control-Allow-Origin': "*" in the server With ws it would be:

let server = require('ws').Server;
let srv = new server({port: 9000, 'Access-Control-Allow-Origin': "*"});
srv.on('connection', ws => {
  ws.on('message', msg => {
    console.log(`Received message => ${msg}`)
  })
  ws.send('hey')
});

Although my question is incorrect as ws is an external module that helps with websockets, anybody who thinks that making a websocket server without this module is easy is wrong, it is possible but the amount of code for just a simple server like in my question takes almost 50 lines of code which is not worth it, as ws has many features that makes it easy and more reliable than without using external modules.

For anybody who wants to actually make it for learning purposes here's the websocket protocol explained, but since they don't provide code I made a repl on repl.it

Community
  • 1
  • 1
lolfail
  • 124
  • 2
  • 12
  • The servers seems to run fine with this, will have to test later with a friend to see if it's solved the connection problem, however. Seems promising – Sean Jun 16 '20 at 00:22
  • I don't know what you mean by "ws is an external module". You mean this is a plugin available somewhere to help with websockets? If so, where? Also your repl link is broken. – Vincent Nov 10 '22 at 22:47