简体   繁体   中英

ERR_CONNECTION_REFUSED with socket.io on Node.js server

I have a basic client-server setup with Node.js:

Server

var port = 8000;
var http = require('http');
var express = require('express');
var app = express();
var server = http.createServer(app).listen(port);
var io = require('socket.io').listen(server);

app.use(express.static(__dirname + '/site'));
app.get('/', function(req, res) {
    res.sendFile(__dirname + '/site/index.html');
});

server.listen(port, function() {
    console.log("Listening on port " + port + "...");
});

io.on('connection', function(socket) {
    console.log("A user connected");
});

Client

var socket = io.connect("http://72.53.66.228:8000");

socket.on('connect', function() {
    console.log("Connected.");
});

However, I keep getting this error in my browser's console, and I'm not sure why:

GET http://72.53.66.228:8000/socket.io/?EIO=3&transport=polling&t=1419738402131-5 net::ERR_CONNECTION_REFUSED

I should also note that I'm running an Apache webserver and I'm using the ProxyPass directive to let Node.js serve off a subdirectory

The main issue was the path. Since Apache was only proxying the /CAH/. . . path.

The trouble with io.connect("http://72.53.66.228/CAH"); was that socket.io does a 'cleaver' trick where it interpreted /CAH as the room rather than as the path.

The solution is to set the path option thusly: io.connect({path:'/CAH/socket.io'}) which causes the socket.io client to connect to the correct urls.

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