Public Socket.IO Server on Heroku or anything else

0 favourites
  • 1 posts
From the Asset Store
Create your own wss server and ws server with ssl and without ssl.
  • Tried it myself without success, so here I am.

    <font color=red><font size="5">U$ 5,00</font></font>

    for the first to deliver a related code(containing the same info present in this one) hosted and working online:

    var entities = [], count = 0;

    var port = process.env.VCAP_APP_PORT || 5000;

    var io = require("socket.io").listen(port);

    var usernames = {};

    var chat = {};

    var rooms = ['room1','room2','room3'];

    var INITIAL_X = 6000;

    var INITIAL_Y = 5000;

    var INITIAL_VEL_X = 0;

    var INITIAL_VEL_Y = 0;

    // assuming io is the Socket.IO server object

    io.configure(function () {

    io.set("transports", ["xhr-polling"]);

    io.set("polling duration", 10);

    io.set('log level', 1);

    console.log("The listening port is:" + port)

    });

    io.sockets.on("connection", function (socket) {

        var myNumber = count++;

        //assign number    

        var mySelf = entities[myNumber] = [myNumber, INITIAL_X, INITIAL_Y, INITIAL_VEL_X, INITIAL_VEL_Y, usernames];

        //Send the initial position and ID to connecting player

    console.log(myNumber + ' sent: ' + 'I,' + mySelf[0] + ',' + mySelf[1] + ',' + mySelf[2]);

        socket.send('I,' + mySelf[0] + ',' + mySelf[1] + ',' + mySelf[2]);

        // Join room

         socket.join('room1');

         // store the room name in the socket session for this client

         socket.room = 'room1';

         //Send to conencting client the current state of all the other players

        for (var entity_idx = 0; entity_idx < entities.length; entity_idx++) { //send initial update

            if (entity_idx != myNumber) {

                entity = entities[entity_idx];

                if (typeof (entity) != "undefined" && entity != null) {

                   console.log(myNumber + ' sent: C for ' + entity_idx);

                   socket.send('C,' + entity[0] + ',' + entity[1] + ',' + entity[2]); //send the client that just connected the position of all the other clients

                }   

            }

        }

        //create new entity in all clients    

        socket.broadcast.emit("message",'C,' + mySelf[0] + ',' + mySelf[1] + ',' + mySelf[2]);

        socket.on("message", function (data) {

           

            //if (myNumber == 0)

            //    console.log(myNumber + ' sent: ' +data);

            var new_data = data.split(',');

            if (new_data[0] == 'UM') {

                mySelf[1] = new_data[1];

                mySelf[2] = new_data[2];

                mySelf[3] = new_data[3];

                mySelf[4] = new_data[4];

                mySelf[5] = new_data[5];

                mySelf[6] = new_data[6];

                mySelf[7] = new_data[7];

                //Update all the other clients about my update

                socket.broadcast.emit("message",

                   'UM,' + mySelf[0] + ',' + mySelf[1] + ',' + mySelf[2] + ',' + mySelf[3] + ',' + mySelf[4] + ',' + mySelf[5] + ',' + mySelf[6] + ',' + mySelf[7]);

            }

            else if (new_data[0] == 'adduser') { // a adduser message

                var usernames = {};

                usernames[0] = new_data[1];

                console.log("message",'adduser,' + mySelf[0] + ',' + usernames[0]);

                socket.broadcast.emit("message",

                   'adduser,' + mySelf[0] + ',' + usernames[0]);

                   }

              else if (new_data[0] == 'chat') { // a chat message

                var chat = {};

                chat[0] = new_data[1];

                console.log("message",'chat,' + mySelf[0] + ',' + chat[0]);

                socket.broadcast.emit("message",

                   'chat,' + mySelf[0] + ',' + chat[0]);

                   }<font size="5">...</font>

           

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)