TooTallNate / Java-WebSocket

A barebones WebSocket client and server implementation written in 100% Java.
http://tootallnate.github.io/Java-WebSocket
MIT License
10.48k stars 2.58k forks source link

read websocket api json format in Java Desktop App #1183

Closed ezizrehimov closed 3 years ago

ezizrehimov commented 3 years ago

How i can read websocket api json format in Java Desktop App? i added your jar file and tryed this, but this only doing closed connection. My webscoket api url : ws://168.119.56.105/radioapi/ws

   WebSocketClient mWs = new WebSocketClient(new URI("ws://168.119.56.105/radioapi/ws"), new Draft_10()) {
            @Override
            public void onMessage(String message) {
                try {
                    JSONObject
                            obj = new JSONObject(message);
                    String channel = obj.getString("channel");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onOpen(ServerHandshake handshake) {
                System.out.println("opened connection");
            }

            @Override
            public void onClose(int code, String reason, boolean remote) {
                System.out.println("closed connection");
            }

            @Override
            public void onError(Exception ex) {
                ex.printStackTrace();
            }

        };
        //open websocket
        mWs.connect();
//            JSONObject obj = new JSONObject();
//            obj.put("event", "addChannel");
//            obj.put("channel", "ok_btccny_ticker");
//            String message = obj.toString();
        //send message
//            mWs.send(message);
        mWs.close();
marci4 commented 3 years ago

Please use the latest version of this lib. Draft_10 got removed a long time ago.

Best regards, Marcel

ezizrehimov commented 3 years ago

okay i now used latest version and delete here Draft_10. But now i getting error

Error: Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory

  WebSocketClient mWs = new WebSocketClient(new URI("ws://168.119.56.105/radioapi/ws")) {
            @Override
            public void onMessage(String message) {
                try {
                    JSONObject
                            obj = new JSONObject(message);
                    String channel = obj.getString("channel");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onOpen(ServerHandshake handshake) {
                System.out.println("opened connection");
            }

            @Override
            public void onClose(int code, String reason, boolean remote) {
                System.out.println("closed connection");
            }

            @Override
            public void onError(Exception ex) {
                ex.printStackTrace();
            }

        };
        mWs.connect();
//            JSONObject obj = new JSONObject();
//            obj.put("event", "addChannel");
//            obj.put("channel", "ok_btccny_ticker");
//            String message = obj.toString();
        //send message
//            mWs.send(message);
        mWs.close();

    }
ezizrehimov commented 3 years ago

I now added slf4j.jar file , now i getting this error:

SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#noProviders for further details.
closed connection
ezizrehimov commented 3 years ago

how i can System.out.println() println this webscoket api response?

ezizrehimov commented 3 years ago

Oh okay. i now find problem. how i can add authentication (login,password) ?

ezizrehimov commented 3 years ago
 client.addHeader("Authorization","Basic " + basicAuth);

I know do this. But i still get this error :


SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#noProviders for further details.
PhilipRoman commented 3 years ago
SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#noProviders for further details.

This just a warning, not an error. You won't be able to turn on logging, but everything else will still work.

ezizrehimov commented 3 years ago

thank you.