I have this socket.io and I cannot seem to connect to it. I don't have client html page, as the clients are mobile apps. Just trying to test this socket to see if communication works, but getting CANNOT GET.

I am not sure where to place the index.js file. Here is my folder structure. I have placed the socket code (index.js) in two places just in case, but I still cannot connect.

FOLDER STRUCTURE:

/index.js /public/index.js

CODE:

// Setup basic express server
var express = require('express');
var app = express();
var path = require('path');
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var port = process.env.PORT || 8080;

if (typeof String.prototype.startsWith != 'function') {
  String.prototype.startsWith = function (str){
    return this.slice(0, str.length) == str;
  };
}

function log_me(msg){
  var ts = new Date(new Date().getTime() - (3600000*4));
  var tss = ts.toString();
  tss = tss.substring(0, tss.indexOf(' GMT'));
  console.log(tss + ": " + msg);
}

server.listen(port, function () {
  console.log('Server listening at port %d', port);
});

// Routing
app.use(express.static(path.join(__dirname, 'public')));

// Chatroom

var numUsers = 0;

io.on('connection', function (socket) {
  var addedUser = false;

  // when the client emits 'new message', this listens and executes
  socket.on('new message', function (data) {
    // we tell the client to execute 'new message'
    socket.broadcast.emit('new message', {
      username: socket.username,
      message: data
    });
  });

  // when the client emits 'add user', this listens and executes
  socket.on('add user', function (username) {
    if (addedUser) return;

    // we store the username in the socket session for this client
    socket.username = username;
    ++numUsers;
    addedUser = true;
    socket.emit('login', {
      numUsers: numUsers
    });
    // echo globally (all clients) that a person has connected
    socket.broadcast.emit('user joined', {
      username: socket.username,
      numUsers: numUsers
    });
  });

  // when the client emits 'typing', we broadcast it to others
  socket.on('typing', function () {
    socket.broadcast.emit('typing', {
      username: socket.username
    });
  });

  // when the client emits 'stop typing', we broadcast it to others
  socket.on('stop typing', function () {
    socket.broadcast.emit('stop typing', {
      username: socket.username
    });
  });

  socket.on('join_channel', function(channel){
    log_me("join_channel: " + channel);
    socket.channel = channel;
    socket.join(channel);
  });
  socket.on('proceed', function(channel, data){
    log_me("proceed: " + channel + " - " + data);
    io.sockets.in(channel).emit('proceed', data);
  });
  socket.on('instruct', function(channel, data){
    log_me("instruct: " + channel + " - " + data);
    io.sockets.in(channel).emit('instruct', data);
  });
  socket.on('status_update', function(channel, data){
    log_me("status_update: " + channel + " - " + data);
    io.sockets.in(channel).emit('status_update', data);
  });
  socket.on('video_call_response', function(data){
    var obj = JSON.parse(data);
    log_me("video_call_response: " + obj.channel + " - " + data);
    io.sockets.in(obj.channel).emit('video_call_response', data);
  });
  socket.on('video_call_hangup', function(data){
    var obj = JSON.parse(data);
    log_me("video_call_hangup: " + obj.channel + " - " + data);
    io.sockets.in(obj.channel).emit('video_call_hangup', data);
  });

  // when the user disconnects.. perform this
  socket.on('disconnect', function () {
    log_me("disconnect: " + socket.channel);
    if (addedUser) {
      --numUsers;

      // echo globally that this client has left
      socket.broadcast.emit('user left', {
        username: socket.username,
        numUsers: numUsers
      });
    }
    if(socket.channel){
      if(socket.channel.startsWith("a_")){
        var c = socket.channel.replace("a_", "");
        var data = {'amb_id' : c, 'status' : 'DISCONNECTED'}
        if(io.sockets){
          io.sockets.in(c).emit('status_update', JSON.stringify(data));
        }
      }
    }
    socket.leave(socket.channel);

  });
});

0 Answers