简体   繁体   English

WebRTC通过Websockets进行视频聊天

[英]WebRTC videochat through Websockets

I'm trying to develop a video-chat application using webRTC and WebSockets for signaling. 我正在尝试使用webRTC和WebSockets开发视频聊天应用程序来发送信号。 My problem is that I don't know exactly what is the process of creating RTCPeerConnection and connect both peers(2 browsers) through webSocket(Locally at least). 我的问题是我不确切知道创建RTCPeerConnection的过程是什么,并通过webSocket(至少在本地)连接两个对等体(2个浏览器)。

I know how to communicate clients though WebSockets, but not with the RTCPeerConnection API, you know any tutorial step-by-step explaining the process?(Offer SDP, answers, ICE, ...) and, on the other hand, how looks the server code to managing these clients through RTCPeerConnection? 我知道如何通过WebSockets与客户进行通信,但不知道如何通过RTCPeerConnection API,你知道任何教程一步一步解释过程吗?(提供SDP,答案,ICE,...),另一方面,如何看起来通过RTCPeerConnection管理这些客户端的服务器代码?

Here is my Node.js code for the server 这是我的服务器的Node.js代码

"use strict";

// Optional. You will see this name in eg. 'ps' or 'top' command
process.title = 'node-webrtc';

// Port where we'll run the websocket server
var webSocketsServerPort = 1337;

// websocket and http servers
var webSocketServer = require('websocket').server;
var http = require('http');

 /* ---------------------------------

            GLOBAL VARIABLES

  ----------------------------------*/

// latest 100 messages
//var history = [ ];

// list of currently connected clients (users)
var clients = [ ];

 /* ---------------------------------

            HTTP SERVER

  ----------------------------------*/

var server = http.createServer(function(request, response) {
    // Not important for us. We're writing WebSocket server, not HTTP server
});
server.listen(webSocketsServerPort, function() {
    console.log((new Date()) + " Server is listening on port " + webSocketsServerPort);
});

 /* ---------------------------------

            WEBSOCKET SERVER

  ----------------------------------*/

var wsServer = new webSocketServer({
    // WebSocket server is tied to a HTTP server. WebSocket request is just
    // an enhanced HTTP request. For more info http://tools.ietf.org/html/rfc6455#page-6
    httpServer: server
});

// This callback function is called every time someone
// tries to connect to the WebSocket server
wsServer.on('request', function(request) {
    console.log((new Date()) + ' Connection from origin ' + request.origin + '.');

    // accept connection - you should check 'request.origin' to make sure that
    // client is connecting from your website
    // (http://en.wikipedia.org/wiki/Same_origin_policy)
    var connection = request.accept(null, request.origin); 
    // we need to know client index to remove them on 'close' event
    var index = clients.push(connection) - 1;

    console.log((new Date()) + ' Connection accepted.');


    // user sent some message
    connection.on('message', function(message) {
        for (var i=0; i < clients.length; i++) {
            clients[i].sendUTF(message);
        }   
    });


    // user disconnected
    connection.on('close', function(conn) {  
        console.log((new Date()) + " Peer " + conn.remoteAddress + " disconnected.");
        // remove user from the list of connected clients
        clients.splice(index, 1);
    });

});

Have you looked at or come across WebRTC.io ? 你看过或遇到过WebRTC.io吗? It is an opensource GitHub project that utilizes Node.js and websockets to do the exact thing you are talking about. 它是一个开源GitHub项目,它利用Node.js和websockets来完成你正在谈论的事情。 I, not being much of a javascript person, was able to figure out what it was doing within a week. 我,不是一个javascript的人,能够在一周内弄清楚它在做什么。 It isn't a step by step instruction, but anyone with javascript experience would be able to figure out the function call order. 它不是一步一步的指令,但任何具有javascript经验的人都能够找出函数调用顺序。

There are two bits to the code: the server side and the client side . 代码有两位: 服务器端客户端 The server side is run with Node.js, and serves up the client side code to the browser. 服务器端使用Node.js运行,并将客户端代码提供给浏览器。 If I remember correctly, since the two projects are separate, if you want to combine them you will have to copy the webrtcio.js file from the client side and paste it into the server side folder. 如果我没记错的话,由于这两个项目是分开的,如果要组合它们,则必须从客户端复制webrtcio.js文件并将其粘贴到服务器端文件夹中。 Although, I think if you clone the github repository right, you might not have to worry about that. 虽然,我认为如果您正确克隆github存储库,您可能不必担心这一点。

You might want to have a look at the codelab I did for Google I/O: bitbucket.org/webrtc/codelab . 您可能想看看我为Google I / O做的codelab: bitbucket.org/webrtc/codelab

Step 5 shows how to set up a signaling server using socket.io, and Step 6 puts this together with RTCPeerConnection to make a simple video chat app. 步骤5显示了如何使用socket.io设置信令服务器, 步骤6将其与RTCPeerConnection结合在一起,制作一个简单的视频聊天应用程序。

You might also want to have a look at easyRTC (full stack) and Signalmaster (signaling server created for SimpleWebRTC ). 您可能还想查看easyRTC (完整堆栈)和Signalmaster (为SimpleWebRTC创建的信令服务器)。

The 'canonical' WebRTC video chat example at apprtc.appspot.com uses XHR and the Google App Engine Channel API for signaling. apprtc.appspot.com上的“规范”WebRTC视频聊天示例使用XHR和Google App Engine Channel API进行信令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM