2

This is simple chat on WS and express.js. I get the error that the browser can't connect to server via websockets.

client connection:

file: rtc.html 
ws = new WebSocket('wss://' + window.location.hostname + '/wr' );
ws.onerror = (error) => { console.log(error); };
ws.onmessage = (message) => {
   . . . 

Server code:

const express =   require('express');
const http =      require('http');
const WebSocket = require('ws');

const app = express();

app.get('/rtc', (req, res)=>{
  res.sendFile('/home/user/dev/rtc.html');
});

const server = http.createServer(app);
const wss = new WebSocket.Server({ server:server, path: "/wr" });

. . . 

app.listen(3000);

UPD: The problem was due to the fact that I was doing chat on webrtc and tested in Mozilla and Mozilla would not connect without https connection however getUserMedia ran fine. It was necessary to write so:

var https = require('https');
var serv = https.createServer(serverConfig, app);
alex10
  • 2,726
  • 3
  • 22
  • 35

2 Answers2

9

Change from:

app.listen(3000);

to:

server.listen(3000);

When you use app.listen(), it creates a new http server and thus the one you connected socket.io to is never started. To fully understand app.listen(), the code for it looks like this:

app.listen = function(){
  var server = http.createServer(this);
  return server.listen.apply(server, arguments);
};

So, you can see it was creating a different http server than the one you attached your webSocket server to and thus that other one was never started.


Alternatively, you could also do this:

const server = app.listen(3000);
const wss = new WebSocket.Server({ server:server, path: "/wr" });

And, not create your own http server at all. app.listen() returns the new server object that it created.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • I replaced: `app.listen(3000);` to: `server.listen(3000);` but still not connection – alex10 Feb 26 '17 at 20:21
  • 1
    @alex10 - Then you need to do some troubleshooting/debugging. In the Chrome browser, look at the network tab in the debugger and see exactly what is happening from the client when your page is loaded. In particular, you will be looking for the attempt to connect on the webSocket and see what is returned (or not) from that. If you don't know how to open the Chrome debugger in your page when it is loaded, then Google that and you should find plenty of info on how to do that. You will want the network tab to be able to see what happens with your webSocket connection. – jfriend00 Feb 26 '17 at 20:59
  • I kind of found what was the problem. At the end of the matter written, the problem was weird. – alex10 Feb 26 '17 at 23:04
  • You gave the correct answer. I had the question to include more details. – alex10 Feb 26 '17 at 23:10
  • Thank you! That's exactly what I was missing :) – s3v3n Nov 30 '18 at 15:53
1

just make sure you use server.listen().Rest the code speaks itself

   var express         = require('express'),
     app             = express(),
     http            = require('http'),
     server          = http.createServer(app),
     WebSocketServer = require('ws').Server,
     wss             = new WebSocketServer({ server });
 
 
 
 app.use(express.static(__dirname));
 
 server.listen(process.env.PORT || 3000, function () {  //
 console.log("Node server is running on http://localhost:3000/"); });
 
 wss.on('connection', function (ws) {
    //console.log("New connection.");
    ws.on('message', function (message) {
        //console.log("Message received:", message);
    });
Manish Kakati
  • 657
  • 5
  • 12