在 java 中发送带有码头的 json 对象
Sending a json object with jetty in java
我不明白为什么没有任何反应。
我正在尝试编写一个 java 程序,将 json 对象发送到码头服务器。
服务器已经编写(由其他人编写,它是一个项目)并且仅排除某些 json 个对象。但是他没有从我的程序中得到任何东西。
public class client {
final static String HOST = "localhost";
final static int PORT = 3000;
public static void main(String[] args)
{
String URIString = "ws://" + HOST + ":" + PORT + "/servlets";
URI uri = URI.create(URIString);
WebSocketClient client = new WebSocketClient();
JSONObject js = new JSONObject();
js.put("toke","hallo");
Clientsocket clientsocket = new Clientsocket();
try {
client.start();
Future<Session> fut = client.connect(clientsocket, uri);
clientsocket.getSession().getRemote().sendString(js.toJSONString());;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
public class Clientsocket extends WebSocketAdapter {
//private static final Logger LOG = Log.getLogger(Clientsocket.class);
@Override
public void onWebSocketClose(int statusCode,String reason)
{
super.onWebSocketClose(statusCode, reason);
//LOG.info("Websocket Close:{} - {} ", statusCode,reason);
}
@Override
public void onWebSocketConnect(Session session)
{
super.onWebSocketConnect(session);
//LOG.info("Websocket Connect: {}", session);
}
}
启动时的错误信息:
2018-06-22 14:26:43.789:INFO::main: Logging initialized @257ms to org.eclipse.jetty.util.log.StdErrLog
java.lang.NullPointerException
[both classes: clientsocket, client][1]
我想你没有等待连接完成。
Future<Session> fut = client.connect(clientsocket, uri);
Session session = fut.get(); // wait for connect to complete (or throw exception)
session.getRemote().sendString(js.toJSONString());
我不明白为什么没有任何反应。
我正在尝试编写一个 java 程序,将 json 对象发送到码头服务器。 服务器已经编写(由其他人编写,它是一个项目)并且仅排除某些 json 个对象。但是他没有从我的程序中得到任何东西。
public class client {
final static String HOST = "localhost";
final static int PORT = 3000;
public static void main(String[] args)
{
String URIString = "ws://" + HOST + ":" + PORT + "/servlets";
URI uri = URI.create(URIString);
WebSocketClient client = new WebSocketClient();
JSONObject js = new JSONObject();
js.put("toke","hallo");
Clientsocket clientsocket = new Clientsocket();
try {
client.start();
Future<Session> fut = client.connect(clientsocket, uri);
clientsocket.getSession().getRemote().sendString(js.toJSONString());;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
public class Clientsocket extends WebSocketAdapter {
//private static final Logger LOG = Log.getLogger(Clientsocket.class);
@Override
public void onWebSocketClose(int statusCode,String reason)
{
super.onWebSocketClose(statusCode, reason);
//LOG.info("Websocket Close:{} - {} ", statusCode,reason);
}
@Override
public void onWebSocketConnect(Session session)
{
super.onWebSocketConnect(session);
//LOG.info("Websocket Connect: {}", session);
}
}
启动时的错误信息:
2018-06-22 14:26:43.789:INFO::main: Logging initialized @257ms to org.eclipse.jetty.util.log.StdErrLog
java.lang.NullPointerException
[both classes: clientsocket, client][1]
我想你没有等待连接完成。
Future<Session> fut = client.connect(clientsocket, uri);
Session session = fut.get(); // wait for connect to complete (or throw exception)
session.getRemote().sendString(js.toJSONString());