eaglercraft-1.5/javascript/eagswebrtc.js

741 lines
22 KiB
JavaScript
Raw Normal View History

2022-07-16 22:02:55 -07:00
"use strict";
/*
2022-08-06 15:33:14 -07:00
This is the backend for voice channels and LAN servers in eaglercraft
2022-07-16 22:02:55 -07:00
2022-08-06 15:33:14 -07:00
it links with TeaVM EaglerAdapter at runtime
2022-07-16 22:02:55 -07:00
2022-08-06 15:33:14 -07:00
Copyright 2022 ayunami2000 & lax1dude. All rights reserved.
2022-07-16 22:02:55 -07:00
*/
2022-08-06 15:33:14 -07:00
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%% VOICE CODE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2022-07-16 22:02:55 -07:00
window.initializeVoiceClient = (() => {
const READYSTATE_NONE = 0;
const READYSTATE_ABORTED = -1;
const READYSTATE_DEVICE_INITIALIZED = 1;
2022-07-18 23:00:13 -07:00
const PEERSTATE_FAILED = 0;
const PEERSTATE_SUCCESS = 1;
const PEERSTATE_LOADING = 2;
2022-07-16 22:02:55 -07:00
class EaglercraftVoicePeer {
2022-07-17 17:57:05 -07:00
constructor(client, peerId, peerConnection, offer) {
2022-07-16 22:02:55 -07:00
this.client = client;
this.peerId = peerId;
this.peerConnection = peerConnection;
2022-07-17 17:57:05 -07:00
this.stream = null;
2022-07-16 22:02:55 -07:00
const self = this;
this.peerConnection.addEventListener("icecandidate", (evt) => {
if(evt.candidate) {
2022-07-17 17:57:05 -07:00
self.client.iceCandidateHandler(self.peerId, JSON.stringify({ sdpMLineIndex: evt.candidate.sdpMLineIndex, candidate: evt.candidate.candidate }));
2022-07-16 22:02:55 -07:00
}
});
this.peerConnection.addEventListener("track", (evt) => {
2022-07-17 17:57:05 -07:00
self.rawStream = evt.streams[0];
const aud = new Audio();
aud.autoplay = true;
aud.muted = true;
aud.onended = function() {
aud.remove();
};
aud.srcObject = self.rawStream;
self.client.peerTrackHandler(self.peerId, self.rawStream);
2022-07-16 22:02:55 -07:00
});
2022-07-17 17:57:05 -07:00
this.peerConnection.addStream(this.client.localMediaStream.stream);
if (offer) {
this.peerConnection.createOffer((desc) => {
const selfDesc = desc;
self.peerConnection.setLocalDescription(selfDesc, () => {
self.client.descriptionHandler(self.peerId, JSON.stringify(selfDesc));
2022-07-18 23:00:13 -07:00
if (self.client.peerStateInitial != PEERSTATE_SUCCESS) self.client.peerStateInitial = PEERSTATE_SUCCESS;
2022-07-17 17:57:05 -07:00
}, (err) => {
console.error("Failed to set local description for \"" + self.peerId + "\"! " + err);
2022-07-18 23:00:13 -07:00
if (self.client.peerStateInitial == PEERSTATE_LOADING) self.client.peerStateInitial = PEERSTATE_FAILED;
2022-07-17 17:57:05 -07:00
self.client.signalDisconnect(self.peerId);
});
2022-07-16 22:02:55 -07:00
}, (err) => {
2022-07-17 17:57:05 -07:00
console.error("Failed to set create offer for \"" + self.peerId + "\"! " + err);
2022-07-18 23:00:13 -07:00
if (self.client.peerStateInitial == PEERSTATE_LOADING) self.client.peerStateInitial = PEERSTATE_FAILED;
2022-07-16 22:02:55 -07:00
self.client.signalDisconnect(self.peerId);
});
2022-07-17 17:57:05 -07:00
}
2022-07-16 22:02:55 -07:00
this.peerConnection.addEventListener("connectionstatechange", (evt) => {
if(evt.connectionState === 'disconnected') {
self.client.signalDisconnect(self.peerId);
2022-07-18 23:00:13 -07:00
} else if (evt.connectionState === 'connected') {
if (self.client.peerState != PEERSTATE_SUCCESS) self.client.peerState = PEERSTATE_SUCCESS;
} else if (evt.connectionState === 'failed') {
if (self.client.peerState == PEERSTATE_LOADING) self.client.peerState = PEERSTATE_FAILED;
self.client.signalDisconnect(self.peerId);
2022-07-16 22:02:55 -07:00
}
});
}
disconnect() {
this.peerConnection.close();
}
2022-07-17 17:57:05 -07:00
mute(muted) {
this.rawStream.getAudioTracks()[0].enabled = !muted;
}
2022-07-16 22:02:55 -07:00
setRemoteDescription(descJSON) {
const self = this;
2022-07-17 17:57:05 -07:00
try {
const remoteDesc = JSON.parse(descJSON);
this.peerConnection.setRemoteDescription(remoteDesc, () => {
if(remoteDesc.type == 'offer') {
self.peerConnection.createAnswer((desc) => {
const selfDesc = desc;
self.peerConnection.setLocalDescription(selfDesc, () => {
self.client.descriptionHandler(self.peerId, JSON.stringify(selfDesc));
2022-07-18 23:00:13 -07:00
if (self.client.peerStateDesc != PEERSTATE_SUCCESS) self.client.peerStateDesc = PEERSTATE_SUCCESS;
2022-07-17 17:57:05 -07:00
}, (err) => {
console.error("Failed to set local description for \"" + self.peerId + "\"! " + err);
2022-07-18 23:00:13 -07:00
if (self.client.peerStateDesc == PEERSTATE_LOADING) self.client.peerStateDesc = PEERSTATE_FAILED;
2022-07-17 17:57:05 -07:00
self.client.signalDisconnect(self.peerId);
});
2022-07-16 22:02:55 -07:00
}, (err) => {
2022-07-17 17:57:05 -07:00
console.error("Failed to create answer for \"" + self.peerId + "\"! " + err);
2022-07-18 23:00:13 -07:00
if (self.client.peerStateDesc == PEERSTATE_LOADING) self.client.peerStateDesc = PEERSTATE_FAILED;
2022-07-17 17:57:05 -07:00
self.client.signalDisconnect(self.peerId);
2022-07-16 22:02:55 -07:00
});
2022-07-17 17:57:05 -07:00
}
}, (err) => {
console.error("Failed to set remote description for \"" + self.peerId + "\"! " + err);
2022-07-18 23:00:13 -07:00
if (self.client.peerStateDesc == PEERSTATE_LOADING) self.client.peerStateDesc = PEERSTATE_FAILED;
2022-07-17 17:57:05 -07:00
self.client.signalDisconnect(self.peerId);
});
} catch (err) {
console.error("Failed to parse remote description for \"" + self.peerId + "\"! " + err);
2022-07-18 23:00:13 -07:00
if (self.client.peerStateDesc == PEERSTATE_LOADING) self.client.peerStateDesc = PEERSTATE_FAILED;
2022-07-17 17:57:05 -07:00
self.client.signalDisconnect(self.peerId);
}
2022-07-16 22:02:55 -07:00
}
addICECandidate(candidate) {
2022-07-17 17:57:05 -07:00
try {
this.peerConnection.addIceCandidate(new RTCIceCandidate(JSON.parse(candidate)));
2022-07-18 23:00:13 -07:00
if (this.client.peerStateIce != PEERSTATE_SUCCESS) this.client.peerStateIce = PEERSTATE_SUCCESS;
2022-07-17 17:57:05 -07:00
} catch (err) {
2022-07-18 23:00:13 -07:00
console.error("Failed to parse ice candidate for \"" + this.peerId + "\"! " + err);
if (this.client.peerStateIce == PEERSTATE_LOADING) this.client.peerStateIce = PEERSTATE_FAILED;
this.client.signalDisconnect(this.peerId);
2022-07-17 17:57:05 -07:00
}
2022-07-16 22:02:55 -07:00
}
}
class EaglercraftVoiceClient {
constructor() {
this.ICEServers = [];
this.hasInit = false;
this.peerList = new Map();
this.readyState = READYSTATE_NONE;
2022-07-18 23:00:13 -07:00
this.peerState = PEERSTATE_LOADING;
this.peerStateConnect = PEERSTATE_LOADING;
this.peerStateInitial = PEERSTATE_LOADING;
this.peerStateDesc = PEERSTATE_LOADING;
this.peerStateIce = PEERSTATE_LOADING;
2022-07-16 22:02:55 -07:00
this.iceCandidateHandler = null;
this.descriptionHandler = null;
this.peerTrackHandler = null;
this.peerDisconnectHandler = null;
2022-07-18 08:07:59 -07:00
this.microphoneVolumeAudioContext = null;
2022-07-16 22:02:55 -07:00
}
voiceClientSupported() {
return typeof window.RTCPeerConnection !== "undefined" && typeof navigator.mediaDevices !== "undefined" &&
typeof navigator.mediaDevices.getUserMedia !== "undefined";
}
setICEServers(urls) {
this.ICEServers.length = 0;
2022-07-17 23:49:05 -07:00
for(var i = 0; i < urls.length; ++i) {
var etr = urls[i].split(";");
if(etr.length == 1) {
this.ICEServers.push({ urls: etr[0] });
}else if(etr.length == 3) {
this.ICEServers.push({ urls: etr[0], username: etr[1], credential: etr[2] });
2022-07-17 17:57:05 -07:00
}
2022-07-16 22:02:55 -07:00
}
}
setICECandidateHandler(cb) {
this.iceCandidateHandler = cb;
}
setDescriptionHandler(cb) {
this.descriptionHandler = cb;
}
setPeerTrackHandler(cb) {
this.peerTrackHandler = cb;
}
setPeerDisconnectHandler(cb) {
this.peerDisconnectHandler = cb;
}
activateVoice(tk) {
2022-07-17 17:57:05 -07:00
if(this.hasInit) this.localRawMediaStream.getAudioTracks()[0].enabled = tk;
2022-07-16 22:02:55 -07:00
}
2022-07-17 17:57:05 -07:00
initializeDevices() {
2022-07-16 22:02:55 -07:00
if(!this.hasInit) {
const self = this;
navigator.mediaDevices.getUserMedia({ audio: true, video: false }).then((stream) => {
2022-07-18 08:07:59 -07:00
self.microphoneVolumeAudioContext = new AudioContext();
2022-07-16 22:02:55 -07:00
self.localRawMediaStream = stream;
self.localRawMediaStream.getAudioTracks()[0].enabled = false;
self.localMediaStream = self.microphoneVolumeAudioContext.createMediaStreamDestination();
self.localMediaStreamGain = self.microphoneVolumeAudioContext.createGain();
var localStreamIn = self.microphoneVolumeAudioContext.createMediaStreamSource(stream);
localStreamIn.connect(self.localMediaStreamGain);
self.localMediaStreamGain.connect(self.localMediaStream);
2022-07-17 17:57:05 -07:00
self.localMediaStreamGain.gain.value = 1.0;
2022-07-16 22:02:55 -07:00
self.readyState = READYSTATE_DEVICE_INITIALIZED;
this.hasInit = true;
2022-07-17 17:57:05 -07:00
}).catch((err) => {
2022-07-16 22:02:55 -07:00
self.readyState = READYSTATE_ABORTED;
});
}else {
2022-07-18 23:00:13 -07:00
this.readyState = READYSTATE_DEVICE_INITIALIZED;
2022-07-16 22:02:55 -07:00
}
}
setMicVolume(val) {
2022-07-17 17:57:05 -07:00
if(this.hasInit) {
if(val > 0.5) val = 0.5 + (val - 0.5) * 2.0;
if(val > 1.5) val = 1.5;
if(val < 0.0) val = 0.0;
this.localMediaStreamGain.gain.value = val * 2.0;
}
2022-07-16 22:02:55 -07:00
}
2022-07-18 23:00:13 -07:00
resetPeerStates() {
this.peerState = this.peerStateConnect = this.peerStateInitial = this.peerStateDesc = this.peerStateIce = PEERSTATE_LOADING;
}
getPeerState() {
return this.peerState;
}
getPeerStateConnect() {
return this.peerStateConnect;
}
getPeerStateInitial() {
return this.peerStateInitial;
}
2022-07-16 22:02:55 -07:00
2022-07-18 23:00:13 -07:00
getPeerStateDesc() {
return this.peerStateDesc;
}
getPeerStateIce() {
return this.peerStateIce;
2022-07-16 22:02:55 -07:00
}
getReadyState() {
return this.readyState;
}
2022-07-17 17:57:05 -07:00
signalConnect(peerId, offer) {
if (!this.hasInit) initializeDevices();
2022-07-18 23:00:13 -07:00
try {
const peerConnection = new RTCPeerConnection({ iceServers: this.ICEServers, optional: [ { DtlsSrtpKeyAgreement: true } ] });
const peerInstance = new EaglercraftVoicePeer(this, peerId, peerConnection, offer);
this.peerList.set(peerId, peerInstance);
if (this.peerStateConnect != PEERSTATE_SUCCESS) this.peerStateConnect = PEERSTATE_SUCCESS;
} catch (e) {
if (this.peerStateConnect == PEERSTATE_LOADING) this.peerStateConnect = PEERSTATE_FAILED;
}
2022-07-16 22:02:55 -07:00
}
signalDescription(peerId, descJSON) {
var thePeer = this.peerList.get(peerId);
if((typeof thePeer !== "undefined") && thePeer !== null) {
thePeer.setRemoteDescription(descJSON);
}
}
2022-07-17 17:57:05 -07:00
signalDisconnect(peerId, quiet) {
2022-07-16 22:02:55 -07:00
var thePeer = this.peerList.get(peerId);
if((typeof thePeer !== "undefined") && thePeer !== null) {
this.peerList.delete(thePeer);
try {
thePeer.disconnect();
}catch(e) {}
2022-07-17 17:57:05 -07:00
this.peerDisconnectHandler(peerId, quiet);
}
}
mutePeer(peerId, muted) {
var thePeer = this.peerList.get(peerId);
if((typeof thePeer !== "undefined") && thePeer !== null) {
thePeer.mute(muted);
2022-07-16 22:02:55 -07:00
}
}
signalICECandidate(peerId, candidate) {
var thePeer = this.peerList.get(peerId);
if((typeof thePeer !== "undefined") && thePeer !== null) {
thePeer.addICECandidate(candidate);
}
}
}
window.constructVoiceClient = () => new EaglercraftVoiceClient();
});
window.startVoiceClient = () => {
if(typeof window.constructVoiceClient !== "function") {
window.initializeVoiceClient();
}
return window.constructVoiceClient();
2022-08-06 15:33:14 -07:00
};
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%% LAN CLIENT CODE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
window.initializeLANClient = (() => {
2022-08-10 08:52:43 -07:00
2022-08-06 15:33:14 -07:00
const READYSTATE_INIT_FAILED = -2;
const READYSTATE_FAILED = -1;
const READYSTATE_DISCONNECTED = 0;
const READYSTATE_CONNECTING = 1;
const READYSTATE_CONNECTED = 2;
class EaglercraftLANClient {
2022-08-10 08:52:43 -07:00
2022-08-06 15:33:14 -07:00
constructor() {
2022-08-10 08:52:43 -07:00
this.ICEServers = [];
this.peerConnection = null;
this.dataChannel = null;
this.readyState = READYSTATE_CONNECTING;
this.iceCandidateHandler = null;
this.descriptionHandler = null;
this.remoteDataChannelHandler = null;
this.remoteDisconnectHandler = null;
this.remotePacketHandler = null;
2022-08-06 15:33:14 -07:00
}
LANClientSupported() {
2022-08-10 08:52:43 -07:00
return typeof window.RTCPeerConnection !== "undefined";
2022-08-06 15:33:14 -07:00
}
initializeClient() {
2022-08-10 08:52:43 -07:00
try {
if(this.dataChannel != null) {
this.dataChannel.close();
this.dataChannel = null;
}
if(this.peerConnection != null) {
this.peerConnection.close();
}
this.peerConnection = new RTCPeerConnection({ iceServers: this.ICEServers, optional: [ { DtlsSrtpKeyAgreement: true } ] });
this.readyState = READYSTATE_CONNECTING;
} catch (e) {
this.readyState = READYSTATE_INIT_FAILED;
}
2022-08-06 15:33:14 -07:00
}
setICEServers(urls) {
2022-08-10 08:52:43 -07:00
this.ICEServers.length = 0;
for(var i = 0; i < urls.length; ++i) {
var etr = urls[i].split(";");
if(etr.length == 1) {
this.ICEServers.push({ urls: etr[0] });
}else if(etr.length == 3) {
this.ICEServers.push({ urls: etr[0], username: etr[1], credential: etr[2] });
}
}
2022-08-06 15:33:14 -07:00
}
setICECandidateHandler(cb) {
2022-08-10 08:52:43 -07:00
this.iceCandidateHandler = cb;
2022-08-06 15:33:14 -07:00
}
setDescriptionHandler(cb) {
2022-08-10 08:52:43 -07:00
this.descriptionHandler = cb;
2022-08-06 15:33:14 -07:00
}
setRemoteDataChannelHandler(cb) {
2022-08-10 08:52:43 -07:00
this.remoteDataChannelHandler = cb;
2022-08-06 15:33:14 -07:00
}
setRemoteDisconnectHandler(cb) {
2022-08-10 08:52:43 -07:00
this.remoteDisconnectHandler = cb;
2022-08-06 15:33:14 -07:00
}
setRemotePacketHandler(cb) {
2022-08-10 08:52:43 -07:00
this.remotePacketHandler = cb;
2022-08-06 15:33:14 -07:00
}
getReadyState() {
2022-08-10 08:52:43 -07:00
return this.readyState;
2022-08-06 15:33:14 -07:00
}
sendPacketToServer(buffer) {
2022-08-10 08:52:43 -07:00
this.dataChannel.send(buffer);
2022-08-06 15:33:14 -07:00
}
signalRemoteConnect() {
2022-08-10 08:52:43 -07:00
const self = this;
2022-08-10 08:52:43 -07:00
this.peerConnection.addEventListener("icecandidate", (evt) => {
if(evt.candidate) {
self.iceCandidateHandler(JSON.stringify({ sdpMLineIndex: evt.candidate.sdpMLineIndex, candidate: evt.candidate.candidate }));
}
});
this.channel = this.peerConnection.createDataChannel("lan");
this.channel.addEventListener("open", (evt) => {
self.remoteDataChannelHandler(self.channel);
});
this.channel.addEventListener("message", (evt) => {
2022-08-19 17:40:48 -07:00
console.log(evt.data);
self.remotePacketHandler(evt.data);
2022-08-19 17:40:48 -07:00
}, false);
2022-08-10 08:52:43 -07:00
this.peerConnection.createOffer((desc) => {
const selfDesc = desc;
self.peerConnection.setLocalDescription(selfDesc, () => {
self.descriptionHandler(JSON.stringify(selfDesc));
}, (err) => {
console.error("Failed to set local description! " + err);
self.readyState = READYSTATE_FAILED;
self.signalRemoteDisconnect();
});
}, (err) => {
console.error("Failed to set create offer! " + err);
self.readyState = READYSTATE_FAILED;
self.signalRemoteDisconnect();
});
this.peerConnection.addEventListener("connectionstatechange", (evt) => {
if(evt.connectionState === 'disconnected') {
self.signalRemoteDisconnect();
} else if (evt.connectionState === 'connected') {
self.readyState = READYSTATE_CONNECTED;
} else if (evt.connectionState === 'failed') {
self.readyState = READYSTATE_FAILED;
self.signalRemoteDisconnect();
}
});
2022-08-06 15:33:14 -07:00
}
signalRemoteDescription(descJSON) {
try {
this.peerConnection.setRemoteDescription(JSON.parse(descJSON));
} catch (e) {
console.error(e);
this.readyState = READYSTATE_FAILED;
this.signalRemoteDisconnect();
}
2022-08-06 15:33:14 -07:00
}
signalRemoteICECandidate(candidate) {
try {
this.peerConnection.addIceCandidate(JSON.parse(candidate));
} catch (e) {
console.error(e);
this.readyState = READYSTATE_FAILED;
this.signalRemoteDisconnect();
}
2022-08-10 08:52:43 -07:00
}
signalRemoteDisconnect() {
if(this.dataChannel != null) {
this.dataChannel.close();
this.dataChannel = null;
}
if(this.peerConnection != null) {
this.peerConnection.close();
}
2022-08-10 08:52:43 -07:00
this.remoteDisconnectHandler();
this.readyState = READYSTATE_DISCONNECTED;
2022-08-06 15:33:14 -07:00
}
};
window.constructLANClient = () => new EaglercraftLANClient();
});
window.startLANClient = () => {
if(typeof window.constructLANClient !== "function") {
window.initializeLANClient();
}
return window.constructLANClient();
};
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%% LAN SERVER CODE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
window.initializeLANServer = (() => {
2022-08-10 08:52:43 -07:00
const PEERSTATE_FAILED = 0;
const PEERSTATE_SUCCESS = 1;
const PEERSTATE_LOADING = 2;
class EaglercraftLANPeer {
constructor(client, peerId, peerConnection) {
this.client = client;
this.peerId = peerId;
this.peerConnection = peerConnection;
this.dataChannel = null;
const self = this;
this.peerConnection.addEventListener("icecandidate", (evt) => {
if(evt.candidate) {
self.client.iceCandidateHandler(self.peerId, JSON.stringify({ sdpMLineIndex: evt.candidate.sdpMLineIndex, candidate: evt.candidate.candidate }));
}
});
this.peerConnection.addEventListener("datachannel", (evt) => {
self.dataChannel = evt.channel;
self.client.remoteClientDataChannelHandler(self.peerId, self.dataChannel);
self.dataChannel.addEventListener("message", (evt) => {
2022-08-19 17:40:48 -07:00
console.log(evt.data);
self.client.remoteClientPacketHandler(self.peerId, evt.data);
2022-08-19 17:40:48 -07:00
}, false);
});
2022-08-10 08:52:43 -07:00
this.peerConnection.addEventListener("connectionstatechange", (evt) => {
if(evt.connectionState === 'disconnected') {
self.client.signalRemoteDisconnect(self.peerId);
} else if (evt.connectionState === 'connected') {
if (self.client.peerState != PEERSTATE_SUCCESS) self.client.peerState = PEERSTATE_SUCCESS;
} else if (evt.connectionState === 'failed') {
if (self.client.peerState == PEERSTATE_LOADING) self.client.peerState = PEERSTATE_FAILED;
self.client.signalRemoteDisconnect(self.peerId);
}
});
}
disconnect() {
if(this.dataChannel != null) {
this.dataChannel.close();
this.dataChannel = null;
}
this.peerConnection.close();
}
setRemoteDescription(descJSON) {
const self = this;
try {
const remoteDesc = JSON.parse(descJSON);
this.peerConnection.setRemoteDescription(remoteDesc, () => {
if(remoteDesc.type == 'offer') {
self.peerConnection.createAnswer((desc) => {
const selfDesc = desc;
self.peerConnection.setLocalDescription(selfDesc, () => {
self.client.descriptionHandler(self.peerId, JSON.stringify(selfDesc));
if (self.client.peerStateDesc != PEERSTATE_SUCCESS) self.client.peerStateDesc = PEERSTATE_SUCCESS;
}, (err) => {
console.error("Failed to set local description for \"" + self.peerId + "\"! " + err);
if (self.client.peerStateDesc == PEERSTATE_LOADING) self.client.peerStateDesc = PEERSTATE_FAILED;
self.client.signalRemoteDisconnect(self.peerId);
});
}, (err) => {
console.error("Failed to create answer for \"" + self.peerId + "\"! " + err);
if (self.client.peerStateDesc == PEERSTATE_LOADING) self.client.peerStateDesc = PEERSTATE_FAILED;
self.client.signalRemoteDisconnect(self.peerId);
});
}
}, (err) => {
console.error("Failed to set remote description for \"" + self.peerId + "\"! " + err);
if (self.client.peerStateDesc == PEERSTATE_LOADING) self.client.peerStateDesc = PEERSTATE_FAILED;
self.client.signalRemoteDisconnect(self.peerId);
});
} catch (err) {
console.error("Failed to parse remote description for \"" + self.peerId + "\"! " + err);
if (self.client.peerStateDesc == PEERSTATE_LOADING) self.client.peerStateDesc = PEERSTATE_FAILED;
self.client.signalRemoteDisconnect(self.peerId);
}
}
addICECandidate(candidate) {
try {
this.peerConnection.addIceCandidate(new RTCIceCandidate(JSON.parse(candidate)));
if (this.client.peerStateIce != PEERSTATE_SUCCESS) this.client.peerStateIce = PEERSTATE_SUCCESS;
} catch (err) {
console.error("Failed to parse ice candidate for \"" + this.peerId + "\"! " + err);
if (this.client.peerStateIce == PEERSTATE_LOADING) this.client.peerStateIce = PEERSTATE_FAILED;
this.client.signalRemoteDisconnect(this.peerId);
}
}
}
2022-08-06 15:33:14 -07:00
class EaglercraftLANServer {
constructor() {
2022-08-10 08:52:43 -07:00
this.ICEServers = [];
this.hasInit = false;
this.peerList = new Map();
this.peerState = PEERSTATE_LOADING;
this.peerStateConnect = PEERSTATE_LOADING;
this.peerStateInitial = PEERSTATE_LOADING;
this.peerStateDesc = PEERSTATE_LOADING;
this.peerStateIce = PEERSTATE_LOADING;
this.iceCandidateHandler = null;
this.descriptionHandler = null;
this.remoteClientDataChannelHandler = null;
this.remoteClientDisconnectHandler = null;
this.remoteClientPacketHandler = null;
2022-08-06 15:33:14 -07:00
}
LANServerSupported() {
2022-08-10 08:52:43 -07:00
return typeof window.RTCPeerConnection !== "undefined";
2022-08-06 15:33:14 -07:00
}
initializeServer() {
2022-08-10 08:52:43 -07:00
// nothing to do!
2022-08-06 15:33:14 -07:00
}
setICEServers(urls) {
2022-08-10 08:52:43 -07:00
this.ICEServers.length = 0;
for(var i = 0; i < urls.length; ++i) {
var etr = urls[i].split(";");
if(etr.length == 1) {
this.ICEServers.push({ urls: etr[0] });
}else if(etr.length == 3) {
this.ICEServers.push({ urls: etr[0], username: etr[1], credential: etr[2] });
}
}
2022-08-06 15:33:14 -07:00
}
setICECandidateHandler(cb) {
2022-08-10 08:52:43 -07:00
this.iceCandidateHandler = cb;
2022-08-06 15:33:14 -07:00
}
setDescriptionHandler(cb) {
2022-08-10 08:52:43 -07:00
this.descriptionHandler = cb;
2022-08-06 15:33:14 -07:00
}
setRemoteClientDataChannelHandler(cb) {
2022-08-10 08:52:43 -07:00
this.remoteClientDataChannelHandler = cb;
2022-08-06 15:33:14 -07:00
}
setRemoteClientDisconnectHandler(cb) {
2022-08-10 08:52:43 -07:00
this.remoteClientDisconnectHandler = cb;
2022-08-06 15:33:14 -07:00
}
setRemoteClientPacketHandler(cb) {
2022-08-10 08:52:43 -07:00
this.remoteClientPacketHandler = cb;
2022-08-06 15:33:14 -07:00
}
sendPacketToRemoteClient(peerId, buffer) {
2022-08-10 08:52:43 -07:00
var thePeer = this.peerList.get(peerId);
if((typeof thePeer !== "undefined") && thePeer !== null) {
2022-08-19 17:40:48 -07:00
console.log(123);
2022-08-10 08:52:43 -07:00
thePeer.dataChannel.send(buffer);
}
2022-08-06 15:33:14 -07:00
}
2022-08-10 08:52:43 -07:00
resetPeerStates() {
this.peerState = this.peerStateConnect = this.peerStateInitial = this.peerStateDesc = this.peerStateIce = PEERSTATE_LOADING;
}
getPeerState() {
return this.peerState;
}
getPeerStateConnect() {
return this.peerStateConnect;
}
getPeerStateInitial() {
return this.peerStateInitial;
}
getPeerStateDesc() {
return this.peerStateDesc;
}
getPeerStateIce() {
return this.peerStateIce;
}
2022-08-06 15:33:14 -07:00
signalRemoteConnect(peerId) {
2022-08-10 08:52:43 -07:00
try {
const peerConnection = new RTCPeerConnection({ iceServers: this.ICEServers, optional: [ { DtlsSrtpKeyAgreement: true } ] });
const peerInstance = new EaglercraftLANPeer(this, peerId, peerConnection);
this.peerList.set(peerId, peerInstance);
if (this.peerStateConnect != PEERSTATE_SUCCESS) this.peerStateConnect = PEERSTATE_SUCCESS;
} catch (e) {
if (this.peerStateConnect == PEERSTATE_LOADING) this.peerStateConnect = PEERSTATE_FAILED;
}
2022-08-06 15:33:14 -07:00
}
2022-08-10 08:52:43 -07:00
2022-08-06 15:33:14 -07:00
signalRemoteDescription(peerId, descJSON) {
2022-08-10 08:52:43 -07:00
var thePeer = this.peerList.get(peerId);
if((typeof thePeer !== "undefined") && thePeer !== null) {
thePeer.setRemoteDescription(descJSON);
}
2022-08-06 15:33:14 -07:00
}
2022-08-10 08:52:43 -07:00
2022-08-06 15:33:14 -07:00
signalRemoteICECandidate(peerId, candidate) {
2022-08-10 08:52:43 -07:00
var thePeer = this.peerList.get(peerId);
if((typeof thePeer !== "undefined") && thePeer !== null) {
thePeer.addICECandidate(candidate);
}
2022-08-06 15:33:14 -07:00
}
2022-08-10 08:52:43 -07:00
signalRemoteDisconnect(peerId) {
if(peerId.length == 0) {
for(const thePeer of this.peerList.values()) {
if((typeof thePeer !== "undefined") && thePeer !== null) {
this.peerList.delete(thePeer);
try {
thePeer.disconnect();
}catch(e) {}
this.remoteClientDisconnectHandler(peerId);
2022-08-10 08:52:43 -07:00
}
}
this.peerList.clear();
return;
}
var thePeer = this.peerList.get(peerId);
if((typeof thePeer !== "undefined") && thePeer !== null) {
this.peerList.delete(thePeer);
try {
thePeer.disconnect();
}catch(e) {}
this.remoteClientDisconnectHandler(peerId);
2022-08-10 08:52:43 -07:00
}
2022-08-06 15:33:14 -07:00
}
};
window.constructLANServer = () => new EaglercraftLANServer();
});
window.startLANServer = () => {
if(typeof window.constructLANServer !== "function") {
window.initializeLANServer();
}
return window.constructLANServer();
2022-08-06 15:33:14 -07:00
};