如何将signalmaster集成到已有的expressjs服务器上

时间:2015-08-13 15:57:27

标签: javascript node.js express webrtc

我尝试在我的应用中使用simplewebrtc,我已经拥有一个带有快速Web框架的简单nodejs服务器。但要使用simpleWebrtc,我们必须安装signal master。我正在查看信号主数据包中server.js文件的源代码,但我无法弄清楚如何将这个server.js与我现有的app.js文件结合起来。这基本上是我的app.js

var express = require('express'); 
var app = express(); 
var http = require('http').Server(app); 
var io = require('socket.io')(http);
var mongoose = require('mongoose');
var favicon = require('serve-favicon');
var bodyParser = require('body-parser');

app.get('/', function(req, res) {
    res.sendFile(__dirname + '/index.html'); 
    console.log("connected to index");
});

这是signalMaster的server.js

/*global console*/
var yetify = require('yetify'),
    config = require('getconfig'),
    uuid = require('node-uuid'),
    crypto = require('crypto'),
    fs = require('fs'),
    port = parseInt(process.env.PORT || config.server.port, 10),
    server_handler = function (req, res) {
        res.writeHead(404);
        res.end();
    },
    server = null;

// Create an http(s) server instance to that socket.io can listen to
if (config.server.secure) {
    server = require('https').Server({
        key: fs.readFileSync(config.server.key),
        cert: fs.readFileSync(config.server.cert),
        passphrase: config.server.password
    }, server_handler);
} else {
    server = require('http').Server(server_handler);
} 
server.listen(port);

var io = require('socket.io').listen(server);

if (config.logLevel) {
    // https://github.com/Automattic/socket.io/wiki/Configuring-Socket.IO
    io.set('log level', config.logLevel);
}

等等,您可以通过下载zip查看其余部分。我以为它只是用http替换服务器,但server = null并没有多大意义。所有依赖项都在signalMaster解压缩文件的目录中。我正在阅读有关signalMaster here的信息。

1 个答案:

答案 0 :(得分:1)

你需要这样的东西

var os = require('os');
    var static = require('node-static');
    var http = require('http');
    var socketIO = require('socket.io');

    var fileServer = new(static.Server)();

    var app = http.createServer(function (req, res) {
        fileServer.serve(req, res);
    }).listen(2013);

    var io = socketIO.listen(app);

    io.sockets.on('connection', function (socket){
     ...
     socket.on('join', function (message) {
      ...
     }
     ...
    }

我希望这有帮助你