how to solve spring sokjs android client broken pipe error

239 Views Asked by At

I have created a spring web socket stomp server for a real-time chat application. So far I have used sockJS client for my web app but now while trying to integrate with Android I am facing some issues. I have tried multiple libraries. Though I was able to use Spring web socket client library successfully for test. But while trying from Android I am getting

com.example.flash.socket.pk.aamir.stompj.StompJRuntimeException: sendto failed: EPIPE (Broken pipe)

using this lib for below code I got the error

private void connectWebSocket() {


    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                JSONObject json = new JSONObject();
              //   Connection con = new Connection("10.0.1.99", 8080,"admin","admin123" );
                Connection con = new Connection("192.168.0.102", 8080);

              // Connection con= new Connection("ufriil.com", 8765);


                // connect
                HashMap<String, String> connectOptionalHeaders = new HashMap<>();
                connectOptionalHeaders.put("host", "vhost-test");


                HashMap<String, String> params = new HashMap<String, String>();
                String creds = String.format("%s:%s","admin","admin123");
                String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
                params.put("Authorization", auth);


                try {
                      con.connect(connectOptionalHeaders);
                      

                    if (con.isConnected())
                    {
                        json.put("writer", "admin");
                        json.put("receiver", "testuser1");
                        json.put("messageBody", "he");
                        Log.e("json", json.toString());
                        con.subscribe("/user/testuser1/reply");

                        // send
                        ChatMessage mess = new ChatMessage();
                        mess.setMessageBody("test bod");
                        mess.setReceiver("testuser1");
                        mess.setWriter("testuser2");
                        Gson gson = new Gson();
                        String str = gson.toJson(mess);
                        Log.e("str", str);
                        con.send("test", "/app/newMessage");

                    }

                } catch (StompJException e) {
                    e.printStackTrace();
                    Log.e("error", e.toString());
                }


                // message listener
                con.setMessageHandler(new MessageHandler() {
                    public void onMessage(Message msg) {
                        Log.e("","message---------");
                        String[] propNameS = msg.getPropertyNames();    // 消息头字段
                        for (String propName : propNameS) {
                            Log.e("",propName + ":" + msg.getProperty(propName));
                        }
                        Log.e("",msg.getContentAsString());   // 消息内容
                    }
                });

// error listener
                con.setErrorHandler(new ErrorHandler() {
                    @Override
                    public void onError(ErrorMessage errormessage) {
                        System.out.println(errormessage.getMessage());
                    }
                });


            } catch (Exception e) {
                Log.e("error5", e.toString());
                e.printStackTrace();
            }
        }
    });

    thread.start();


} 

Any alternative working library or solution would also be much appreciated.

0

There are 0 best solutions below