我遇到了问题:
应用程序在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) {
}
}
答案 0 :(得分:2)
答案 1 :(得分:0)
我发现了一个错误。我不得不改变&#34; localhost&#34;到&#34; 10.0.2.2&#34;