我正在尝试使用socket.io连接到Nodejs服务器和Android客户端。尝试连接时,我收到xhr轮询错误。我已经看到其他人提出的这些问题,但没有解决方案。我在节点服务器上有socket io 2.1.1,在Android中使用'io.socket:socket.io-client:0.8.3'。我的Android版本是奥利奥。
这是我的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天开始坚持这个。
答案 0 :(得分:0)
我在节点服务器上具有套接字io 2.1.1,并使用'io.socket:socket.io-client:0.8.3'
我遇到了类似的情况,将库版本更改为1.0.0很有帮助。
答案 1 :(得分:0)
您必须将此代码添加到清单中的清单中的应用程序标记中,如下所示, 它解决了错误连接:io.socket.engineio.client.EngineIOException:xhr轮询错误 相同的问题
<Application . . android:usesCleartextTraffic="true"> . . . <Application/>