mercredi 6 mai 2015

socket.io along with nodejs when used on web server receiving 'http://ift.tt/1EQ8lKS' '404 network error'

I have been working on a chat process with the help of nodejs and socket.io when I worked on my windows system I didn't face any issue with the socket.io transport and polling but when am running the same from aws EC2 server with Windows Operating system its giving me

NetworkError: 404 Not Found - http://[my ip address]:8081/http://ift.tt/1EXhySG

I have been looking out for answers but I couldn't get any idea regarding this problem. For much more clarification I am integrating this chat with IOS app.

My app.js server code

var express =require('express');
var bodyParser =require('body-parser');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io').listen(http);
var routes = require('./routes/router');
io.configure(function() {
  io.set('transports': [
    'websocket', 
    'flashsocket', 
    'htmlfile', 
    'xhr-polling', 
    'jsonp-polling', 
    'polling'
  ]);
});
var rooms = ['b','c','d'];

app.set('port', process.env.OPENSHIFT_NODEJS_PORT || 8081);
app.set('ipaddr', process.env.OPENSHIFT_NODEJS_IP || MY IP ADDRESS);
app.set('view engine', 'ejs');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);


app.get('/', routes.index);

var receive_chat = {};
var connectedSocket = {};

io.on('connection', function (socket){
  socket.on('addUser', function (userid){
    //console.log(userid);
    connectedSocket[userid] = socket;
    socket.UserId = userid;
    //console.log(connectedSocket);
  });

  socket.on('createRoom', function (chatid,receiverid){
    //console.log("inside create room ");
    //socket.room = chatid;
    socket.join(chatid);
    for(i=0;i<receiverid.length;i++){
      if(connectedSocket[receiverid[i]]){
        //connectedSocket[receiverid[i]].room = chatid;
        connectedSocket[receiverid[i]].join(chatid);
        //console.log("room name - "+connectedSocket);
      }
    }
  });

  socket.on('sent_chat_message', function (sender_content){
        //console.log('Message from '+sender_content["sender_id"]+", Message - "+sender_content["msg"]+", receiver_id - "+sender_content["receiver_id"]);
     //console.log(sender_content["msg"].replace("\n", "\\n"));
     receive_chat['sender_id'] = sender_content["sender_id"];
     //receive_chat['msg'] = JSON.parse(sender_content["msg"].replace("\n", "\\n"))
     var re = new RegExp("\n", 'g');
     var receiveridArray = sender_content["receiver_id"];
     console.log(receiveridArray);
     receive_chat['msg'] = JSON.parse(sender_content["msg"].replace(re, '\\n'))
     receive_chat['receiver_id'] = sender_content["receiver_id"]
     receive_chat['chat_id'] = sender_content["chat_id"]
     // receive_chat['persons_in_chat'] = [];
     // for(i=0;i<sender_content["receiver_id"].length;i++){
     //  receive_chat['persons_in_chat'].push(sender_content["receiver_id"][i]);
     // }
     // receive_chat['persons_in_chat'].push(receive_chat['sender_id']);

     console.log(receive_chat);
     // console.log(socket);

     // socket.broadcast.to(sender_content["chat_id"]).emit('receiving_chat_message',receive_chat);

     for(j=0; j<receiveridArray.length;j++)
     {
        //console.log(connectedSocket.length);
        //console.log("inside loop - "+receiveridArray[j]);
        if(connectedSocket[receiveridArray[j]]){
          if(receiveridArray[j] != sender_content["sender_id"]){
            connectedSocket[receiveridArray[j]].emit('receiving_chat_message',receive_chat);
          }
        }
     }

  });
});

//io.on('disconnect', function () {
 //        console.log('socket disconnected');
 //        delete connectedSocket[socket.UserId];
//         socket.leave(socket.room);
//});

// to(receive_chat["receiver_id"])
http.listen(app.get('port'), app.get('host'),function(){
  console.log('listening on '+app.get('port'));
});

My index.html client side

<!DOCTYPE html>
<html>
    <head>

        <script src="/public/javascript/socket.io.js"></script>
        
        <script type="text/javascript">
            var url = "http://my ip address:8081/";
            var socket = io.connect(url,
                {
                    'reconnect': true,
                    'reconnection delay': 500,
                    'max reconnection attempts': 10
                });
            
            var chatidArray = [];
            alert('connect')
            //on connect
            socket.on('connect', function () {
                   alert('connected')
                var data = {};
                data.status = "Connected";
                prepareiOSFunction("chatStatus", JSON.parse(JSON.stringify(data)));
            });
            
            
            
            //on disconnect
            socket.on('disconnect', function() {
                var data = {};
                data.status = "Disconnected";
                prepareiOSFunction("chatStatus", JSON.parse(JSON.stringify(data)));
            });

            
        </script>
    </head>
    <body>
        

    </body>
</html>

Kindly help me out with this issue.Thanks




Aucun commentaire:

Enregistrer un commentaire