我想在Android中创建长轮询连接。我使用图书馆 - 高速公路。 Void startpolling我在OnCreate Activity中调用:
private void startPolling() {
final String wsuri = "ws://xxx.xxx.xxx.xxx:8084/free";
try {
mConnection.connect(wsuri, new WebSocketHandler() {
@Override
public void onOpen() {
Log.d(TAG, "Status: Connected to " + wsuri);
mConnection.sendTextMessage("Hello, world!");
}
@Override
public void onTextMessage(String payload) {
Log.d(TAG, "Got echo: " + payload);
}
@Override
public void onClose(int code, String reason) {
Log.d(TAG, "Connection lost.");
}
});
} catch (WebSocketException e) {
Log.d(TAG, e.toString());
}
}
WebServer Apache Tomcat 8.0.3.0
ServerEndpoint("/free")
public class WebSocketPolling {
private static final long serialVersionUID = 1L;
@Resource
WebServiceContext webServiceContext;
@OnOpen
public void onOpen(Session session){
System.out.println("onOpen");
session.addMessageHandler(new MessageHandler.Whole<String>() {
@Override
public void onMessage(String message) {
}
});
}
}
我在客户端代码中遇到了下一个异常:
10-03 12:36:52.370: D/de.tavendo.autobahn.WebSocketReader(5539): run() : WebSocketException (de.tavendo.autobahn.WebSocketException: RSV != 0 and no extension negotiated)
10-03 12:36:52.753: D/de.tavendo.autobahn.WebSocketConnection(5539): opening handshake received
10-03 12:36:52.760: D/TMS:QrRecognitionActivity(5539): Status: Connected to ws://xxx.xxx.xxx.xxx:8084/free
10-03 12:36:52.760: D/de.tavendo.autobahn.WebSocketConnection(5539): fail connection [code = 4, reason = WebSockets protocol violation
10-03 12:36:52.760: D/de.tavendo.autobahn.WebSocketReader(5539): quit
10-03 12:36:52.760: D/de.tavendo.autobahn.WebSocketWriter(5539): ended
10-03 12:36:52.760: D/MainActivity(5539): Connection lost.
10-03 12:36:52.760: D/de.tavendo.autobahn.WebSocketConnection(5539): worker threads stopped
UPD。我尝试在web.xml中添加信息:
<servlet-name>wsChat</servlet-name>
<servlet-class>
com.pop.server.WebSocketPolling
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>wsChat</servlet-name>
<url-pattern>/free</url-pattern>
</servlet-mapping>
没影响......