我正在尝试使用WebRTC和WebSockets进行信令开发视频聊天应用程序。
我的问题是我不知道创建RTCPeerConnection并通过WebSockets连接两个对等方(2个浏览器)的确切过程(至少在本地)。
我知道如何通过WebSockets与客户端通信,但不知道如何使用RTCPeerConnection API,你知道任何逐步解释此过程(Offer SDP、answers、ICE...)的教程吗?另外,管理这些客户端通过RTCPeerConnection的服务器代码是什么样子?
以下是我的服务器的Node.js代码。
我知道如何通过WebSockets与客户端通信,但不知道如何使用RTCPeerConnection API,你知道任何逐步解释此过程(Offer SDP、answers、ICE...)的教程吗?另外,管理这些客户端通过RTCPeerConnection的服务器代码是什么样子?
以下是我的服务器的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);
});
});