如何在服务器上编写videoJS的错误消息对象

How to write error message object of videoJS on server

我正在使用 videojs 播放视频。我想在服务器上记录错误消息。在浏览器控制台中,我能够收到消息,但我没有收到可以发送到服务器的错误对象。需要帮助 。测试代码将非常有帮助。

 player=videojs("myvideo", { "controls": true,  "techOrder": ["html5", "flash"]}, function(){
    console.log("here");


    var err =this.on('error', function(e){
    console.log("Error n caught" +this.error());
    console.log(this.error());  // Is not printing anything



});

});

我认为问题出在错误处理程序中的匿名函数,它无法访问 'this.' 的值 查看绑定 'this' 是否有效:

var err =this.on('error', function(e){
  console.log("Error n caught" +this.error());
  console.log(this.error());  // Is not printing anything
}.bind(this));

我找到了答案:

   this.player().on('error', function(e) {
      console.log(e);
       e.stopImmediatePropagation();
        var error = this.player().error();
        console.log('error!', error.code, error.type , error.message);
});         

不,为了向服务器发送任何东西,服务器需要有一个接口。

你的服务器端是什么样的?

在我的日志记录功能中,我在 客户端使用 jquery 向服务器 post 记录和错误消息:

$.post( your_url_to_the_server, { entry: your_log_error_data }, function(data){ /* do something after the message was sent */}); 

服务器端你可以使用PHP,例如,可以在给定的URL下达到(见"your_url_to_the_server"客户端代码示例):

if (isset($_POST['entry'])) {
 write_log($_POST['entry']);
}

//
function write_log($message) {
 $logfile= "my-log-file.log";
  // Append to the log file
  if($fd = @fopen($logfile, "a")) {
    $result = fputs($fd, $message);
    fclose($fd);

    if($result > 0)
      return true;  
    else
      return 'Unable to write to '.$logfile.'!';
  }
  else {
    return 'Unable to open log '.$logfile.'!';
  }
}