I have my node.js server running and communicating on my local machine (windows). But when i copy my project and try to run it on my online server (Linux), socket.io doesn't communicate from server to client (html page). I am getting no errors and socket.io is installed on server. The directory is /var/www/lynx.html
How do I direct the server to communicate with the client, and the client to connect with the server. I have tried to change http:\\localhost
to all kinds of things like the actual ip address and local 127.0.0.1
Here are the important parts of my code.
Client
<script type="text/javascript">
var socket = io.connect('http://localhost');
socket.on('message', function(message){
var obj = jQuery.parseJSON(message);
var htmlStr ='';
Server
var dgram = require("dgram");
var udpResultsServer = dgram.createSocket("udp4");
var udpTimeServer = dgram.createSocket("udp4");
var http = require('http');
var io = require('socket.io');
var htmlPage;
var jsonResults;
var tempResults = '';
var finalResults = '';
var time = '0.0';
var r = new Array();
/*
************************************
** Get Ip Addresses **
************************************
*/
function getIP(){
var os = require( 'os' );
var networkConfig = os.networkInterfaces( );
var ipList ='';
for (var name in networkConfig) {
networks = networkConfig[name];
for (var name in networks) {
details = networks[name];
if (details['family'] == "IPv4"){
ipList = ipList + details['address'] + " ";
}
}
}
return ipList;
}
/*
************************************
** Web server and sockets.io **
************************************
*/
var htmlPage;
// read html file to serve
fs.readFile('lynx.html', function(error, data){
if (error){
throw error;
}
else{
htmlPage = data;
console.log('Server wrote to lynx.html file');
}
});
// start web server
var htmlServer = http.createServer(function(request, response) {
response.writeHeader(200, {"Content-Type": "text/html"});
console.log('Client Connected ....');
response.end(htmlPage);
});
htmlServer.listen(8000,'127.0.0.1');
console.log("HTML server started on: " + getIP());
// start up socket.io for transmitting data to the web page
var serv_io = io.listen(htmlServer, { log: false });
serv_io.sockets.on('connection', function(socket){
//send data to client
setInterval(function(){
jsonResults = JSON.stringify(r); // convert final array into a json string
socket.send(jsonResults); // sent json results via socket
console.log('send <--> receive');
}, 1000);
});
These are the errors when I installed Socket.io locally in the drive I am working on, would this cause the errors.
I also installed it globally -g and there were not the errors.
npm WARN package.json fs@0.0.2 fs is also the name of a node core module.
npm WARN package.json fs@0.0.2 No description
npm WARN package.json fs@0.0.2 No repository field.
npm WARN package.json net@1.0.2 net is also the name of a node core module.
npm WARN package.json net@1.0.2 'repositories' (plural) Not supported. Please pick one as the 'repository' field
npm WARN package.json has-binary-data@0.1.5 No repository field.
socket.io@1.2.1 node_modules/socket.io
âââ debug@0.7.4
âââ has-binary-data@0.1.3 (isarray@0.0.1)