gpt4 book ai didi

JAVA:json + websocket

转载 作者:行者123 更新时间:2023-12-03 06:32:04 25 4
gpt4 key购买 nike

我正在与 friend 一起创建编程项目。我们把它分成两部分,我负责客户端(简单的窗口应用程序),他负责服务器。我应该在 websocket 的帮助下将 JSON 对象发送到他的服务器(他给了我信息,我应该发送什么 http://pastebin.com/dmYBtN25 )。我知道如何创建 json 对象,但对我来说问题是如何将 websocket lib 与 json 结合使用(目前我正在使用 weberknecht 和 json-lib )。下面是我发现的一个示例,可能适合我的客户。我非常乐意提供提示和帮助,或者只是简单的示例如何做到这一点。

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();
}
}

}

最佳答案

你尝试过吗:

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

如果您知道如何创建 JSON,那么应该可以做到。

如何使用 google gson 创建 JSON 的示例:

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());

}

}

关于JAVA:json + websocket,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12957440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com