JAVA: json + websocket

14,878

have you tried:

websocket.send("{\"firstName\": \"John\"}" /* stick your JSON here */);

If you know how to create JSON that should do it.

Exmaple of how you could create the JSON with google gson:

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * @author jadlr
 */
public class UserConnected {

private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();

private final int event;
private final String cookie;
private final From from;
private final Channel channel;

public UserConnected(int event, String cookie, From from, Channel channel) {
    this.from = from;
    this.cookie = cookie;
    this.event = event;
    this.channel = channel;
}

public int getEvent() {
    return event;
}

public String getCookie() {
    return cookie;
}

public From getFrom() {
    return from;
}

public String toJson() {
    return GSON.toJson(this, UserConnected.class);
}

public static class From {

    private final int id;
    private final String userId;

    public From(int id, String userId) {
        this.id = id;
        this.userId = userId;
    }

    public int getId() {
        return id;
    }

    public String getUserId() {
        return userId;
    }

}

public static class Channel {

    private final int id;
    private final String name;

    public Channel(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

}

public static void main(String[] args) {

    UserConnected userConnected = new UserConnected(0, "cookie123", new From(1, "user"), new Channel(1, "channel"));
    System.out.println(userConnected.toJson());

}

}
Share:
14,878
ashur
Author by

ashur

Updated on August 21, 2022

Comments

  • ashur
    ashur almost 2 years

    I'm creating with friend programming project. We splitted this in two parts, I'm responsible for client( simple window application ), he made server. I'm supposed to send JSON objects to his server with help of websocket ( he gave me info, what I should send http://pastebin.com/dmYBtN25). I know how to create json objects, but problem for me is how to use websocket lib combined with json( currently I'm using weberknecht and json-lib ). Below is an example I found that may be base for my client. I would be greatfull for tips and help or just simple example how to do that.

    import java.net.URI;
    import java.net.URISyntaxException;
    
    import de.roderick.weberknecht.WebSocket;
    import de.roderick.weberknecht.WebSocketConnection;
    import de.roderick.weberknecht.WebSocketEventHandler;
    import de.roderick.weberknecht.WebSocketException;
    import de.roderick.weberknecht.WebSocketMessage;
    
    
    public class App {
        public static void main(String[] args) {
    
        try {
            URI url = new URI("ws://127.0.0.1/test");
            WebSocket websocket = new WebSocketConnection(url);
    
            // Register Event Handlers
    
            websocket.setEventHandler(new WebSocketEventHandler() {
                public void onOpen() {
                    System.out.println("--open");
                }
    
                public void onMessage(WebSocketMessage message) {
                    System.out.println("--received message: "
                            + message.getText());
                }
    
                public void onClose() {
                    System.out.println("--close");
                }
            });
    
            // Establish WebSocket Connection
            websocket.connect();
    
            // Send UTF-8 Text
            websocket.send("hello world");
    
            // Close WebSocket Connection
            websocket.close();
        } catch (WebSocketException wse) {
            wse.printStackTrace();
        } catch (URISyntaxException use) {
            use.printStackTrace();
        }
    }
    

    }

    • oberstet
      oberstet over 11 years
      It may help to look at the code of github.com/tavendo/AutobahnAndroid, which implements WebSocket, and a RPC/PubSub on top with JSON and by using the Jackson JSON processor. Disclaimer: I am author of Autobahn and WAMP.
  • ashur
    ashur over 11 years
    websocket.send("{\"firstName\": \"John\"}" /* stick your JSON here */); This method only takes String as argument, my main Problem is that I don't know how to send this object.
  • Jonas Adler
    Jonas Adler over 11 years
    The String is sent with that method, the toJson() method in my json sample return a String.
  • ashur
    ashur over 11 years
    I get it, thanks, it helped a lot. I'll tinker with the code.