连接错误:io.socket.engineio.client.EngineIOException:xhr 轮询错误

Posted

技术标签:

【中文标题】连接错误:io.socket.engineio.client.EngineIOException:xhr 轮询错误【英文标题】:Error connecting: io.socket.engineio.client.EngineIOException: xhr poll error 【发布时间】:2018-10-30 10:31:35 【问题描述】:

我正在尝试使用 socket.io 连接到 Nodejs 服务器和 android 客户端。尝试连接时出现 xhr poll 错误。我见过其他人提出的此类问题,但没有解决方案。我在节点服务器上有套接字 io 2.1.1 并在 Android 中使用 'io.socket:socket.io-client:0.8.3'。我的安卓版本是奥利奥。

这是我的 Android 客户端代码:

package com.example.socketiodemo;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


import io.socket.client.IO;
import io.socket.client.Manager;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;


public class MainActivity extends AppCompatActivity 

    private TextView display_text;
    private Socket mSocket;
    Manager mManager;
    private boolean fClientClosedConnection;
    private boolean fClientIsConnected;
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        display_text = (TextView)findViewById(R.id.display_text);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            
        );
        fClientClosedConnection = false;
        fClientIsConnected = false;

        new MyAsyncTask(mSocket).execute();
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) 
            return true;
        

        return super.onOptionsItemSelected(item);
    


    private class MyAsyncTask extends AsyncTask<String, Void, String> 
        Socket mSocket;
        Manager mManager;
        MyAsyncTask(Socket socket) 
            mSocket = socket;
        
        @Override
        protected String doInBackground(String... params) 
            Log.e(TAG, "Initiating the connection");


            try 

                mSocket = IO.socket("http://192.168.0.104:4444");
                mSocket.on(Socket.EVENT_CONNECT, onConnect);
                mSocket.on(Socket.EVENT_DISCONNECT, onDisconnect);
                mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError);
                mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, onTimeoutError);
                mSocket.on(Socket.EVENT_ERROR, onError);
                mSocket.on("data", onNewMessage);
                mSocket.connect();
                while (!fClientClosedConnection) 
                    // Keep the thread active
                
            
            catch (URISyntaxException e)
            
                e.printStackTrace();
            
            Log.e(TAG, "Finished the background Process");
            return "";
        
        @Override
        protected void onPostExecute(String result) 
            mSocket.close();
        
    

    private Emitter.Listener onConnect = new Emitter.Listener() 
        @Override
        public void call(Object... args) 
            runOnUiThread(new Runnable() 
                @Override
                public void run() 
                    Log.e(TAG, "onConnect");
                    if(!fClientIsConnected) 
                        Toast.makeText(getApplicationContext(),
                                "Client is connected", Toast.LENGTH_LONG).show();
                        fClientIsConnected = true;
                    
                
            );
        
    ;

    private Emitter.Listener onDisconnect = new Emitter.Listener() 
        @Override
        public void call(Object... args) 
            runOnUiThread(new Runnable() 
                @Override
                public void run() 
                    Log.e(TAG, "diconnected");
                    fClientIsConnected = false;
                    Toast.makeText(getApplicationContext(),
                            "Client is disconnected", Toast.LENGTH_LONG).show();
                
            );
        
    ;

    private Emitter.Listener onConnectError = new Emitter.Listener() 
        @Override
        public void call(final Object... args) 
            runOnUiThread(new Runnable() 
                @Override
                public void run() 
                    Log.e(TAG, "Error connecting: " + args[0].toString());
                    Toast.makeText(getApplicationContext(),
                            "Client connect error", Toast.LENGTH_LONG).show();
                
            );
        
    ;

    private Emitter.Listener onTimeoutError = new Emitter.Listener() 
        @Override
        public void call(Object... args) 
            runOnUiThread(new Runnable() 
                @Override
                public void run() 
                    Log.e(TAG, "Timeout Error ");
                
            );
        
    ;

    private Emitter.Listener onError = new Emitter.Listener() 
        @Override
        public void call(Object... args) 
            runOnUiThread(new Runnable() 
                @Override
                public void run() 
                    Log.e(TAG, "Error ");
                
            );
        
    ;

    private Emitter.Listener onNewMessage = new Emitter.Listener() 
        @Override
        public void call(final Object... args) 
            runOnUiThread(new Runnable() 
                @Override
                public void run() 
                    JSONObject data = (JSONObject) args[0];
                    String message;
                    try 
                        message = data.getString("message");
                     catch (JSONException e) 
                        Log.e(TAG, e.getMessage());
                        return;
                    

                    printMessage(message);
                    if (message.contentEquals("Bye"))
                    
                        fClientClosedConnection = true;
                    
                
            );
        
    ;

    private void printMessage (String message)
    
        display_text.setText(message);
    

这是我的 nodejs 服务器:

process.title = 'node-android';
var app = require('http').createServer()
var io = require('socket.io')(app);
app.listen(4444, onListenStart);

io.on('connection', onConnect);

function onListenStart()

    console.log("Server listening on port: " + SERVER_PORT);


function onConnect(client)

    console.log("client is connected");
    client.emit("data",  message: 'Hello');
    client.on('event', onMessageReceived);
    client.on('disconnect', onDisconnect);


function onMessageReceived(data)

    console.log(data);


function onDisconnect()

    console.log("client is disconnected");

我得到了这些日志:

05-20 21:48:56.404 28026-28026/com.example.socketiodemo E/MainActivity: Error connecting: io.socket.engineio.client.EngineIOException: xhr poll error
05-20 21:48:57.921 28026-28026/com.example.socketiodemo E/MainActivity: Error connecting: io.socket.engineio.client.EngineIOException: xhr poll error

请帮忙,因为我已经坚持了 3 天。

【问题讨论】:

你解决了吗? 有人能解决这个问题吗? 你有什么解决办法吗?我也面临同样的问题 @Abhishek 对此有什么解决方案吗? @DeepakRana 我添加了通过我的服务器进行身份验证所需的 SSL 证书。 【参考方案1】:

我在节点服务器上有套接字 io 2.1.1 并使用 'io.socket:socket.io-client:0.8.3'

我有类似的情况,将库版本更改为 1.0.0 有帮助。

【讨论】:

是的,使用 'io.socket:socket.io-client:1.0.0' 有助于连接稳定性和重新连接,但如果服务器在 HTTPS 上,则它再次无法连接到服务器,并且出现上述异常SSLHandShake 错误。【参考方案2】:

您必须在清单中的应用程序标记中添加此代码,如下所示, 它解决了错误连接:io.socket.engineio.client.EngineIOException: xhr poll error 同样的问题

<Application . . android:usesCleartextTraffic="true"> . . . <Application/>

【讨论】:

以上是关于连接错误:io.socket.engineio.client.EngineIOException:xhr 轮询错误的主要内容,如果未能解决你的问题,请参考以下文章

如何轻松解决MYSQL数据库连接过多的错误

宽带连接错误代码

Win10宽带连接拨号错误651怎么办?

概要设计说明书

连接失败 错误为651 已拒绝远程链接

连接mysql错误2013