0

I have a PHP application, to which I am adding real time capabilities using node.js and socket.io

While I am experienced with PHP Node.js and am finding myself going round in circles.

I have installed both node.js and socket.io. I have created an a /usr/local/lib/app.js containing the following example code from the socket.io site:

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

app.listen(3000);

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);
  });
});

When I run node /usr/local/lib/app.js I get info - socket.io started So everything so far appears to work correctly.

However the next step is to add a script for the client to connect to the socket.io server upon load.

Within my PHP app I am adding the following:

<script src="http://xx.xx.xx.xx:3000/socket.io/socket.io.js"></script>
    <script>
        var socket = io.connect('http://localhost');
        socket.on('news', function (data) {
            console.log(data);
            socket.emit('my other event', { my: 'data' });
        });
    </script>

With my sites ip address in places of the xx's. However the request for the socket.io.js file always simply returns a 404 error.

I am clearly not grasping something. Hopefully someone can help.

4

0 回答 0