Question

how to run socket.io server

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);
   })
})

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hope… this url (http://socket.io/get-started/chat/) will help you out

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