I'm having problems to get socket.io working with my project that uses Grunt. I'm currently using angular-socket-io via bower. So here are things that I have done this far:
1) Installed angular-socket-io and socket.io-client via bower
2) Imported following lines exactly into index.html
<script src="bower_components/socket.io-client/socket.io.js"></script>
<script src="bower_components/angular-socket-io/socket.min.js"></script>
3) Added 'btford.socket-io' into my app.js
angular.module('angularApp', [ 'ngCookies', 'ngResource', 'ngSanitize', 'ngRoute', 'ui.bootstrap', 'btford.socket-io'])
4) Made Socket.js
file which is
app.factory('Socket', ['socketFactory', '$location',
function(socketFactory, $location) {
if($location.host() === "127.0.0.1") {
return socketFactory({
prefix: '',
ioSocket: io.connect($location.protocol() + '://' + $location.host() + ':' + $location.port())
});
} else {
return socketFactory({
prefix: '',
ioSocket: io.connect({path: '/node_modules/socket.io'})
});
}
}
]);
Project structure:
Project
|
- node_modules
|
- socket.io
- src
|
- main
|
- webapp
|
- app
|
- bower_components
- scripts
|
- controllers
- services
- app.js
- Socket.js
- styles
- views
- index.html
- bower.json
- Gruntfile.js
- server.js
But I get following error on every polling:
GET http://127.0.0.1:9000/socket.io/?EIO=3&transport=polling&t=1448635105443-105 404 (Not Found)
What could be wrong? Is it possible to tell angular to import some specific modules of node? I have been searching solution for this from stackoverflow and also Googled it many days, but I haven't found any solution.
However I have found that when it tries to GET socket.io using that address it tries to get it from node_modules path. I have installed there just in case socket.io via npm.
I have also read the instructions of angular-socket-io at Github angular-socket-io
EDIT*
I have tried to create server.js
which includes following lines:
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');
app.listen(9000, "127.0.0.1");
function handler (req, res) {
fs.readFile(__dirname + '/src/main/webapp/app/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', {message: 'hello world'});
socket.on('my event', function(data){
console.log(data);
})
})
But it still say the same thing.