Search in sources :

Example 1 with Bid

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());
}
Also used : Bidding(org.jboss.quickstarts.websocket.model.Bidding) Bid(org.jboss.quickstarts.websocket.model.Bid) OnMessage(javax.websocket.OnMessage)

Example 2 with Bid

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();
}
Also used : StringWriter(java.io.StringWriter) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) Bid(org.jboss.quickstarts.websocket.model.Bid) JsonWriter(javax.json.JsonWriter)

Aggregations

Bid (org.jboss.quickstarts.websocket.model.Bid)2 StringWriter (java.io.StringWriter)1 JsonArrayBuilder (javax.json.JsonArrayBuilder)1 JsonObjectBuilder (javax.json.JsonObjectBuilder)1 JsonWriter (javax.json.JsonWriter)1 OnMessage (javax.websocket.OnMessage)1 Bidding (org.jboss.quickstarts.websocket.model.Bidding)1