WebRTC信令服务器使用Express& '网页套接字'包

时间:2016-04-08 16:06:33

标签: javascript node.js express websocket webrtc

我正在使用Rob Manson的 WebRTC入门中的以下代码。它是使用Node.js实现的。代码启动实时视频通话,我已按预期在Web浏览器中的两个选项卡之间运行。我只是想修改它,以便它使用Express而不是' http'包。

我遇到的问题是我的版本中没有显示视频。 '来电者'按预期工作,但随后“Callee”#39;停在"请等一下......连接你的电话......"信息。在浏览器控制台或我的终端中没有检测到错误,并且花了一天时间尝试解决问题我仍然不知道我哪里出错了。

这是原始信令服务器文件:

$("#search_result").html(data);

这是我修改过的文件。只是第一位被改变了:

// useful libs
var http = require("http");
var fs = require("fs");
var websocket = require("websocket").server;

// general variables
var port = 8000;
var webrtc_clients = [];
var webrtc_discussions = {};

// web server functions
var http_server = http.createServer(function(request, response) {
      var matches = undefined;
      if (matches = request.url.match("^/images/(.*)")) {
            var path = process.cwd()+"/images/"+matches[1];

            fs.readFile(path, function(error, data) {
              if (error) {
                      log_error(error);
              } else {
                      response.end(data);
              }
            });
      } else {
          response.end(page);
      }
});

http_server.listen(port, function() {
        log_comment("server listening (port "+port+")");
});

var page = undefined;
fs.readFile("basic_video_call.html", function(error, data) {
      if (error) {
          log_error(error);
      } else {
          page = data;
      }
});

// web socket functions
var websocket_server = new websocket({
  httpServer: http_server
});
websocket_server.on("request", function(request) {

  log_comment("new request ("+request.origin+")");

  var connection = request.accept(null, request.origin);
  log_comment("new connection ("+connection.remoteAddress+")");

  webrtc_clients.push(connection);
  connection.id = webrtc_clients.length-1;

  connection.on("message", function(message) {
    if (message.type === "utf8") {
      log_comment("got message "+message.utf8Data);

          var signal = undefined;
          try { signal = JSON.parse(message.utf8Data); } catch(e) { };

          if (signal) {
            if (signal.type === "join" && signal.token !== undefined) {
                  try {
                        if (webrtc_discussions[signal.token] === undefined) {
                                webrtc_discussions[signal.token] = {};
                        }
                  } catch(e) { };
                  try {
                      webrtc_discussions[signal.token][connection.id] = true;
                  } catch(e) { };
            } else if (signal.token !== undefined) {
                  try {
                        Object.keys(webrtc_discussions[signal.token]).forEach(function(id) {
                              if (id != connection.id) {
                                      webrtc_clients[id].send(message.utf8Data, log_error);
                              }
                        });
                  } catch(e) { };
            } else {
                log_comment("invalid signal: "+message.utf8Data);
            }
          } else {
                  log_comment("invalid signal: "+message.utf8Data);
          }
    }
  });

  connection.on("close", function(connection) {
        log_comment("connection closed ("+connection.remoteAddress+")"); 

        Object.keys(webrtc_discussions).forEach(function(token) {
              Object.keys(webrtc_discussions[token]).forEach(function(id) {
                if (id === connection.id) {
                    delete webrtc_discussions[token][id];
                }
              });
        });
  });
});

// utility functions
function log_error(error) {
  if (error !== "Connection closed" && error !== undefined) {
    log_comment("ERROR: "+error);
  }
}
function log_comment(comment) {
  console.log((new Date())+" "+comment);
}

此外,这是处理WebRTC调用的代码,我还没有改变:

// useful libs
var http = require("http");
var fs = require("fs");
var websocket = require("websocket").server;
var express = require('express');
var morgan = require('morgan');
var bodyParser = require('body-parser');

// general variables
var hostname = 'localhost';
var port = 8000;
var webrtc_clients = [];
var webrtc_discussions = {};

var expressApp = express();

expressApp.use(morgan('dev'));

var myRouter = express.Router();

myRouter.use(bodyParser.json());

// web server functions
myRouter.route('/').all(function(request,response,next) {
  var matches = undefined;
  if (matches = request.url.match("^/images/(.*)")) {
    var path = process.cwd() +"/images/"+matches[1];

    debugger;
    console.log("PATH: " + path);

    fs.readFile(path, function(error, data) {
      if (error) {
        log_error(error);
      } else {
        response.end(data);
      }
    });
  } else {
    response.end(page);
  }
});
//////////////////////


expressApp.use('/',myRouter);

expressApp.listen(port, hostname, function(){
  console.log(`Server running at http://${hostname}:${port}/`);
});

/////////////////////// **I CHANGED NOTHING BELOW HERE** ////////////

var page = undefined;
fs.readFile("basic_video_call.html", function(error, data) {
  if (error) {
    log_error(error);
  } else {
    page = data;
  }
});



// web socket functions
var websocket_server = new websocket({
  httpServer: expressApp
});

websocket_server.on("request", function(request) {


  log_comment("new request ("+request.origin+")");

  var connection = request.accept(null, request.origin);
  log_comment("new connection ("+connection.remoteAddress+")");

  webrtc_clients.push(connection);
  connection.id = webrtc_clients.length-1;

  connection.on("message", function(message) {
    if (message.type === "utf8") {
      log_comment("got message "+message.utf8Data);

      var signal = undefined;
      try { signal = JSON.parse(message.utf8Data); } catch(e) { };
      if (signal) {
        if (signal.type === "join" && signal.token !== undefined) {
          try {
            if (webrtc_discussions[signal.token] === undefined) {
              webrtc_discussions[signal.token] = {};
            }
          } catch(e) { };
          try {
            webrtc_discussions[signal.token][connection.id] = true;
          } catch(e) { };
        } else if (signal.token !== undefined) {
          try {
            Object.keys(webrtc_discussions[signal.token]).forEach(function(id) {
              if (id != connection.id) {
                webrtc_clients[id].send(message.utf8Data, log_error);
              }
            });
          } catch(e) { };
        } else {
          log_comment("invalid signal: "+message.utf8Data);
        }
      } else {
        log_comment("invalid signal: "+message.utf8Data);
      }
    }
  });

  connection.on("close", function(connection) {
    log_comment("connection closed ("+connection.remoteAddress+")");    
    Object.keys(webrtc_discussions).forEach(function(token) {
      Object.keys(webrtc_discussions[token]).forEach(function(id) {
        if (id === connection.id) {
          delete webrtc_discussions[token][id];
        }
      });
    });
  });
});

// utility functions
function log_error(error) {
  if (error !== "Connection closed" && error !== undefined) {
    log_comment("ERROR: "+error);
  }
}
function log_comment(comment) {
  console.log((new Date())+" "+comment);
}

我也对那些不使用Express但仍支持WebRTC身份验证的解决方案持开放态度。非常感谢你的帮助。

0 个答案:

没有答案