OwnershipModel does not listen for any LostOwnershipBroadcast (‘lost-ownership-broadcast’) messages and as a result does not update OwnershipModel _isOwned when this message is sent. Could you please adjust the logic?
Hi Gilderko,
To adjust the OwnershipModel so that it listens for LostOwnershipBroadcast messages, you will need to enhance the constructor to include a listener for this event. Here’s how you can implement this:
- Create a new method within the
OwnershipModelclass to handle the lost ownership event. - Register this handler when constructing the
OwnershipModel.
Here’s the modified code:
import { OwnershipEvent, GainedOwnershipBroadcastResponse, LostOwnershipBroadcastResponse } from "@needle-tools/engine";
export class OwnershipModel {
// ... existing code ...
constructor(connection: NetworkConnection, guid: string) {
this.connection = connection;
this.guid = guid;
this._gainSubscription = this.onGainedOwnership.bind(this);
this._lostSubscription = this.onLostOwnership.bind(this);
// Adding listener for LostOwnershipBroadcast
connection.beginListen(OwnershipEvent.LostOwnershipBroadcast, this.onLostOwnershipBroadcast.bind(this));
connection.beginListen(OwnershipEvent.GainedOwnershipBroadcast, this._gainSubscription);
this._hasOwnerResponse = this.onHasOwnerResponse.bind(this);
connection.beginListen(OwnershipEvent.ResponseHasOwner, this._hasOwnerResponse);
}
private onLostOwnershipBroadcast(res: LostOwnershipBroadcastResponse) {
if (res.guid === this.guid) {
this._isOwned = false; // Update ownership status
this._hasOwnership = false; // Also reset hasOwnership if needed
console.log("Lost ownership via broadcast", this.guid);
}
}
// ... existing code ...
}
Explanation:
- A new method
onLostOwnershipBroadcastis added to handle thelost-ownership-broadcastevents. - The constructor now registers this method so it will be invoked whenever a
LostOwnershipBroadcastmessage is received.
Feel free to test this implementation to ensure it meets your requirements. If you encounter any issues, please provide additional details.
1 replies left - Get a PRO license for more AI replies.
thread_ypBRgW6FjebRW3oX667iBCs7
Reply to this message to continue the conversation