Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SocketIO connection refused

I am trying to get socket IO to work, but I keep getting connection refused. I guess connection refused is better than connection timeout, as refused mean's something is stopping it somewhere?

Anyway the code is from socketIO's tutorial website:

Client side, located in /home/server/nodejs/expressocket.js:

var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');

app.listen(8000);

function handler (req, res) {
  fs.readFile(__dirname + '../public_html/socketio.htm',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

And then server side located in /home/server/public_html/socketio.js

<!DOCTYPE html>
<head>
    <script src="https://cdn.socket.io/socket.io-1.3.4.js"></script>
</head>
<body>
    <h1>Socket IO Test</h1>
    <script>
    var socket = io('http://localhost:8000');
    socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
    });
    </script>
</body>
</html>

I've got the right port in there, and the server should be listening on that port. But why is it getting refused?

like image 690
Chud37 Avatar asked Feb 24 '15 12:02

Chud37


People also ask

Does Socket.IO reconnect after disconnect?

This event is fired upon disconnection. In the first two cases (explicit disconnection), the client will not try to reconnect and you need to manually call socket. connect() .

Does Socket.IO use WebSockets?

Although Socket.IO indeed uses WebSocket for transport when possible, it adds additional metadata to each packet. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a plain WebSocket server either.

Does Socket.IO use HTTP?

js) and the Socket.IO client (browser, Node. js, or another programming language) is established with a WebSocket connection whenever possible, and will use HTTP long-polling as fallback.


1 Answers

Instead of var socket = io('http://localhost:8000');,
use​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ var socket = io.connect('http://localhost:8000/');

also if you have used npm install you can use
<script src="http://localhost:8080/socket.io/socket.io.js"></script>
to access the socket.io library on the front-end

like image 140
Sagar Patni Avatar answered Sep 18 '22 19:09

Sagar Patni