Implemented basic Pong lobby system
This commit is contained in:
88
PongGame/wwwroot/js/pong.js
Normal file
88
PongGame/wwwroot/js/pong.js
Normal file
@@ -0,0 +1,88 @@
|
||||
"use strict";
|
||||
|
||||
console.log("Pong script was run!");
|
||||
|
||||
const connection = new signalR.HubConnectionBuilder()
|
||||
.withUrl("/pong/hub")
|
||||
.withAutomaticReconnect()
|
||||
.withHubProtocol(new signalR.protocols.msgpack.MessagePackHubProtocol())
|
||||
.build();
|
||||
|
||||
function getElement(id) {
|
||||
return document.getElementById(id) ?? console.error(`Element #${id} not found!`)
|
||||
}
|
||||
|
||||
const connectionStatus = getElement("connection");
|
||||
const createlobby = getElement("createlobby");
|
||||
const roomid = getElement("roomid");
|
||||
const joinroom = getElement("joinroom");
|
||||
const usernameinput = getElement("username");
|
||||
const setusername = getElement("setusername");
|
||||
const leavelobby = getElement("leavelobby");
|
||||
|
||||
connection.onclose(function (error) {
|
||||
if (error) {
|
||||
connectionStatus.textContent = "Unexpected error!";
|
||||
return console.error(`Connection aborted: ${error.message}`);
|
||||
}
|
||||
console.info("Disconnected!");
|
||||
connectionStatus.textContent = "Closed!";
|
||||
});
|
||||
|
||||
connection.onreconnecting(function (error) {
|
||||
if (error) {
|
||||
connectionStatus.textContent = "Reconnecting!";
|
||||
return console.error(`Connection reconnecting: ${error.message}`);
|
||||
}
|
||||
console.info("Reconnecting!");
|
||||
connectionStatus.textContent = "Reconnecting!";
|
||||
});
|
||||
|
||||
connection.onreconnected(function (connectionId) {
|
||||
console.info(`Connected as ${connectionId}!`);
|
||||
connectionStatus.textContent = "Connected!";
|
||||
});
|
||||
|
||||
createlobby.addEventListener("click", function (event) {
|
||||
connection.invoke("CreateRoom").catch(function (err) {
|
||||
return console.error(err.toString());
|
||||
});
|
||||
event.preventDefault();
|
||||
});
|
||||
|
||||
connection.on("GameStateChanged", function (state) {
|
||||
console.info(`Game is now in state ${state}`);
|
||||
});
|
||||
connection.on("UsernameChanged", function (username) {
|
||||
console.info(`Username is now ${username}`);
|
||||
usernameinput.value = username;
|
||||
});
|
||||
|
||||
joinroom.addEventListener("click", function (event) {
|
||||
connection.invoke("JoinRoom", roomid.value).catch(function (err) {
|
||||
return console.error(err.toString());
|
||||
});
|
||||
event.preventDefault();
|
||||
});
|
||||
|
||||
setusername.addEventListener("click", function (event) {
|
||||
connection.invoke("RequestUsernameChange", usernameinput.value).catch(function (err) {
|
||||
return console.error(err.toString());
|
||||
});
|
||||
event.preventDefault();
|
||||
});
|
||||
|
||||
leavelobby.addEventListener("click", function (event) {
|
||||
connection.invoke("LeaveRoom").catch(function (err) {
|
||||
return console.error(err.toString());
|
||||
});
|
||||
event.preventDefault();
|
||||
});
|
||||
|
||||
connection.start().then(function () {
|
||||
console.info(`Connected!`);
|
||||
connectionStatus.textContent = "Connected!";
|
||||
}).catch(function (err) {
|
||||
connectionStatus.textContent = "Connection failed!";
|
||||
return console.error(err.toString());
|
||||
});
|
2077
PongGame/wwwroot/js/signalr/signalr-protocol-msgpack.js
Normal file
2077
PongGame/wwwroot/js/signalr/signalr-protocol-msgpack.js
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
2
PongGame/wwwroot/js/signalr/signalr-protocol-msgpack.min.js
vendored
Normal file
2
PongGame/wwwroot/js/signalr/signalr-protocol-msgpack.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
3115
PongGame/wwwroot/js/signalr/signalr.js
Normal file
3115
PongGame/wwwroot/js/signalr/signalr.js
Normal file
File diff suppressed because it is too large
Load Diff
1
PongGame/wwwroot/js/signalr/signalr.js.map
Normal file
1
PongGame/wwwroot/js/signalr/signalr.js.map
Normal file
File diff suppressed because one or more lines are too long
2
PongGame/wwwroot/js/signalr/signalr.min.js
vendored
Normal file
2
PongGame/wwwroot/js/signalr/signalr.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
1
PongGame/wwwroot/js/signalr/signalr.min.js.map
Normal file
1
PongGame/wwwroot/js/signalr/signalr.min.js.map
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user