Report this

What is the reason for this report?

how to run socket.io server

Posted on December 31, 2016

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


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!

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.

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

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

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.