peerJS应用

构建自己peerjs服务器

webrtc的peerjs教程和插件下载

服务器.js

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.
const peerServer = ExpressPeerServer(server, options);
app.use('/peerjs', peerServer);
// Now listen to your ip and port.
server.listen(443, "192.168.2.18");

peerServer.on('connection', (client) => { 
  console.log('connection:'+client.getId());
});
peerServer.on('disconnect', (client) => { 
  console.log('disconnect:'+client.getId());
});

终端JS

function initialize() {
    peer = new Peer({
        host: '192.168.2.18',
        port: 443,
        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.168.191.128:3478',
                credential: 'password1',
                username: 'user1'
            },
            ]
        },
    
        debug: 3
    });
    peer.on('open', function (id) {
        // Workaround for peer.reconnect deleting previous id
        if (peer.id === null) {
            console.log('Received null id from peer open');
            peer.id = lastPeerId;
        } else {
            lastPeerId = peer.id;
        }

        console.log('ID: ' + peer.id);
    });
    
    peer.on('connection', function (c) {
        // Disallow incoming connections
        c.on('open', function() {
            c.send("Sender does not accept incoming connections");
            setTimeout(function() { c.close(); }, 500);
        });
    });
    peer.on('disconnected', function () {
        status.innerHTML = "Connection lost. Please reconnect";
        console.log('Connection lost. Please reconnect');

        // Workaround for peer.reconnect deleting previous id
        peer.id = lastPeerId;
        peer._lastServerId = lastPeerId;
        peer.reconnect();
    });
    peer.on('close', function() {
        conn = null;
        status.innerHTML = "Connection destroyed. Please refresh";
        console.log('Connection destroyed');
    });
    peer.on('error', function (err) {
        console.log(err);
        alert('' + err);
    });
};

iceServers 服务器搭建

turn服务器搭建教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的PeerJS的示例代码,用于在网页中进行P2P视频通信: ```html <!DOCTYPE html> <html> <head> <title>PeerJS Video Chat Demo</title> </head> <body> <video id="localVideo" autoplay></video> <video id="remoteVideo" autoplay></video> <script src="https://cdn.peerjs.com/0.6.1/peer.js"></script> <script> var peer = new Peer(); // 使用默认的随机ID // 当连接建立时 peer.on('open', function(id) { console.log('My peer ID is: ' + id); // 将本地视频流显示在video标签中 navigator.mediaDevices.getUserMedia({ video: true, audio: true }) .then(function(stream) { var localVideo = document.getElementById('localVideo'); localVideo.srcObject = stream; }) .catch(function(error) { console.error('Error accessing media devices: ', error); }); }); // 当有其他用户连接时 peer.on('connection', function(connection) { console.log('New connection received'); // 接受连接请求 connection.on('open', function() { console.log('Connection opened'); // 将远程视频流显示在video标签中 connection.on('stream', function(remoteStream) { var remoteVideo = document.getElementById('remoteVideo'); remoteVideo.srcObject = remoteStream; }); // 发送本地视频流给对方 navigator.mediaDevices.getUserMedia({ video: true, audio: true }) .then(function(stream) { connection.send(stream); }) .catch(function(error) { console.error('Error accessing media devices: ', error); }); }); }); </script> </body> </html> ``` 这段代码创建了一个PeerJS对象,当连接建立时,获取本地视频流并在一个video标签中播放。当有其他用户连接时,接受连接请求并将远程视频流显示在另一个video标签中,同时将本地视频流发送给对方。 你可以在两个不同的浏览器窗口或设备上打开这个网页,它们之间会建立P2P视频通信。注意,由于涉及到获取本地摄像头和麦克风权限,这段代码需要在HTTPS环境下运行,或者在localhost上运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值