如何解决spring sokjs android客户端破管错误

时间:2017-01-04 10:38:34

标签: java android spring stomp spring-websocket

我为实时聊天应用程序创建了一个spring web socket stomp服务器。到目前为止,我已经将sockJS客户端用于我的网络应用程序,但现在在尝试与android集成时我遇到了一些问题。我尝试了多个库。虽然我能够成功使用spring web socket客户端库进行测试。但是在尝试从andoird尝试

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

使用this lib获取以下代码我收到了错误

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


    } 

任何替代工作库或解决方案也将非常受欢迎。

0 个答案:

没有答案