如何在Pharo中托管Web应用,使每个客户端获得独立实例?
Hey there! The problem you're seeing makes total sense—right now, you're using a single instance of RBDisplay for every incoming request. That's why all visitors are seeing the same persisted data. Let's fix this so each person gets a fresh, clean instance when they hit localhost/display.
Quick Fix: Fresh Instance Per Request
Instead of passing a pre-created RBDisplay object to your route mapping, use a block that creates a new instance every time the route is accessed. Here's the updated code:
ZnServer startDefaultOn: 80. ZnServer default delegate map: #display to: [ RBDisplay new ]
Why this works:
When you pass a block to map:to:, ZnServer evaluates that block for each incoming request. This means every visitor (and every request they make) gets their own brand new RBDisplay instance, so their uploaded data stays isolated from others. No more shared state, and no need to restart the server to clear data!
Bonus: Session-Specific Instances (Optional)
If you want the same visitor to keep their data across multiple requests (like navigating between pages in your app), you can tie the instance to their session instead. Here's how:
ZnServer startDefaultOn: 80. ZnServer default delegate map: #display to: [ :request | | handler | # Fetch or create a RBDisplay instance tied to the user's session handler := request session attributeAt: #UserSpecificRBDisplay ifAbsentPut: [ RBDisplay new ]. handler handleRequest: request ]
This way, a returning visitor will get back their existing instance with their data, but new visitors still get a fresh one. It's a middle ground between "fresh instance every request" and "single shared instance".
Just make sure your RBDisplay class properly implements the handleRequest: method to work with the incoming request object—this approach passes the request directly to your handler, so you'll need to adjust if your current implementation expects a different setup.
内容的提问来源于stack exchange,提问作者rbsc




