如何将android设备连接到Spring websocket服务器
Posted
技术标签:
【中文标题】如何将android设备连接到Spring websocket服务器【英文标题】:How connect android device to Spring websocket server 【发布时间】:2016-12-16 14:39:13 【问题描述】:我正在尝试将 android 设备连接到 Spring websocke 服务器。 但是所有的库都给我错误。我在计算机上启动我的 Spring websocket 服务器,客户端在同一个网络中,所以我试图通过计算机的 ip 连接。
这是服务器属性
@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】:据此answer
简单示例(带有retrolambda):
在项目中添加以下类路径:
classpath 'me.tatarka:gradle-retrolambda:3.2.0'
在您的应用 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" />
别忘了在你的 URL 中添加“websocket”。
【讨论】:
你刚刚给了我图书馆页面的例子,我看到了,谢谢。以上是关于如何将android设备连接到Spring websocket服务器的主要内容,如果未能解决你的问题,请参考以下文章
如何将 Android 手机连接到 Visual Studio Code?
如何将 OS 环境变量连接到 Spring Web 应用程序项目中 application.properties 中的占位符?