Thursday, 16 March 2017

Problems using socket.io, socket.io.js not found

I receive this error in console GET http://localhost/socket.io/socket.io.js 404 (Not Found). I used npm install to install express and socket.io.
This is my javascript code

var express = require('express')
  , http = require('http');

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

server.listen(8000);
users = [];
connnection = [];

console.log('Server running!');

app.get('/',function(req, res){
    res.sendFile(__dirname + '/chat.php');
});

io.sockets.on('connection', function(socket){
  connections.push(socket);
  console.log('Connected: %s sockets connected', connections.length);

  //Disconnect
  connections.splice(connections.indexOf(socket),1);
  console.log('Disconnected: %s sockets connected', connections.length);
});

And this is what I added into php file

<script src="/socket.io/socket.io.js"></script>
<script>
$(function(){
    var socket=io.connect();
});
</script>


via Daniel

No comments:

Post a Comment