WebRTC通过Websockets进行视频聊天 [英] WebRTC videochat through Websockets

查看:100
本文介绍了WebRTC通过Websockets进行视频聊天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用webRTC和WebSockets开发视频聊天应用程序来发送信号。
我的问题是我不确切知道创建RTCPeerConnection的过程是什么,并通过webSocket连接两个对等体(2个浏览器)(至少在本地)。

I'm trying to develop a video-chat application using webRTC and WebSockets for signaling. 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).

我知道如何通过WebSockets与客户进行通信,但不知道如何通过RTCPeerConnection API进行通信,您是否知道任何教程一步一步解释过程?(提供SDP,答案,ICE,...),另一方面,如何查看服务器代码通过RTCPeerConnection管理这些客户端?

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?

这是我的服务器的Node.js代码

Here is my Node.js code for the server

"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);
    });

});


推荐答案

您是否看过或遇到过 WebRTC.io ?它是一个开源GitHub项目,它利用Node.js和websockets来完成你正在谈论的事情。我,不是一个javascript的人,能够在一周内弄清楚它在做什么。它不是一步一步的指令,但任何具有javascript经验的人都能够找出函数调用顺序。

Have you looked at or come across WebRTC.io? It is an opensource GitHub project that utilizes Node.js and websockets to do the exact thing you are talking about. I, not being much of a javascript person, was able to figure out what it was doing within a week. It isn't a step by step instruction, but anyone with javascript experience would be able to figure out the function call order.

代码有两位: 服务器端客户端。服务器端使用Node.js运行,并将客户端代码提供给浏览器。如果我没记错的话,由于这两个项目是分开的,如果要组合它们,则必须从客户端复制webrtcio.js文件并将其粘贴到服务器端文件夹中。虽然,我认为如果您正确克隆github存储库,您可能不必担心这一点。

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. 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. Although, I think if you clone the github repository right, you might not have to worry about that.

这篇关于WebRTC通过Websockets进行视频聊天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆