Java Autobahn Android:如何断开与服务器的连接
我在服务器端使用Jetty WebSockets,在客户端使用Autobahn Android 服务器和客户端之间的简单连接可以正常工作。但是当我试图处理失去连接的问题时,我遇到了一些麻烦 在Android上,我拥有的是:Java Autobahn Android:如何断开与服务器的连接,java,android,websocket,jetty,autobahn,Java,Android,Websocket,Jetty,Autobahn,我在服务器端使用Jetty WebSockets,在客户端使用Autobahn Android 服务器和客户端之间的简单连接可以正常工作。但是当我试图处理失去连接的问题时,我遇到了一些麻烦 在Android上,我拥有的是: private void connectToServer() { try { mConnection.connect(getString(R.string.server_addr), new WebSocketHandler(){
private void connectToServer() {
try {
mConnection.connect(getString(R.string.server_addr), new WebSocketHandler(){
// connection au serveur
@Override
public void onOpen(){
Log.d(TAG, "Connected");
Log.d(TAG, "First connexion, sending MAC @");
Log.d(TAG, "My MAC Addr: "+ macAddr);
mConnection.sendTextMessage(macAddr);
}
// reception d'un message text
@Override
public void onTextMessage(String payload) {
//TODO
}
// fermeture de la connexion
@Override
public void onClose(int code, String reason) {
Log.d(TAG, "Connection lost. "+reason);
if(mConnection.isConnected()){
Log.d(TAG, "Still connected, disconnect!");
mConnection.disconnect();
}
if(code<4000){
int totalWaitTime = 0;
int waitTime = 0;
Log.d(TAG, "Should be disconnected");
while(!mConnection.isConnected()){
try {
waitTime= random.nextInt(MAX_TO_WAIT - MIN_TO_WAIT + 1) + MIN_TO_WAIT;
Log.d(TAG, "I'll wait "+waitTime+"ms");
totalWaitTime +=waitTime;
Log.d(TAG, "Waiting for "+totalWaitTime+"ms");
if(totalWaitTime <= HOUR_TO_MS){
Thread.sleep(waitTime);
Log.d(TAG, "Trying to reconnect");
connectToServer();
}else{
throw new InterruptedException("Attempt to connect to the server during 1 hours without success");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
} catch (WebSocketException e) {
Log.e(TAG, "Error on connect: "+e.toString());
Log.d(TAG, "is connected: "+mConnection.isConnected());
if(mConnection.isConnected())
mConnection.disconnect();
connectToServer();
}
}
但正如您所看到的,我在onClose()中关闭了连接,并在捕获WebSocketException时关闭了连接。这个方法真的有效吗?还是我做错了
顺便说一下,mConnection是最终的。也许问题就在这里
private final WebSocketConnection mConnection = new WebSocketConnection();
在服务器上,当连接丢失时,我手动关闭会话:
@OnWebSocketClose
public void onClose(Session session, int closeCode, String closeReason){
try {
System.out.println("connexion closed. Reason: "+closeReason);
pingPongTimer.cancel();
if(session.isOpen())
session.close();
WebSocketsCentralisation.getInstance().leave(this);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
提前感谢了不起的人 所以,我写的东西有点乱。我现在所做的工作:
private void connectToServer() {
try {
mConnection.connect(getString(R.string.server_addr), new WebSocketHandler(){
// connection au serveur
@Override
public void onOpen(){
Log.d(TAG, "Connected");
Log.d(TAG, "First connexion, sending MAC @");
Log.d(TAG, "My MAC Addr: "+ macAddr);
mConnection.sendTextMessage(macAddr);
}
// reception d'un message text
@Override
public void onTextMessage(String payload) {
//TODO
}
// fermeture de la connexion
@Override
public void onClose(int code, String reason) {
Log.d(TAG, "Connection lost. "+reason+" error code : "+code);
if(code<4000){
reconnectToServer();
}
}
});
} catch (WebSocketException e) {
Log.e(TAG, "Error on connect: "+e.toString());
Log.d(TAG, "is connected: "+mConnection.isConnected());
}
}
private void reconnectToServer() {
try {
if(goConnect){
goConnect = false;
Thread.sleep(1000);
Log.d(TAG, "DISCONNECT:");
// mConnection.disconnect();
Log.d(TAG, "ReconnectTimer Launched");
new ReconnectTask().run();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private class ReconnectTask extends TimerTask{
@Override
public void run() {
try{
if(totalWaitTime<HOUR_TO_MS){
if(!mConnection.isConnected()){
int waitTime= random.nextInt(MAX_TO_WAIT - MIN_TO_WAIT + 1) + MIN_TO_WAIT;
Log.d(TAG, "Next tentative to connect in "+waitTime+" ms");
totalWaitTime +=waitTime;
reconnectTimer.schedule(new ReconnectTask(), waitTime);
connectToServer();
}else{
Log.d(TAG, "Connected to the server again");
reinitializeReconnection();
}
}else throw new InterruptedException("Attempt to connect to the server during 1 hours without success");
}catch(InterruptedException e){
Log.d(TAG, e.getMessage());
}
}
}
private void reinitializeReconnection(){
reconnectTimer.purge();
goConnect = true;
totalWaitTime = 0;
}
private void connectToServer(){
试一试{
mConnection.connect(getString(R.string.server_addr),新的WebSocketHandler(){
//服务连接
@凌驾
公共开放(){
日志d(标记“已连接”);
Log.d(标记“第一连接,发送MAC”);
Log.d(标记“我的MAC地址:+macAddr”);
mConnection.sendTextMessage(macAddr);
}
//接收联合国信息文本
@凌驾
公共void onTextMessage(字符串负载){
//待办事项
}
//连接费
@凌驾
公共void onClose(int代码,字符串原因){
Log.d(标记“连接丢失”。+原因+”错误代码:+代码);
如果(codeadd)这行mConnection=null;@Poovizhirajan.N:我不能这么做,mConnection是最终的!好的,等等,我会查一下电话u@Poovizhirajan.N谢谢您好,我可以问您如何实例化reconnectTimer对象吗?
private void connectToServer() {
try {
mConnection.connect(getString(R.string.server_addr), new WebSocketHandler(){
// connection au serveur
@Override
public void onOpen(){
Log.d(TAG, "Connected");
Log.d(TAG, "First connexion, sending MAC @");
Log.d(TAG, "My MAC Addr: "+ macAddr);
mConnection.sendTextMessage(macAddr);
}
// reception d'un message text
@Override
public void onTextMessage(String payload) {
//TODO
}
// fermeture de la connexion
@Override
public void onClose(int code, String reason) {
Log.d(TAG, "Connection lost. "+reason+" error code : "+code);
if(code<4000){
reconnectToServer();
}
}
});
} catch (WebSocketException e) {
Log.e(TAG, "Error on connect: "+e.toString());
Log.d(TAG, "is connected: "+mConnection.isConnected());
}
}
private void reconnectToServer() {
try {
if(goConnect){
goConnect = false;
Thread.sleep(1000);
Log.d(TAG, "DISCONNECT:");
// mConnection.disconnect();
Log.d(TAG, "ReconnectTimer Launched");
new ReconnectTask().run();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private class ReconnectTask extends TimerTask{
@Override
public void run() {
try{
if(totalWaitTime<HOUR_TO_MS){
if(!mConnection.isConnected()){
int waitTime= random.nextInt(MAX_TO_WAIT - MIN_TO_WAIT + 1) + MIN_TO_WAIT;
Log.d(TAG, "Next tentative to connect in "+waitTime+" ms");
totalWaitTime +=waitTime;
reconnectTimer.schedule(new ReconnectTask(), waitTime);
connectToServer();
}else{
Log.d(TAG, "Connected to the server again");
reinitializeReconnection();
}
}else throw new InterruptedException("Attempt to connect to the server during 1 hours without success");
}catch(InterruptedException e){
Log.d(TAG, e.getMessage());
}
}
}
private void reinitializeReconnection(){
reconnectTimer.purge();
goConnect = true;
totalWaitTime = 0;
}