Skip to content

Commit b4d825d

Browse files
save file
1 parent 2ed2d6a commit b4d825d

File tree

1 file changed

+31
-205
lines changed

1 file changed

+31
-205
lines changed

utils/webrtc/webrtc-file-transfer-http/webrtc-file-transfer-http.html

Lines changed: 31 additions & 205 deletions
Original file line numberDiff line numberDiff line change
@@ -344,17 +344,13 @@ <h3>
344344

345345
var http = {};
346346
http.send = {};
347-
http.read = {};
347+
348348
var poll = {};
349349
poll.time = 1000;
350350
poll.max = 30_000;
351351
poll.timer = null;
352352

353-
/*
354-
var ws = {};
355-
ws.on = {};
356-
ws.send = {};
357-
*/
353+
358354

359355
var pc = {};
360356
pc.on = {};
@@ -364,6 +360,7 @@ <h3>
364360
var btn = {};
365361
btn.click = {};
366362
var create = {};
363+
var read = {};
367364
var send = {};
368365
var server = {};
369366
var kd = {};
@@ -490,7 +487,7 @@ <h3>
490487
btn['server-init'] = async function(){
491488

492489
focus();
493-
await http.send.init();
490+
await http.init();
494491

495492
}//server-init
496493

@@ -517,7 +514,7 @@ <h3>
517514
btn.read = async function(){
518515

519516
focus();
520-
http.send.read();
517+
http.read();
521518

522519
}//read
523520

@@ -764,8 +761,8 @@ <h3>
764761
//:
765762

766763

767-
http.send.init = async function(){
768-
764+
http.init = async function(){
765+
debug('http.init');
769766
var url = $(root,'#server').value;
770767
url += '/init';
771768

@@ -783,9 +780,9 @@ <h3>
783780

784781
}//catch
785782
if(err){
786-
console.log('http.send.init error');
783+
console.log('http.init error');
787784
console.error(err);
788-
ui.view.sys.red('http.send.init error');
785+
ui.view.sys.red('http.init error');
789786
ui.view.sys.red(err.toString());
790787
return;
791788
}
@@ -796,8 +793,8 @@ <h3>
796793
}//init
797794

798795

799-
http.send.offer = async function(offer){
800-
debug('http.send.offer');
796+
http.offer = async function(offer){
797+
debug('http.offer');
801798
var url = $(root,'#server').value;
802799
url += '/offer';
803800

@@ -814,8 +811,8 @@ <h3>
814811
}//offer
815812

816813

817-
http.send.answer = async function(answer){
818-
debug('http.send.answer');
814+
http.answer = async function(answer){
815+
debug('http.answer');
819816
var url = $(root,'#server').value;
820817
url += '/answer';
821818

@@ -832,8 +829,8 @@ <h3>
832829
}//answer
833830

834831

835-
http.send.ice = async function(ice){
836-
832+
http.ice = async function(ice){
833+
debug('http.ice');
837834
var url = $(root,'#server').value;
838835
url += '/ice';
839836
var headers = {mode};
@@ -847,8 +844,8 @@ <h3>
847844
}//ice
848845

849846

850-
http.send.read = async function(){
851-
847+
http.read = async function(){
848+
debug('http.read');
852849
var url = $(root,'#server').value;
853850
url += '/read';
854851
var headers = {mode};
@@ -863,16 +860,16 @@ <h3>
863860
var con = JSON.parse(txt);
864861
console.log(con);
865862
if(con.offer){
866-
await http.read.offer(con.offer);
863+
await read.offer(con.offer);
867864
}
868865
if(con.answer){
869-
await http.read.answer(con.answer);
866+
await read.answer(con.answer);
870867
}
871868
var n = con.ice.length;
872869
for(var i=0;i<n;i++){
873870

874871
var ice = con.ice[i];
875-
await http.read.ice(ice);
872+
await read.ice(ice);
876873

877874
}//for
878875

@@ -884,8 +881,8 @@ <h3>
884881
//:
885882

886883

887-
http.read.offer = async function(offer){
888-
debug('http.read.offer');
884+
read.offer = async function(offer){
885+
debug('read.offer');
889886
var sdp = JSON.parse(offer);
890887
console.log(sdp);
891888
await pc.con.setRemoteDescription(sdp);
@@ -900,8 +897,8 @@ <h3>
900897
}//offer
901898

902899

903-
http.read.answer = async function(answer){
904-
debug('http.read.answer');
900+
read.answer = async function(answer){
901+
debug('read.answer');
905902
var sdp = JSON.parse(answer);
906903
console.log(sdp);
907904
await pc.con.setRemoteDescription(sdp);
@@ -916,11 +913,11 @@ <h3>
916913
}//answer
917914

918915

919-
http.read.ice = async function(ice){
920-
916+
read.ice = async function(ice){
917+
debug('read.ice');
921918
var str = ice;
922919
var candidate = JSON.parse(str);
923-
//console.log(candidate);
920+
console.log(candidate);
924921

925922
ui.view.sys('remote ice candidate');
926923

@@ -942,7 +939,7 @@ <h3>
942939

943940
poll.send = async function(){
944941
console.log('poll.send');
945-
await http.send.read();
942+
await http.read();
946943

947944
poll.timer = setTimeout(poll.send,poll.time);
948945

@@ -954,174 +951,6 @@ <h3>
954951
clearTimeout(poll.timer);
955952

956953
}//stop
957-
958-
959-
//:
960-
961-
962-
963-
/*
964-
965-
966-
ws.create = function(url){
967-
debug('ws.create');
968-
var promise = new Promise(res=>ws.create.resolve=res);
969-
970-
ws.con = new WebSocket(url);
971-
ws.con.onopen = ws.on.open;
972-
ws.con.onclose = ws.on.close;
973-
ws.con.onerror = ws.on.error;
974-
ws.con.onmessage = ws.on.message;
975-
976-
return promise;
977-
978-
}//create.ws
979-
980-
981-
ws.on.open = function(){
982-
debug('ws.on.open');
983-
ui.view.sys('signalling open');
984-
985-
}//on.open
986-
987-
988-
ws.on.close = function(){
989-
debug('ws.on.close');
990-
ui.view.sys('signalling closed');
991-
}//close
992-
993-
994-
ws.on.error = function(){
995-
debug('ws.on.error');
996-
}//error
997-
998-
999-
ws.on.message = function({data}){
1000-
debug('ws.on.message');
1001-
//debug.log(data);
1002-
data = JSON.parse(data);
1003-
1004-
switch(data.type){
1005-
1006-
case 'init' : ws.on.message.init(data); break;
1007-
case 'remote' : ws.on.message.remote(data); break;
1008-
case 'offer' : ws.on.message.offer(data); break;
1009-
case 'answer' : ws.on.message.answer(data); break;
1010-
case 'sdp' : ws.on.message.sdp(data); break;
1011-
case 'ice' : ws.on.message.ice(data); break;
1012-
1013-
default :
1014-
debug('ws.on.message unknown');
1015-
}//switch
1016-
1017-
}//message
1018-
1019-
1020-
ws.on.message.init = function(data){
1021-
debug('ws.on.message.init');
1022-
//debug.log(data);
1023-
mode = data.mode;
1024-
ui.view.sys(`mode : ${mode}`);
1025-
1026-
if(mode=='master'){
1027-
ui.view.sys('waiting for remote ...');
1028-
}
1029-
1030-
ws.create.resolve();
1031-
1032-
1033-
}//init
1034-
1035-
1036-
ws.on.message.remote = function(){
1037-
}//remotee
1038-
1039-
1040-
ws.on.message.offer = async function({offer}){
1041-
debug('ws.on.message.offer');
1042-
ui.view.sys('offer received');
1043-
1044-
await pc.con.setRemoteDescription(offer);
1045-
1046-
pc.create.answer();
1047-
1048-
}//offer
1049-
1050-
1051-
ws.on.message.answer = async function({answer}){
1052-
debug('ws.on.message.answer');
1053-
ui.view.sys('answer received');
1054-
1055-
await pc.con.setRemoteDescription(answer);
1056-
1057-
}//answer
1058-
1059-
1060-
ws.on.message.sdp = function(data){
1061-
}//sdp
1062-
1063-
1064-
ws.on.message.ice = async function({candidate}){
1065-
debug('ws.on.message.ice');
1066-
console.log(candidate);
1067-
ui.view.sys('ice candidate received');
1068-
1069-
var err;
1070-
try{
1071-
1072-
await pc.con.addIceCandidate(candidate);
1073-
1074-
}//try
1075-
catch(err2){
1076-
1077-
err = err2;
1078-
1079-
}//catch
1080-
if(err){
1081-
console.log('ice candidate error');
1082-
console.error(err);
1083-
ui.view.sys.red('ice cadidate received error');
1084-
ui.view.sys.red(err.toString());
1085-
return;
1086-
}
1087-
1088-
}//ice
1089-
1090-
1091-
//:
1092-
1093-
1094-
ws.send.json = function(json){
1095-
1096-
var str = JSON.stringify(json);
1097-
ws.con.send(str);
1098-
1099-
}//json
1100-
1101-
1102-
ws.send.offer = function(offer){
1103-
debug('ws.send.offer');
1104-
//debug.log(offer);
1105-
ws.send.json({type:'offer',offer});
1106-
1107-
}//offer
1108-
1109-
1110-
ws.send.answer = function(answer){
1111-
debug('ws.send.answer');
1112-
//debug.log(answer);
1113-
ws.send.json({type:'answer',answer});
1114-
1115-
}//answer
1116-
1117-
1118-
ws.send.ice = function(candidate){
1119-
1120-
ws.send.json({type:'ice',candidate});
1121-
1122-
}//ice
1123-
1124-
*/
1125954

1126955

1127956
//:
@@ -1170,9 +999,7 @@ <h3>
1170999
var offer = await pc.con.createOffer();
11711000
await pc.con.setLocalDescription(offer);
11721001

1173-
http.send.offer(offer);
1174-
//ws.send.offer(offer);
1175-
1002+
http.offer(offer);
11761003

11771004
}//offer
11781005

@@ -1184,8 +1011,7 @@ <h3>
11841011
var answer = await pc.con.createAnswer();
11851012
await pc.con.setLocalDescription(answer);
11861013

1187-
http.send.answer(answer);
1188-
//ws.send.answer(answer);
1014+
http.answer(answer);
11891015

11901016
}//answer
11911017

@@ -1240,7 +1066,7 @@ <h3>
12401066

12411067
ui.view.sys('local ice candidate');
12421068

1243-
http.send.ice(candidate);
1069+
http.ice(candidate);
12441070
pc.process(candidate);
12451071

12461072
}//onice

0 commit comments

Comments
 (0)