我不知道为什么什么也没发生。我正在尝试编写一个将json对象发送到码头服务器的java程序。该服务器已经被写入(由其他人,它是一个项目),并且只有某些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.StdErrLogjava.lang.NullPointerException[both classes: clientsocket, client][1]
1 回答
杨魅力
TA贡献1811条经验 获得超6个赞
我认为您没有等待连接完成。
Future<Session> fut = client.connect(clientsocket, uri);
Session session = fut.get(); // wait for connect to complete (or throw exception)
session.getRemote().sendString(js.toJSONString());
添加回答
举报
0/150
提交
取消