@needle-tools/engine
Version:
Needle Engine is a web-based runtime for 3D apps. It runs on your machine for development with great integrations into editors like Unity or Blender - and can be deployed onto any device! It is flexible, extensible and networking and XR are built-in
81 lines • 3.69 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { isLocalNetwork } from "../engine/engine_networking_utils.js";
import { serializable } from "../engine/engine_serialization.js";
import { getParam } from "../engine/engine_utils.js";
import { Behaviour } from "./Component.js";
const debug = getParam("debugnet");
/**
* The networking component is used to provide a websocket url to the networking system. It implements the {@link INetworkingWebsocketUrlProvider} interface.
* @category Networking
* @group Components
*/
export class Networking extends Behaviour {
/** The url that should be used for the websocket connection */
url = null;
/** The name of the url parameter that should be used to override the url. When set the url will be overridden by the url parameter e.g. when `urlParameterName=ws` `?ws=ws://localhost:8080` */
urlParameterName = null;
/** Thie localhost url that should be used when the networking is running on a local network. This is useful when the server is running on the same machine as the client.
*/
localhost = null;
/** @internal */
awake() {
if (debug)
console.log(this);
this.context.connection.registerProvider(this);
}
/** @internal */
getWebsocketUrl() {
let socketurl = this.url ? Networking.GetUrl(this.url, this.localhost) : null;
if (this.urlParameterName) {
const res = getParam(this.urlParameterName);
if (res && typeof res === "string") {
socketurl = res;
}
}
if (!socketurl)
return null;
// regex https://regex101.com/r/JQ5WqB/1
const regex = new RegExp("(((https?)|(?<socket_prefix>wss?)):\/\/)?(www\.)?(?<url>.+)", "gm");
const match = regex.exec(socketurl);
if (!match?.groups)
return null;
// if the url has a ws or wss prefix already assume the whole url is in the correct format
const socketPrefix = match?.groups["socket_prefix"];
if (socketPrefix)
return socketurl;
// otherwise add the ws prefix
return "wss://" + match?.groups["url"];
}
static GetUrl(url, localhostFallback) {
let result = url;
const useLocalHostUrl = Networking.IsLocalNetwork() && localhostFallback;
if (useLocalHostUrl) {
result = localhostFallback;
}
if (url?.startsWith("/")) {
const base = useLocalHostUrl ? result : window.location.origin;
if (base?.endsWith("/") && url.startsWith("/"))
url = url.substring(1);
result = base + url;
}
return result;
}
static IsLocalNetwork(hostname = window.location.hostname) {
return isLocalNetwork(hostname);
}
}
__decorate([
serializable()
], Networking.prototype, "url", void 0);
__decorate([
serializable()
], Networking.prototype, "urlParameterName", void 0);
__decorate([
serializable()
], Networking.prototype, "localhost", void 0);
//# sourceMappingURL=Networking.js.map