friend-connect
Version:
**BEFORE YOU USE THIS TOOL, PLEASE READ THE FOLLOWING: WE _AS CONTRIBUTORS_ ARE NOT RESPONSIBLE FOR ANY DAMAGE OR LOSS CAUSED BY THIS APP. USE AN ALT ACCOUNT, JUST IN CASE THERE IS AN ISSUE WITH THIS METHOD.**
185 lines (184 loc) • 8.1 kB
JavaScript
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var _RTAMultiplayerSession_sessionName;
import EventEmitter from "events";
import crypto from "crypto";
import wsPkg from "websocket";
const { w3cwebsocket: W3CWebSocket } = wsPkg;
export class RTAMultiplayerSession extends EventEmitter {
constructor(xbox, multiplayerSessionRequest, serviceConfigId, sessionTemplateName, autoRestart, updateSessionCallback) {
super();
this.uri = "wss://rta.xboxlive.com";
_RTAMultiplayerSession_sessionName.set(this, void 0);
this.websocketConnected = false;
this.startTimes = 0;
this.members = new Set();
this.functionsToRunOnSessionUpdate = new Set();
this.firstStartSignaled = false;
this.updateSessionCallback = updateSessionCallback;
this.multiplayerSessionRequest = multiplayerSessionRequest;
this.serviceConfigId = serviceConfigId;
this.sessionTemplateName = sessionTemplateName;
this.xbox = xbox;
if (autoRestart)
this.autoRestart = true;
setInterval(() => {
if (this.websocketConnected && !this.xbox.token.isRefreshing)
for (let i of this.functionsToRunOnSessionUpdate) {
i();
}
}, 27600);
this.functionsToRunOnSessionUpdate.add(() => {
this.updateSession();
});
this.start();
}
get sessionName() {
return __classPrivateFieldGet(this, _RTAMultiplayerSession_sessionName, "f");
}
start() {
if (this.websocketConnected)
return void 0;
if (this.startTimes >= 5) {
this.xbox.token.reset();
//fs.unlinkSync(`${this.xbox.authPath}/${this.xbox.emailHash}_live-cache.json`);
this.startTimes = 0;
setTimeout(() => {
this.xbox.token.refresh().then(() => {
this.emit("timeUntilStart");
this.start();
return void 0;
});
}, 29000);
}
__classPrivateFieldSet(this, _RTAMultiplayerSession_sessionName, crypto.randomUUID(), "f");
this.startTimes++;
const ws = new W3CWebSocket(`${this.uri}/connect`, "echo-protocol", undefined, {
Authorization: this.xbox.authorizationHeader,
});
ws.onerror = (error) => {
this.websocketConnected = false;
this.emit("error", error);
};
ws.onopen = () => {
ws.send('[1,1,"https://sessiondirectory.xboxlive.com/connections/"]');
this.websocketConnected = true;
if (this.autoRestart)
this.start();
this.emit("open");
};
ws.onclose = (event) => {
this.websocketConnected = false;
if (this.autoRestart)
this.start();
this.emit("close", event);
};
ws.onmessage = (event) => {
switch (typeof event.data) {
case "string":
if (event.data.includes("ConnectionId")) {
this.connectionId = JSON.parse(event.data)[4].ConnectionId;
//debug("connectionId: " + this.connectionId);
this.updateSession();
}
default:
this.emit("message", event.data);
}
};
}
updateSession() {
//debug("updateSession called");
if (this.updateSessionCallback)
this.updateSessionCallback(this);
//@ts-ignore
this.multiplayerSessionRequest.members = {
me: {
constants: {
system: {
initialize: true,
xuid: this.xbox.xuid,
},
},
properties: {
system: {
active: true,
connection: this.connectionId,
subscription: {
changeTypes: ["everything"],
id: "9042513B-D0CF-48F6-AF40-AD83B3C9EED4",
},
},
},
},
};
this.xbox.sessionDirectory
.session(this.multiplayerSessionRequest, this.serviceConfigId, this.sessionTemplateName, __classPrivateFieldGet(this, _RTAMultiplayerSession_sessionName, "f"))
.then(async (res) => {
let data = await res.json();
//debug("updateSessionCallback called");
this.emit("sessionResponse", data);
if (this.websocketConnected) {
this.xbox.sessionDirectory
.setActivity({
scid: this.serviceConfigId,
templateName: this.sessionTemplateName,
name: __classPrivateFieldGet(this, _RTAMultiplayerSession_sessionName, "f"),
})
.then(async (res) => {
this.emit("join", await res.text());
});
}
});
}
join(xbox) {
this.functionsToRunOnSessionUpdate.add(() => {
if (this.websocketConnected)
xbox.sessionDirectory.sessionKeepAlivePacket(this.serviceConfigId, this.sessionTemplateName, __classPrivateFieldGet(this, _RTAMultiplayerSession_sessionName, "f"));
});
xbox.sessionDirectory
.session({
//@ts-ignore
members: {
me: {
constants: {
system: {
initialize: true,
xuid: xbox.xuid,
},
},
properties: {
system: {
active: true,
connection: this.connectionId,
subscription: {
changeTypes: ["everything"],
id: "9042513B-D0CF-48F6-AF40-AD83B3C9EED4",
},
},
},
},
},
}, this.serviceConfigId, this.sessionTemplateName, __classPrivateFieldGet(this, _RTAMultiplayerSession_sessionName, "f"))
.finally(() => {
xbox.sessionDirectory
.setActivity({
scid: this.serviceConfigId,
templateName: this.sessionTemplateName,
name: __classPrivateFieldGet(this, _RTAMultiplayerSession_sessionName, "f"),
})
.then(async (res) => {
this.emit("join", await res.text());
});
});
}
}
_RTAMultiplayerSession_sessionName = new WeakMap();