Tuesday, 2 May 2017

How do i using socket.io?

Good day, I'm trying to use socket.io i facing some problem with 404 not found. I'm using :84 as my xampp port

Folder structure

--tests
     --node_modules
  --app.js
  --index.html

Here is my html content

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.3/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost:84/test/');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });

</script>

and this app.js

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

app.listen(8080);

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

    }

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

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

I'm bit confuse about setting this part

  var socket = io.connect('http://localhost:84/test/');

and this part

app.listen(8080);

I'm using windows 7 64 bit and i'm using xampp to run it. When i run it i get 404 error like this

"NetworkError: 404 Not Found - http://localhost:84/socket.io/?EIO=3&transport=polling&t=Ll8amII" 

how can i fix it? sorry for my english and thanks in advance.



via YVS1102

No comments:

Post a Comment