如何将android设备连接到Spring websocket服务器

时间:2016-12-16 14:39:13

标签: android spring websocket

我试图将Android设备连接到Spring websocke服务器。 但是所有的库都给了我错误。我在计算机和客户端上启动我的Spring websocket服务器是在同一个网络,所以我试图通过计算机的IP连接。

这是sevrer属性

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig 
   extends AbstractSessionWebSocketMessageBrokerConfigurer<ExpiringSession> {

@Override
protected void configureStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/anveo-appserver").withSockJS();
}

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
    registry.enableSimpleBroker("/topic/", "/anveo/");
    //registry.enableStompBrokerRelay("/queue/", "/topic/", "/exchange/");
    registry.setApplicationDestinationPrefixes("/app");
   }
}

这是使用我的stomp客户端的示例。

public class StompWebSocketHandler {

private LoggerHelper logger = LoggerHelper.get(this.getClass());

public void connect() {
    StompClient mStompClient;


    mStompClient = Stomp.over(WebSocket.class, "ws://172.26.252.197:8443/app/anveo-appserver/websocket");

    mStompClient.lifecycle().subscribe(new Action1<LifecycleEvent>() {
        @Override
        public void call(LifecycleEvent lifecycleEvent) {
            switch (lifecycleEvent.getType()) {

                case OPENED:
                    logger.log("Stomp connection opened");
                    break;

                case ERROR:
                    logger.log("Stomp connection error");
                    logger.log(lifecycleEvent.getException().toString());
                    break;

                case CLOSED:
                    logger.log("Stomp connection closed");
                    break;
            }
        }
    });
    mStompClient.connect();
}

}

谁能告诉我我做错了什么?

1 个答案:

答案 0 :(得分:-1)

根据此answer

简单示例(使用retrolambda):

在项目中添加以下类路径:

classpath 'me.tatarka:gradle-retrolambda:3.2.0'

在app build.gradle中添加以下内容:

apply plugin: 'me.tatarka.retrolambda'

android {
       .............
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}


  dependencies {
      ............................
    compile 'org.java-websocket:Java-WebSocket:1.3.0'
    compile 'com.github.NaikSoftware:StompProtocolAndroid:1.1.5'
}

例如:

public class MainActivity extends AppCompatActivity { 
    private StompClient mStompClient;
    public static  final String TAG="StompClient";
    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button view = (Button) findViewById(R.id.button);
        view.setOnClickListener(e->  new LongOperation().execute(""));
    } 


    private 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/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) {

        } 

    } 
} 

在manifest.xml中添加Internet权限

<uses-permission android:name="android.permission.INTERNET" />

并且不要忘记添加&#34; websocket&#34;在您的网址中。