Created
June 1, 2019 19:37
-
-
Save timetocode/22240446b253d257b845df5695077560 to your computer and use it in GitHub Desktop.
Example of transferring a player between two nengi instances while maintaining an authoritative server. One instance registers some information with the other instance, and provides the client with a token which it uses to be reconnected with that information after connecting to the other instance.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// pseudocode for transfers if game has a practically infinite number of instances. | |
// From one of the game servers | |
transfer(client, toInstance) { | |
toInstance.someWebApi.request( | |
{ transfer: { token: 'abc123', data }}, | |
(res) => { | |
if (res.transferAccepted) { | |
instance.message(new TransferMessage(address, token)), client) | |
client.disconnect() | |
} | |
} | |
) | |
} | |
// on the other game server, very pseudocode | |
someWebApi.onRequest((req, res) => { | |
pendingTransfers.add(req.body.token, req.body.data) | |
res.send({ transferAccepted: true}) | |
}) | |
// from that client... | |
if (msg.protocol.name === 'TransferMessage') { | |
removeEverythingFromThisClientAndGetReadyToReconnect() // uh uh... like a full reset | |
client.connect(msg.address, { token: msg.token }) | |
// alternately can stick this data into localstorage and just reload the window | |
} | |
// in onConnect on any given instance | |
instance.onConnect((client, clientData, callback) => { | |
const player = something() | |
// oh hey this player is connecting with transfer data | |
if (clientData.fromClient && clientData.fromClient.token) { | |
const transferData = pendingTransfers.get(clientData.fromClient.token) | |
// transerData now contains information that was authoritatively sent from the other instance, and can be trusted | |
// do whatever w/ it, e.g. | |
Object.assign(player, transerData) | |
}// else this is just a normal connecting player, not a transfer | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment