简体   繁体   中英

Stomp client with Spring Server

I have a problem:

The application is running on Tomcat 8.5

Client in 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)

My server 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();
    }
}

And Controller:

@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;
        }

}

I created client as the link 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) {

    }
}
  • 10.0.2.2
  • your computer IP
  • 127.0.0.1

I found a bug. I had to change "localhost" to "10.0.2.2"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM