esprfid / esp-rfid

ESP8266 RFID (RC522, PN532, Wiegand, RDM6300) Access Control system featuring WebSocket, JSON, NTP Client, Javascript, SPIFFS
MIT License
1.35k stars 423 forks source link

Issue on OSX Sierra #17

Closed ivanlunardi closed 7 years ago

ivanlunardi commented 7 years ago

I have a problem with websocket on OSX Sierra! " the message sent directly in websock.onopen doesent work , if you try by console it work"

Ivan

omersiar commented 7 years ago

Hi,

Can you share Safari version? And are you building esp-rfid from source or flashing compiled binaries?

ivanlunardi commented 7 years ago

Hi, Safari Version 10.1.2 (12603.3.8) Chrome Version 60.0.3112.113 I tried from sourso and with binaries. I Tried also with other Mac with same issues.

Ivan

omersiar commented 7 years ago

Do you get same behaviour on chrome too? Does it same for every page ? (logs users settings)

ivanlunardi commented 7 years ago

Yes I Do, the issue is present in every page. Ivan

omersiar commented 7 years ago

goto HTML editor "http://ipaddressofESP-RFID/edit" then select "log.js" delete everything paste below code and hit CTRL+S to save the file and goto "Logs" section.

var websock;
var timezone;
var devicetime;
var userdata = {};

function twoDigits(value) {
   if(value < 10) {
    return "0" + value;
   }
   return value;
}

function initTable() {
  jQuery(function($) {
    FooTable.init("#latestlogtable", {
        columns: [
            {
            "name": "timestamp",
            "title": "Date",
            "parser": function(value) {
              var vuepoch = new Date(value * 1000);
              var formatted = vuepoch.getUTCFullYear()
                  + "-" + twoDigits(vuepoch.getUTCMonth() + 1) 
                  + "-" + twoDigits(vuepoch.getUTCDate())
                  + "-" + twoDigits(vuepoch.getUTCHours())
                  + ":" + twoDigits(vuepoch.getUTCMinutes())
                  + ":" + twoDigits(vuepoch.getUTCSeconds());
                return formatted;
            },
            "sorted": true,
            "direction": "DESC"
            },
            {
            "name": "uid",
            "title": "UID",
            "type": "text",
          },
          {
            "name": "username",
            "title": "User Name or Label"
          }
        ],
        rows: userdata
      });
  });
}

function start() {
  websock = new WebSocket("ws://" + window.location.hostname + "/ws");
  websock.onopen = function() {
    alert("WebSocket Opened");
    var commandtosend = {};
    commandtosend.command = "latestlog";
    alert("Now i will try to send latestlog command");
    websock.send(JSON.stringify(commandtosend));
    commandtosend = {};
    commandtosend.command = "gettime";
    alert("Now i will try to send gettime command");
    websock.send(JSON.stringify(commandtosend));
  };
  websock.onclose = function(evt) {};
  websock.onerror = function(evt) {
    console.log(evt);
  };
  websock.onmessage = function(evt) {
    var obj = JSON.parse(evt.data);
    if (obj.type === "latestlog") {
      userdata = obj.list;
      initTable();
      document.getElementById("loading-img").style.display = "none";
    } else if (obj.command === "gettime") {
      timezone = obj.timezone;
      devicetime = obj.epoch;
    }
  };
}
ivanlunardi commented 7 years ago

I find the solution, the problem is the variable socket you must set it to null in declaration var websock; -> var websock = null;

I tried with a demo of node application and i had the same problem.

Ivan

omersiar commented 7 years ago

Thank you finding this bug. Kind regards.