流星SocketIO客户端-服务器连接未建立

时间:2019-09-26 22:09:36

标签: javascript heroku meteor websocket socket.io

我正在尝试使用socket.io将本地部署的客户端应用程序连接到部署到heroku的服务器应用程序。该应用程序在本地运行良好,但部署到heroku时无法建立网络套接字。

我的服务器代码:

import { Meteor } from 'meteor/meteor';
import "../imports/api/salaDeVenta.js"
import "../imports/api/dispositivo.js"
import "../imports/api/empresa.js"

import {SalaDeVenta} from "../imports/api/salaDeVenta.js";
import { Dispositivo } from "../imports/api/dispositivo.js";

Meteor.startup(() => {
// @ts-nocheck
var Collections = require('typescript-collections');
var dictScheduling =  new Collections.MultiDictionary(); //HAY QUE GUARDAR LOS J ACA
var dict =  new Collections.Dictionary();

var socketVar = null
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');
var portToListen = 8080
var server = app.listen({
  host: '127.0.0.1',
  port: 8080,
}, function(data) {
  console.log('------CALLBACKS DATA: ' , data)
  var host = server.address().address;
  var portp = server.address().port;
  console.log('Example app listening at http://%s:%s', host, portp);
});
//SCHEDULING <------------------------
var schedule = require('node-schedule');

var salasDeVenta = SalaDeVenta.find({}).fetch()
//console.log(salasDeVenta)
salasDeVenta.map((salaDeVenta,salaDeVentai)=>{
 //console.log(salaDeVenta.name)
 //para el schedule de despertar
 var dispositivoEnCabeza = Dispositivo.find({ idSalaDeVentas: salaDeVenta._id, dispCabeza: true}).fetch()
    dispositivoEnCabeza.map((dispEnCabeza,dispEnCabezai)=>{
        //console.log(dispEnCabeza.especificaciones + dispEnCabeza.direccionMAC)
        var res = salaDeVenta.turnOnHour.split(":") // PUEDE QUE FALLE AL NO HABER HECHO TRIM DEL 0
        var dispositivosPorSalaDeVenta = Dispositivo.find({ idSalaDeVentas: salaDeVenta._id}).fetch()
       // console.log(dispositivosPorSalaDeVenta);
        var j1 = schedule.scheduleJob({hour: res[0], minute: res[1]}, function(){
         // console.log("HASTA")
           //EXCUTE WAKE ON LAN PROTOCOL
           var dispositivosALevantar = dispositivosPorSalaDeVenta
           //console.log("DONDE")
           dispositivosPorSalaDeVenta.map((dispActual,dispActuali)=>{
            //console.log(dispEnCabeza.especificaciones + dispEnCabeza.direccionMAC)
              dict.getValue(dispEnCabeza.direccionMAC).emit('wakePCByMAC',dispActual.direccionMAC) 
           });

          });
        j1.disp = dispEnCabeza.especificaciones;
        dictScheduling.setValue(salaDeVenta._id,j1)
        console.log("wakeonlan scheduled for device with MAC ADDRESS " + dispEnCabeza.direccionMAC + " at "+salaDeVenta.turnOnHour);

    });
//para el schedule de apagar
var dispositivosPorSalaDeVenta = Dispositivo.find({ idSalaDeVentas: salaDeVenta._id}).fetch()
dispositivosPorSalaDeVenta.map((dispActual,dispActuali)=>{
  var res = salaDeVenta.turnOffHour.split(":") // PUEDE QUE FALLE AL NO HABER HECHO TRIM DEL 0
  var j2 = schedule.scheduleJob({hour: res[0], minute: res[1]}, function(){
     //EXCUTE SLEEP PROTOCOL
     dict.getValue(dispActual.direccionMAC).emit('sleepPCByMAC')
    });
  j2.disp = dispActual.especificaciones;
  dictScheduling.setValue(salaDeVenta._id,j2)
  console.log("sleep scheduled for device with MAC ADDRESS " + dispActual.direccionMAC + " at "+salaDeVenta.turnOffHour);

});

});


//-------------TEST-------------------

//No puedee ser 00 el tiempo, tiene que ser 0
//Cuando se corre el servidor por primera vez, la libreria usa la hora del sistema en ESE momento, por lo que cambiar la hora del sistema luego de haber iniciado el servidor no tiene ningun efecto
var j = schedule.scheduleJob({hour: 10, minute: 02}, function(){
  console.log("THIS IS IT");
  });

//-------------------------------------


  //SOCKETS XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.on('connection',Meteor.bindEnvironment((socket)=> {
  var maciD;
  console.log("connection attempt...")
  socketVar = socket;
  socketVar.emit('IdentifySocket')
  socketVar.on('MACIdentification',  Meteor.bindEnvironment((MACIdentification) =>{
    console.log('The client with MAC address ' + MACIdentification + ' has joined the server' );
    dict.setValue(MACIdentification,socketVar);
    dict.getValue(MACIdentification).emit('SocketValidationTest')
    Meteor.call('dispositivos.updateConnection',MACIdentification,true)
    maciD = MACIdentification
  }));
  socketVar.on('disconnect', Meteor.bindEnvironment(function() {
    console.log('The client with MAC address ' + maciD + ' has disconnected')
    Meteor.call('dispositivos.updateConnection',maciD,false)

  }));  
}));


  //METHODS
  Meteor.methods({

    foo: function () {
        return 1;
    },

    bar: function () {
    // QUESTION: HOW TO CALL Meteor.methods.foo

        return 1 + foo;

    },

    'socket.sendWakeMessageToMAC':function(pconMac){

      var res = pconMac.split("-")
      console.log("DIR MAC CABEZA "+ res[0])
      console.log("DIR MAC TO "+ res[1])
      dict.getValue(res[0]).emit('wakePCByMAC',res[1])
      //socketVar.emit('wakePCByMAC', DirecconMAC);
  },

  'socket.sendSleepMessageToMAC':function(DirecconMAC){
    dict.getValue(DirecconMAC).emit('sleepPCByMAC')
    //socketVar.emit('sleepPCByMAC', DirecconMAC);
},

'schedule.updateEncendidoYApagadoBySalaDeVentas':function(idSalaDeVentas){
  console.log("-------RESCHEDULING-----------------");
  dictScheduling.getValue(idSalaDeVentas).forEach(cancelSchedules);
  function cancelSchedules(item, index) {
    console.log(item.disp + "cancelled")
    item.cancel();
  }
  dictScheduling.remove(idSalaDeVentas)
  var salasDeVenta = SalaDeVenta.find({_id: idSalaDeVentas}).fetch()
  salasDeVenta.map((salaDeVenta,salaDeVentai)=>{
   //para el schedule de despertar
   var dispositivoEnCabeza = Dispositivo.find({ idSalaDeVentas: salaDeVenta._id, dispCabeza: true}).fetch()
      dispositivoEnCabeza.map((dispEnCabeza,dispEnCabezai)=>{
          var res = salaDeVenta.turnOnHour.split(":") // PUEDE QUE FALLE AL NO HABER HECHO TRIM DEL 0
          var dispositivosPorSalaDeVenta = Dispositivo.find({ idSalaDeVentas: salaDeVenta._id}).fetch()
          var j1 = schedule.scheduleJob({hour: res[0], minute: res[1]}, function(){
             //EXCUTE WAKE ON LAN PROTOCOL
             var dispositivosALevantar = dispositivosPorSalaDeVenta
             dispositivosPorSalaDeVenta.map((dispActual,dispActuali)=>{
                dict.getValue(dispEnCabeza.direccionMAC).emit('wakePCByMAC',dispActual.direccionMAC) 
             });

            });
            j1.disp = dispEnCabeza.especificaciones;
          dictScheduling.setValue(salaDeVenta._id,j1)
          console.log("wakeonlan scheduled for device with MAC ADDRESS " + dispEnCabeza.direccionMAC + " at "+salaDeVenta.turnOnHour);

      });
  //para el schedule de apagar
  var dispositivosPorSalaDeVenta = Dispositivo.find({ idSalaDeVentas: salaDeVenta._id}).fetch()
  dispositivosPorSalaDeVenta.map((dispActual,dispActuali)=>{
    var res = salaDeVenta.turnOffHour.split(":") // PUEDE QUE FALLE AL NO HABER HECHO TRIM DEL 0
    var j2 = schedule.scheduleJob({hour: res[0], minute: res[1]}, function(){
       //EXCUTE SLEEP PROTOCOL
       dict.getValue(dispActual.direccionMAC).emit('sleepPCByMAC')
      });
      j2.disp = dispActual.especificaciones;
    dictScheduling.setValue(salaDeVenta._id,j2)
    console.log("sleep scheduled for device with MAC ADDRESS " + dispActual.direccionMAC + " at "+salaDeVenta.turnOffHour);

  });

  });

  console.log("-------ReschedulingOVER-----------------");
},

 });

});

我的客户代码:

import { Meteor } from 'meteor/meteor';
import io from 'socket.io-client';

Meteor.startup(() => {
// code to run on server at startup

//WAKE UP PC BY MAC When message is received through socket
const socket = io('http://hwmonitool.herokuapp.com/');

  console.log("relaunch")
  socket.on('wakePCByMAC', function (MacAddressReceived) {
    console.log(MacAddressReceived);
    const wol = require('wakeonlan')

    // MAC is case-insensitive. colons optional
     wol(MacAddressReceived).then(() => {
     console.log('wol sent!')

  })
  },

  //SLEEPS PC when message is received rhoeough PC
  );
  socket.on('sleepPCByMAC', function () {
    var cmd=require('node-cmd');
    cmd.run('rundll32.exe powrprof.dll,SetSuspendState 0,1,0');
  },
  );

  //IDENTIFIES the computer
  socket.on('IdentifySocket', function () {
    var macaddress = require('macaddress');

    macaddress.one('Ethernet', function (err, mac) {
      console.log("Mac address for Ethernet: %s", mac);  
      socket.emit('MACIdentification', mac)
    });
  },
  );

 //TEST 
socket.on('SocketValidationTest', function () {
  console.log('AIGHT')
},
);



  //METHODS
  Meteor.methods({

    foo: function () {
      console.log(":(")
      var cmd=require('node-cmd');
      cmd.run('rundll32.exe powrprof.dll,SetSuspendState 0,1,0');
        return 1;
    },

    bar: function () {

    // QUESTION: HOW TO CALL Meteor.methods.foo

    return 1 + foo;        

    },

    'socket.sendMessage':function(){
      socket.emit('my other event', { message: 'message' });
  },
 });
}
);

每当我运行客户端应用程序时,我都会尝试进行连接,因为heroku的服务器应用程序开始打印以下行。

2019-09-26T20:33:23.248253+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=3&transport=polling&t=MrlHqdj&b64=1" host=hwmonitool.herokuapp.com request_id=815b548d-dbd2-4fc4-8c07-4d26d5356058 fwd="190.85.77.232" dyno=web.1 connect=0ms service=4ms status=200 bytes=2026 protocol=http
2019-09-26T20:33:23.399404+00:00 heroku[router]: at=info method=POST path="/socket.io/?EIO=3&transport=polling&t=MrlHqg9&b64=1" host=hwmonitool.herokuapp.com request_id=419aa329-cbdb-48bc-bc7c-0bf7ecec20ca fwd="190.85.77.232" dyno=web.1 connect=0ms service=5ms status=200 bytes=2026 protocol=http
2019-09-26T20:33:28.412723+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=3&transport=polling&t=MrlHruI&b64=1" host=hwmonitool.herokuapp.com request_id=96dcbfcb-4c3f-4e36-9125-76af5bba3f52 fwd="190.85.77.232" dyno=web.1 connect=1ms service=8ms status=200 bytes=2026 protocol=http
2019-09-26T20:33:28.547917+00:00 heroku[router]: at=info method=POST path="/socket.io/?EIO=3&transport=polling&t=MrlHrwk&b64=1" host=hwmonitool.herokuapp.com request_id=2c3bb984-9076-4b9f-a45b-e6883f12efe5 fwd="190.85.77.232" dyno=web.1 connect=0ms service=3ms status=200 bytes=2026 protocol=http
2019-09-26T20:33:33.566847+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=3&transport=polling&t=MrlHt8t&b64=1" host=hwmonitool.herokuapp.com request_id=f6b7897a-18bd-4964-8041-f50d44574d53 fwd="190.85.77.232" dyno=web.1 connect=1ms service=4ms status=200 bytes=2026 protocol=http
2019-09-26T20:33:33.719816+00:00 heroku[router]: at=info method=POST path="/socket.io/?EIO=3&transport=polling&t=MrlHtBO&b64=1" host=hwmonitool.herokuapp.com request_id=65ceb84b-328a-4f45-9b35-efa735c465b0 fwd="190.85.77.232" dyno=web.1 connect=1ms service=10ms status=200 bytes=2026 protocol=http
2019-09-26T20:33:38.729319+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=3&transport=polling&t=MrlHuPY&b64=1" host=hwmonitool.herokuapp.com request_id=1484a69f-bd67-4ad9-a03a-4c91d846c67b fwd="190.85.77.232" dyno=web.1 connect=1ms service=8ms status=200 bytes=2026 protocol=http
2019-09-26T20:33:38.881143+00:00 heroku[router]: at=info method=POST path="/socket.io/?EIO=3&transport=polling&t=MrlHuS3&b64=1" host=hwmonitool.herokuapp.com request_id=3adf54da-612b-4890-804d-f8701d5c67ea fwd="190.85.77.232" dyno=web.1 connect=0ms service=5ms status=200 bytes=2026 protocol=http
2019-09-26T20:33:43.882337+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=3&transport=polling&t=MrlHvgC&b64=1" host=hwmonitool.herokuapp.com request_id=ad78dfac-1cac-4d25-9941-97fe29074538 fwd="190.85.77.232" dyno=web.1 connect=0ms service=3ms status=200 bytes=2026 protocol=http
2019-09-26T20:33:44.030293+00:00 heroku[router]: at=info method=POST path="/socket.io/?EIO=3&transport=polling&t=MrlHviW&b64=1" host=hwmonitool.herokuapp.com request_id=adef9a95-a249-4c03-bdb4-1b224e50bf63 fwd="190.85.77.232" dyno=web.1 connect=0ms service=5ms status=200 bytes=2026 protocol=http
2019-09-26T20:33:49.039047+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=3&transport=polling&t=MrlHwwg&b64=1" host=hwmonitool.herokuapp.com request_id=2427bba1-48a1-4a49-9aa6-4804716a4fbd fwd="190.85.77.232" dyno=web.1 connect=1ms service=4ms status=200 bytes=2026 protocol=http
2019-09-26T20:33:49.200997+00:00 heroku[router]: at=info method=POST path="/socket.io/?EIO=3&transport=polling&t=MrlHwz7&b64=1" host=hwmonitool.herokuapp.com request_id=619d9b26-b2c6-4851-a16b-4c44c162a01a fwd="190.85.77.232" dyno=web.1 connect=0ms service=6ms status=200 bytes=2026 protocol=http
2019-09-26T20:33:54.197094+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=3&transport=polling&t=MrlHyBG&b64=1" host=hwmonitool.herokuapp.com request_id=413a834d-85e2-475b-9a0a-ddf3a4a8bcab fwd="190.85.77.232" dyno=web.1 connect=0ms service=5ms status=200 bytes=2026 protocol=http
2019-09-26T20:33:54.348955+00:00 heroku[router]: at=info method=POST path="/socket.io/?EIO=3&transport=polling&t=MrlHyDh&b64=1" host=hwmonitool.herokuapp.com request_id=3d3105b5-35b4-4422-858f-519df67a1125 fwd="190.85.77.232" dyno=web.1 connect=0ms service=4ms status=200 bytes=2026 protocol=http
2019-09-26T20:33:59.560996+00:00 heroku[router]: at=info method=POST path="/socket.io/?EIO=3&transport=polling&t=MrlHzVC&b64=1" host=hwmonitool.herokuapp.com request_id=189a264a-a244-4632-b64b-31c50a90dd34 fwd="190.85.77.232" dyno=web.1 connect=1ms service=4ms status=200 bytes=2026 protocol=http
2019-09-26T20:33:59.414639+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=3&transport=polling&t=MrlHzRq&b64=1" host=hwmonitool.herokuapp.com request_id=8e26e925-449d-409a-b46c-ac4660ba8789 fwd="190.85.77.232" dyno=web.1 connect=0ms service=8ms status=200 bytes=2026 protocol=http
2019-09-26T20:34:04.570754+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=3&transport=polling&t=MrlH-jM&b64=1" host=hwmonitool.herokuapp.com request_id=a82e8a58-b9aa-40b6-9cd6-a46c61d65e50 fwd="190.85.77.232" dyno=web.1 connect=1ms service=7ms status=200 bytes=2026 protocol=http
2019-09-26T20:34:04.720824+00:00 heroku[router]: at=info method=POST path="/socket.io/?EIO=3&transport=polling&t=MrlH-ln&b64=1" host=hwmonitool.herokuapp.com request_id=c305a604-da21-42b0-ba19-348afc89054a fwd="190.85.77.232" dyno=web.1 connect=0ms service=5ms status=200 bytes=2026 protocol=http
2019-09-26T20:34:09.723010+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=3&transport=polling&t=MrlH_zv&b64=1" host=hwmonitool.herokuapp.com request_id=0d4a471f-6012-414d-bbab-0447b545bec0 fwd="190.85.77.232" dyno=web.1 connect=0ms service=8ms status=200 bytes=2026 protocol=http
2019-09-26T20:34:09.882613+00:00 heroku[router]: at=info method=POST path="/socket.io/?EIO=3&transport=polling&t=MrlI00J&b64=1" host=hwmonitool.herokuapp.com request_id=c44c6d64-8bb7-473d-8ff3-ddf4b42c6f1d fwd="190.85.77.232" dyno=web.1 connect=1ms service=13ms status=200 bytes=2026 protocol=http
2019-09-26T20:34:14.875853+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=3&transport=polling&t=MrlI1ER&b64=1" host=hwmonitool.herokuapp.com request_id=72f083b6-d06b-4504-bf5d-e45a433ec23c fwd="190.85.77.232" dyno=web.1 connect=0ms service=5ms status=200 bytes=2026 protocol=http
2019-09-26T20:34:15.030964+00:00 heroku[router]: at=info method=POST path="/socket.io/?EIO=3&transport=polling&t=MrlI1Go&b64=1" host=hwmonitool.herokuapp.com request_id=673699c2-0851-47b2-b60e-f8997cde5361 fwd="190.85.77.232" dyno=web.1 connect=1ms service=8ms status=200 bytes=2026 protocol=http
2019-09-26T20:34:20.033573+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=3&transport=polling&t=MrlI2Uw&b64=1" host=hwmonitool.herokuapp.com request_id=a9da6da5-396b-4753-9e5d-512493c53475 fwd="190.85.77.232" dyno=web.1 connect=1ms service=9ms status=200 bytes=2026 protocol=http
2019-09-26T20:34:20.196098+00:00 heroku[router]: at=info method=POST path="/socket.io/?EIO=3&transport=polling&t=MrlI2XR&b64=1" host=hwmonitool.herokuapp.com request_id=c722107d-99ab-450b-84e6-d1b5109085e7 fwd="190.85.77.232" dyno=web.1 connect=1ms service=12ms status=200 bytes=2026 protocol=http
2019-09-26T20:34:25.191264+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=3&transport=polling&t=MrlI3la&b64=1" host=hwmonitool.herokuapp.com request_id=c9a6ab60-aab4-46d2-9dff-48e79e542f4b fwd="190.85.77.232" dyno=web.1 connect=1ms service=5ms status=200 bytes=2026 protocol=http
2019-09-26T20:34:25.351664+00:00 heroku[router]: at=info method=POST path="/socket.io/?EIO=3&transport=polling&t=MrlI3n-&b64=1" host=hwmonitool.herokuapp.com request_id=660c977f-3b42-4878-b912-fccc5a8fb67a fwd="190.85.77.232" dyno=web.1 connect=1ms service=10ms status=200 bytes=2026 protocol=http
2019-09-26T20:34:30.346371+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=3&transport=polling&t=MrlI508&b64=1" host=hwmonitool.herokuapp.com request_id=8225df63-8b7b-4105-a6c1-95bcd8069616 fwd="190.85.77.232" dyno=web.1 connect=0ms service=3ms status=200 bytes=2026 protocol=http
2019-09-26T20:34:30.495179+00:00 heroku[router]: at=info method=POST path="/socket.io/?EIO=3&transport=polling&t=MrlI52Z&b64=1" host=hwmonitool.herokuapp.com request_id=4f37b176-c3cc-4a19-a986-5cab9e9e5522 fwd="190.85.77.232" dyno=web.1 connect=0ms service=5ms status=200 bytes=2026 protocol=http

但是,从来没有建立连接。 socket-io调试系统提供的信息不存在。我不知道可能是问题的根源,而且我的想法也用光了。对于可能是什么问题以及如何解决这个问题,我将不胜感激。谢谢!

0 个答案:

没有答案