node.js - 结合express设置peerJS服务器

标签 node.js express peerjs

我正在尝试按照https://github.com/peers/peerjs-server#combining-with-existing-express-app上的自述文件设置我自己的peerJS服务器

我在服务器上的代码

port = process.env.PORT or 8080
http = require 'http'
express = require 'express'
app = express()
server = http.createServer app
app.use '/peerjs', ExpressPeerServer server, debug : on
server.listen port
server.listen 9000

我在客户端的代码

peer = new Peer
    host : 'localhost'
    port : 9000
    secure : no
    config :
        iceServers : [ url : 'stun:stun.l.google.com:19302' ]

我在客户端控制台上收到此错误

GET http://localhost:9000/peerjs/peerjs/id?ts=14150106969530.4679094860330224 net::ERR_CONNECTION_REFUSED

最佳答案

如果您仍在寻找建立自己的对等服务器的方法,那么我所做的就是使它适合我。

server.js

// initialize express
var express = require('express');
var app = express();
// create express peer server
var ExpressPeerServer = require('peer').ExpressPeerServer;

var options = {
    debug: true
}

// create a http server instance to listen to request
var server = require('http').createServer(app);

// peerjs is the path that the peerjs server will be connected to.
app.use('/peerjs', ExpressPeerServer(server, options));
// Now listen to your ip and port.
server.listen(8878, "192.168.1.14");

客户端代码

我想,你应该不会有太多问题,但如果你想知道为某些参数设置什么,那么这里是对等对象的初始化:

var peer = new Peer({
    host: '192.168.1.14',
    port: 8878,
    path: '/peerjs',
    config: { 'iceServers': [
    { url: 'stun:stun01.sipphone.com' },
    { url: 'stun:stun.ekiga.net' },
{ url: 'stun:stunserver.org' },
{ url: 'stun:stun.softjoys.com' },
{ url: 'stun:stun.voiparound.com' },
{ url: 'stun:stun.voipbuster.com' },
{ url: 'stun:stun.voipstunt.com' },
{ url: 'stun:stun.voxgratia.org' },
{ url: 'stun:stun.xten.com' },
{
    url: 'turn:192.158.29.39:3478?transport=udp',
    credential: 'JZEOEt2V3Qb0y27GRntt2u2PAYA=',
    username: '28224511:1379330808'
},
{
    url: 'turn:192.158.29.39:3478?transport=tcp',
    credential: 'JZEOEt2V3Qb0y27GRntt2u2PAYA=',
    username: '28224511:1379330808'
    }
  ]
   },

debug: 3
});

这应该可以帮助您建立连接。

关于node.js - 结合express设置peerJS服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26712426/

相关文章:

node.js - 修改expressjs中间件中的req.path

javascript - Webpack 在包输出中导致语法错误

node.js - 在 AWS Lambda 函数中通过 AWS SES 发送电子邮件

node.js - Node.js 中的响应顺序?

mysql - Nodejs和mysql数据库时区问题

css - 虽然 Ejs 文件没有看到 css 样式?

javascript - PeerJS:是否可以同时传输调用和数据?

javascript - PeerJS 文字聊天

typescript - 当其类型定义未声明模块时如何在 TypeScript 中导入库?

node.js - Mongoose - REST API - 查询不同模型的模式