how to run socket.io server

December 31, 2016 1.3k views
Node.js Ubuntu

I installed: Node, npm socket.io and npm websockets on ubuntu. Javascrip code is below. And its running on 139.59.215.170 but unity wont get any data :( Do you have any idea what could be wrong ? Code is fine, i tested it on my computer.

#!/usr/bin/env nodejs
var io = require('socket.io')({
   transports: ['websocket'],
});

var userdataN = [ ];
var redovi = 6;

for(var i = 0; i < redovi; ++i) {
    userdataN[i] = [ ];
}
console.log("Online players: " + userdataN[0].length);
io.attach(4567);
io.on('connection', function(socket){

      socket.on('userdata', function(data){

      if(userdataN[0].indexOf(socket.id) == -1){

      userdataN[0].push(socket.id);
      }else{

         var poz = userdataN[0].indexOf(socket.id);
            userdataN[1][poz] = data.username;
               userdataN[2][poz] = data.x;
               userdataN[3][poz] = data.y;
               userdataN[4][poz] = data.z;
               userdataN[5][poz] = data.rotY;

               socket.emit('update', {

                  username : userdataN[1],
                  x : userdataN[2],
                  y : userdataN[3],
                  z : userdataN[4],
                  rotY : userdataN[5]
               });
         }
})
      socket.on('chatmsg', function(poruka){
         var poz = userdataN[0].indexOf(socket.id);
         socket.emit('update', {

                  username : userdataN[1][poz],
                  msg : poruka

               });

      })
   socket.on('disconnect', function() {

      var poz = userdataN[0].indexOf(socket.id);
            userdataN[0].splice(poz,1);
               userdataN[1].splice(poz,1);
               userdataN[2].splice(poz,1);
               userdataN[3].splice(poz,1);
               userdataN[4].splice(poz,1);
               userdataN[5].splice(poz,1);
   })
})
2 Answers

Lol, no edit button.

Just to add that im trying to connect with my unity game.
ws://139.59.215.170:4567/socket.io/?EIO=4&transport=websocket

Have another answer? Share your knowledge.