使用 Spring Server 的 Stomp 客户端

Posted

技术标签:

【中文标题】使用 Spring Server 的 Stomp 客户端【英文标题】:Stomp client with Spring Server 【发布时间】:2017-02-21 12:35:52 【问题描述】:

我有一个问题:

应用程序在 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) 

    

【问题讨论】:

【参考方案1】: 10.0.2.2 你的电脑IP 127.0.0.1

【讨论】:

【参考方案2】:

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

【讨论】:

以上是关于使用 Spring Server 的 Stomp 客户端的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 spring+stomp+sockjs 获得 websocket 响应

Spring 4 websocket + stomp + rabbitmq 和集群

带有 Sockjs 和 Spring 4 但没有 Stomp 的 WebSocket

使用Spring STOMP websockets向单个人发送消息

使用 SimpleBrokerMessageHandler 时的通信关系 - STOMP Spring

Spring 4 Web 套接字 - 我必须有一个 stomp 代理吗?