用Spring Server踩踏客户端

问题描述 投票:0回答:2

我有个问题:

该应用程序在Tomcat 8.5上运行

Android中的客户端

E/WebSocketsConnectionProvider: onError
                                java.net.ConnectException: Connection refused
                                    at sun.nio.ch.Net.connect0(Native Method)
                                    at sun.nio.ch.Net.connect(Net.java:477)
                                    at sun.nio.ch.Net.connect(Net.java:467)
                                    at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:669)
                                    at org.java_websocket.client.WebSocketClient.interruptableRun(WebSocketClient.java:210)
                                    at org.java_websocket.client.WebSocketClient.run(WebSocketClient.java:188)
                                    at java.lang.Thread.run(Thread.java:761)

我的服务器Spring:

public class AppConfigurer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        // TODO Auto-generated method stub
        return new Class[]{AppConfiguration.class};
    }

    @Override
    protected String[] getServletMappings() {
        // TODO Auto-generated method stub
        return new String[]{"/"};
    }

}

.

@Configuration
@ComponentScan("com.app.controllers")
@EnableWebMvc
public class AppConfiguration extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/hello").withSockJS();
    }
}

和控制器:

@RestController
public class AppController {

    public AppController(){
        super();
    }

        @MessageMapping("/hello")
        @SendTo("/topic/greetings")
        public String greeting(String message) throws Exception {
            Thread.sleep(1000); // simulated delay
            return "Hello" + message;
        }

}

我创建了客户端作为链接LINK

public class AddPikActivity extends AppCompatActivity implements OnItemSelectedListener {

    private StompClient mStompClient;
    public static final String TAG = "StompClient";
    Button btn_add_pik;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_pik);

        btn_add_pik = (Button) findViewById(R.id.btn_add_pik);
        btn_add_pik.setOnClickListener(e -> new LongOperation().execute(""));
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}

.

public class LongOperation extends AsyncTask<String, Void, String> {

    private StompClient mStompClient;
    String TAG = "LongOperation";

    @Override
    protected String doInBackground(String... params) {

        mStompClient = Stomp.over(WebSocket.class, "ws://localhost:8080/beta/app/hello/websocket");
        mStompClient.connect();

        mStompClient.topic("/topic/greetings").subscribe(topicMessage -> {
            Log.d(TAG, topicMessage.getPayload());
        });

        mStompClient.send("/app/hello", "My first STOMP message!").subscribe();
        mStompClient.lifecycle().subscribe(lifecycleEvent -> {
            switch (lifecycleEvent.getType()) {

                case OPENED:
                    Log.d(TAG, "Stomp connection opened");
                    break;

                case ERROR:
                    Log.e(TAG, "Error", lifecycleEvent.getException());
                    break;

                case CLOSED:
                    Log.d(TAG, "Stomp connection closed");
                    break;
            }
        });
        return "Executed";
    }

    @Override
    protected void onPostExecute(String result) {

    }
}
android spring websocket stomp spring-websocket
2个回答
2
投票
  • 10.0.2.2
  • 你的电脑IP
  • 127.0.0.1

0
投票

我发现了一个bug。我不得不将“localhost”改为“10.0.2.2”

© www.soinside.com 2019 - 2024. All rights reserved.