最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firefox can’t establish a connection to the server at ws:localhost:8080 - Stack Overflow

programmeradmin0浏览0评论

I have two files one is test.html which is:

<script src="js/jquery-2.1.4.min.js"></script>
<script type="text/javascript">
  
$(function () {
  // if user is running mozilla then use it's built-in WebSocket
  window.WebSocket = window.WebSocket || window.MozWebSocket;

  var connection = new WebSocket('ws://localhost:8080/');

  connection.onopen = function () {
    // connection is opened and ready to use
    alert('connection Open');
  };

  connection.onerror = function (error) {
    // an error occurred when sending/receiving data
    alert('Error');
  };

  connection.onmessage = function (message) {
    alert('Message');
    
  };
});

</script>

And one nodejs file which is

var WebSocketServer = require('websocket').server;
var http = require('http');
 
var server = http.createServer(function(request, response) {
    console.log((new Date()) + ' Received request for ' + request.url);
    response.writeHead(404);
    response.end();
});
server.listen(8080, function() {
    console.log((new Date()) + ' Server is listening on port 8080');
});


wsServer = new WebSocketServer({
    httpServer: server,
    
    autoAcceptConnections: false
});
function originIsAllowed(origin) {
 return true;
}
 
wsServer.on('request', function(request) {
    if (!originIsAllowed(request.origin)) {
      request.reject();
      console.log((new Date()) + ' Connection from origin ' + request.origin + ' rejected.');
      return;
    }
    
    var connection = request.accept('echo-protocol', request.origin);
    console.log((new Date()) + ' Connection accepted.');
    connection.on('message', function(message) {
        console.log(message);
        connection.sendBytes(message);
       
    });
    connection.on('close', function(reasonCode, description) {
        console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
    });
});

While I am trying to connect with web-socket with my HTML file its give me an error which is

Firefox can’t establish a connection to the server at ws://localhost:8080/

I have two files one is test.html which is:

<script src="js/jquery-2.1.4.min.js"></script>
<script type="text/javascript">
  
$(function () {
  // if user is running mozilla then use it's built-in WebSocket
  window.WebSocket = window.WebSocket || window.MozWebSocket;

  var connection = new WebSocket('ws://localhost:8080/');

  connection.onopen = function () {
    // connection is opened and ready to use
    alert('connection Open');
  };

  connection.onerror = function (error) {
    // an error occurred when sending/receiving data
    alert('Error');
  };

  connection.onmessage = function (message) {
    alert('Message');
    
  };
});

</script>

And one nodejs file which is

var WebSocketServer = require('websocket').server;
var http = require('http');
 
var server = http.createServer(function(request, response) {
    console.log((new Date()) + ' Received request for ' + request.url);
    response.writeHead(404);
    response.end();
});
server.listen(8080, function() {
    console.log((new Date()) + ' Server is listening on port 8080');
});


wsServer = new WebSocketServer({
    httpServer: server,
    
    autoAcceptConnections: false
});
function originIsAllowed(origin) {
 return true;
}
 
wsServer.on('request', function(request) {
    if (!originIsAllowed(request.origin)) {
      request.reject();
      console.log((new Date()) + ' Connection from origin ' + request.origin + ' rejected.');
      return;
    }
    
    var connection = request.accept('echo-protocol', request.origin);
    console.log((new Date()) + ' Connection accepted.');
    connection.on('message', function(message) {
        console.log(message);
        connection.sendBytes(message);
       
    });
    connection.on('close', function(reasonCode, description) {
        console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
    });
});

While I am trying to connect with web-socket with my HTML file its give me an error which is

Firefox can’t establish a connection to the server at ws://localhost:8080/
Share Improve this question edited Oct 15, 2021 at 3:22 Sabito 5,0659 gold badges37 silver badges65 bronze badges asked Feb 21, 2018 at 10:57 Gaurav KandpalGaurav Kandpal 1,3303 gold badges16 silver badges35 bronze badges 1
  • 1 Logs of node.js server? – mfkw1 Commented Feb 21, 2018 at 15:43
Add a comment  | 

2 Answers 2

Reset to default 7

It should be able to find and connect to the server but the server will reject your request and shutdown because of request.accept('echo-protocol', request.origin) in your server file.

Check the log of you nodejs command prompt.

To fix this just modify

var connection = new WebSocket('ws://localhost:8080/');

to

var connection = new WebSocket('ws://localhost:8080/', 'echo-protocol');

I was facing a similar problem and trying other browsers wont even let me connect to my APIs. Just run the same url you have as a https fixed the issue for me.

For example: wss://localhost:3038/ turns into https://localhst3038.

Browsers will ask you to accept the risk on self signed certificates. After that all browsers worked just fine.

发布评论

评论列表(0)

  1. 暂无评论