mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-20 20:29:06 +00:00
Merge pull request #3 from Monadical-SAS/jdc/webrtc-connection
WebRTC server connection implementation
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import Peer from "simple-peer";
|
import Peer from "simple-peer";
|
||||||
|
|
||||||
|
const WebRTC_SERVER_URL = "http://127.0.0.1:1250/offer"
|
||||||
|
|
||||||
const useWebRTC = (stream) => {
|
const useWebRTC = (stream) => {
|
||||||
const [data, setData] = useState(null);
|
const [data, setData] = useState(null);
|
||||||
|
|
||||||
@@ -11,7 +13,24 @@ const useWebRTC = (stream) => {
|
|||||||
// This is where you'd send the signal data to the server.
|
// This is where you'd send the signal data to the server.
|
||||||
// The server would then send it back to other peers who would then
|
// The server would then send it back to other peers who would then
|
||||||
// use `peer.signal()` method to continue the connection negotiation.
|
// use `peer.signal()` method to continue the connection negotiation.
|
||||||
console.log("signal", data);
|
if ('sdp' in data) {
|
||||||
|
fetch(WebRTC_SERVER_URL, {
|
||||||
|
body: JSON.stringify({
|
||||||
|
sdp: data.sdp,
|
||||||
|
type: data.type,
|
||||||
|
}),
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
method: 'POST'
|
||||||
|
}).then(function (response) {
|
||||||
|
return response.json();
|
||||||
|
}).then(function (answer) {
|
||||||
|
return peer.signal(answer);
|
||||||
|
}).catch(function (e) {
|
||||||
|
alert(e);
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
peer.on("connect", () => {
|
peer.on("connect", () => {
|
||||||
@@ -20,6 +39,7 @@ const useWebRTC = (stream) => {
|
|||||||
|
|
||||||
peer.on("data", (data) => {
|
peer.on("data", (data) => {
|
||||||
// Received data from the server.
|
// Received data from the server.
|
||||||
|
console.log(data.toString())
|
||||||
const serverData = JSON.parse(data.toString());
|
const serverData = JSON.parse(data.toString());
|
||||||
setData(serverData);
|
setData(serverData);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user