use of org.jboss.quickstarts.websocket.model.Bid in project quickstart by wildfly.
the class BidWebSocketEndpoint method onMessage.
// This method receives a Message that contains a command
// The Message object is "decoded" by the MessageDecoder class
@OnMessage
public void onMessage(Session session, Message message) throws IOException, EncodeException {
if (message.getCommand().equals("newBid")) {
Bidding bidding = BiddingFactory.getBidding();
bidding.addBid(new Bid(session.getId(), message.getBidValue()));
}
if (message.getCommand().equals("buyItNow")) {
Bidding bidding = BiddingFactory.getBidding();
bidding.buyItNow();
}
if (message.getCommand().equals("resetBid")) {
BiddingFactory.resetBidding();
}
notifyAllSessions(BiddingFactory.getBidding());
}
use of org.jboss.quickstarts.websocket.model.Bid in project quickstart by wildfly.
the class BiddingEncoder method encode.
@Override
public String encode(Bidding bidding) throws EncodeException {
// It uses the JSON-P API to create a JSON representation
JsonObjectBuilder jsonBuilder = Json.createObjectBuilder().add("item", Json.createObjectBuilder().add("buyNowPrice", bidding.getItem().getBuyNowPrice()).add("description", bidding.getItem().getDescription()).add("imagePath", bidding.getItem().getImagePath()).add("title", bidding.getItem().getTitle()).build()).add("bidStatus", bidding.getBidStatus().toString()).add("currentPrice", bidding.getCurrentPrice()).add("secondsLeft", 0);
if (bidding.getDueDate() != null) {
jsonBuilder.add("dueDate", bidding.getDueDate().getTime());
}
if (bidding.getSecondsLeft() != null) {
jsonBuilder.add("secondsLeft", bidding.getSecondsLeft());
}
JsonArrayBuilder jsonBidArray = Json.createArrayBuilder();
for (Bid bid : bidding.getBids()) {
jsonBidArray.add(Json.createObjectBuilder().add("dateTime", bid.getDateTime().getTime()).add("value", bid.getValue()).add("id", bid.getId()).build());
}
jsonBuilder.add("bids", jsonBidArray);
StringWriter stWriter = new StringWriter();
JsonWriter jsonWriter = Json.createWriter(stWriter);
jsonWriter.writeObject(jsonBuilder.build());
jsonWriter.close();
return stWriter.toString();
}
Aggregations