awrtc_browser
Version:
Compatible browser implementation to the Unity asset WebRTC Video Chat. Try examples in build folder
253 lines (248 loc) • 10.4 kB
JavaScript
/*
Copyright (c) 2019, because-why-not.com Limited
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** Abstract interfaces and serialization to keep different
* versions compatible to each other.
*
* Watch out before changing anything in this file. Content is reused
* between webclient, signaling server and needs to remain compatible to
* the C# implementation.
*/
import { SLog } from "./Helper";
export var NetEventType;
(function (NetEventType) {
NetEventType[NetEventType["Invalid"] = 0] = "Invalid";
NetEventType[NetEventType["UnreliableMessageReceived"] = 1] = "UnreliableMessageReceived";
NetEventType[NetEventType["ReliableMessageReceived"] = 2] = "ReliableMessageReceived";
NetEventType[NetEventType["ServerInitialized"] = 3] = "ServerInitialized";
NetEventType[NetEventType["ServerInitFailed"] = 4] = "ServerInitFailed";
NetEventType[NetEventType["ServerClosed"] = 5] = "ServerClosed";
NetEventType[NetEventType["NewConnection"] = 6] = "NewConnection";
NetEventType[NetEventType["ConnectionFailed"] = 7] = "ConnectionFailed";
NetEventType[NetEventType["Disconnected"] = 8] = "Disconnected";
NetEventType[NetEventType["FatalError"] = 100] = "FatalError";
NetEventType[NetEventType["Warning"] = 101] = "Warning";
NetEventType[NetEventType["Log"] = 102] = "Log";
/// <summary>
/// This value and higher are reserved for other uses.
/// Should never get to the user and should be filtered out.
/// </summary>
NetEventType[NetEventType["ReservedStart"] = 200] = "ReservedStart";
/// <summary>
/// Reserved.
/// Used by protocols that forward NetworkEvents
/// </summary>
NetEventType[NetEventType["MetaVersion"] = 201] = "MetaVersion";
/// <summary>
/// Reserved.
/// Used by protocols that forward NetworkEvents.
/// </summary>
NetEventType[NetEventType["MetaHeartbeat"] = 202] = "MetaHeartbeat";
})(NetEventType || (NetEventType = {}));
export var NetEventDataType;
(function (NetEventDataType) {
NetEventDataType[NetEventDataType["Null"] = 0] = "Null";
NetEventDataType[NetEventDataType["ByteArray"] = 1] = "ByteArray";
NetEventDataType[NetEventDataType["UTF16String"] = 2] = "UTF16String";
})(NetEventDataType || (NetEventDataType = {}));
var NetworkEvent = /** @class */ (function () {
function NetworkEvent(t, conId, data) {
this.type = t;
this.connectionId = conId;
this.data = data;
}
Object.defineProperty(NetworkEvent.prototype, "RawData", {
get: function () {
return this.data;
},
enumerable: true,
configurable: true
});
Object.defineProperty(NetworkEvent.prototype, "MessageData", {
get: function () {
if (typeof this.data != "string")
return this.data;
return null;
},
enumerable: true,
configurable: true
});
Object.defineProperty(NetworkEvent.prototype, "Info", {
get: function () {
if (typeof this.data == "string")
return this.data;
return null;
},
enumerable: true,
configurable: true
});
Object.defineProperty(NetworkEvent.prototype, "Type", {
get: function () {
return this.type;
},
enumerable: true,
configurable: true
});
Object.defineProperty(NetworkEvent.prototype, "ConnectionId", {
get: function () {
return this.connectionId;
},
enumerable: true,
configurable: true
});
//for debugging only
NetworkEvent.prototype.toString = function () {
var output = "NetworkEvent[";
output += "NetEventType: (";
output += NetEventType[this.type];
output += "), id: (";
output += this.connectionId.id;
output += "), Data: (";
if (typeof this.data == "string") {
output += this.data;
}
output += ")]";
return output;
};
NetworkEvent.parseFromString = function (str) {
var values = JSON.parse(str);
var data;
if (values.data == null) {
data = null;
}
else if (typeof values.data == "string") {
data = values.data;
}
else if (typeof values.data == "object") {
//json represents the array as an object containing each index and the
//value as string number ... improve that later
var arrayAsObject = values.data;
var length = 0;
for (var prop in arrayAsObject) {
//if (arrayAsObject.hasOwnProperty(prop)) { //shouldnt be needed
length++;
//}
}
var buffer = new Uint8Array(Object.keys(arrayAsObject).length);
for (var i = 0; i < buffer.length; i++)
buffer[i] = arrayAsObject[i];
data = buffer;
}
else {
SLog.LogError("network event can't be parsed: " + str);
}
var evt = new NetworkEvent(values.type, values.connectionId, data);
return evt;
};
NetworkEvent.toString = function (evt) {
return JSON.stringify(evt);
};
NetworkEvent.fromByteArray = function (arrin) {
//old node js versions seem to not return proper Uint8Arrays but
//buffers -> make sure it is a Uint8Array
var arr = new Uint8Array(arrin);
var type = arr[0]; //byte
var dataType = arr[1]; //byte
var id = new Int16Array(arr.buffer, arr.byteOffset + 2, 1)[0]; //short
var data = null;
if (dataType == NetEventDataType.ByteArray) {
var length_1 = new Uint32Array(arr.buffer, arr.byteOffset + 4, 1)[0]; //uint
var byteArray = new Uint8Array(arr.buffer, arr.byteOffset + 8, length_1);
data = byteArray;
}
else if (dataType == NetEventDataType.UTF16String) {
var length_2 = new Uint32Array(arr.buffer, arr.byteOffset + 4, 1)[0]; //uint
var uint16Arr = new Uint16Array(arr.buffer, arr.byteOffset + 8, length_2);
var str = "";
for (var i = 0; i < uint16Arr.length; i++) {
str += String.fromCharCode(uint16Arr[i]);
}
data = str;
}
else if (dataType == NetEventDataType.Null) {
//message has no data
}
else {
throw new Error('Message has an invalid data type flag: ' + dataType);
}
var conId = new ConnectionId(id);
var result = new NetworkEvent(type, conId, data);
return result;
};
NetworkEvent.toByteArray = function (evt) {
var dataType;
var length = 4; //4 bytes are always needed
//getting type and length
if (evt.data == null) {
dataType = NetEventDataType.Null;
}
else if (typeof evt.data == "string") {
dataType = NetEventDataType.UTF16String;
var str = evt.data;
length += str.length * 2 + 4;
}
else {
dataType = NetEventDataType.ByteArray;
var byteArray = evt.data;
length += 4 + byteArray.length;
}
//creating the byte array
var result = new Uint8Array(length);
result[0] = evt.type;
;
result[1] = dataType;
var conIdField = new Int16Array(result.buffer, result.byteOffset + 2, 1);
conIdField[0] = evt.connectionId.id;
if (dataType == NetEventDataType.ByteArray) {
var byteArray = evt.data;
var lengthField = new Uint32Array(result.buffer, result.byteOffset + 4, 1);
lengthField[0] = byteArray.length;
for (var i = 0; i < byteArray.length; i++) {
result[8 + i] = byteArray[i];
}
}
else if (dataType == NetEventDataType.UTF16String) {
var str = evt.data;
var lengthField = new Uint32Array(result.buffer, result.byteOffset + 4, 1);
lengthField[0] = str.length;
var dataField = new Uint16Array(result.buffer, result.byteOffset + 8, str.length);
for (var i = 0; i < dataField.length; i++) {
dataField[i] = str.charCodeAt(i);
}
}
return result;
};
return NetworkEvent;
}());
export { NetworkEvent };
var ConnectionId = /** @class */ (function () {
function ConnectionId(nid) {
this.id = nid;
}
ConnectionId.INVALID = new ConnectionId(-1);
return ConnectionId;
}());
export { ConnectionId };
//export {NetEventType, NetworkEvent, ConnectionId, INetwork, IBasicNetwork};
//# sourceMappingURL=INetwork.js.map