为什么我不能连接到我的套接字。

时间:2019-05-13 15:13:15

标签: sockets socket.io

我有这个socket.io,但似乎无法连接到它。我没有客户端html页面,因为客户端是移动应用程序。只是尝试测试此套接字以查看通信是否有效,但无法获取。

我不确定将index.js文件放在何处。这是我的文件夹结构。为了以防万一,我将套接字代码(index.js)放在了两个位置,但是我仍然无法连接。

文件夹结构:

/index.js /public/index.js

代码:

// 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 个答案:

没有答案