[英]android app socket.io not connecting to node.js server but android browser makes connection
I have setup node.js server running on port 3000. Connections from my laptop browser are successful and I can see the result in the console log. 我在端口3000上运行了setup node.js服务器。我的笔记本电脑浏览器连接成功,并且可以在控制台日志中看到结果。
In my android client I used nkzawa's Socket.io library, but it's not establishing connection with the server, where as when I try to open the http://ipaddr:PORT , in my android browser, connection is successful. 在我的android客户端中,我使用了nkzawa的 Socket.io库,但是它没有与服务器建立连接,就像当我尝试在我的android浏览器中打开http:// ipaddr:PORT时 ,连接成功。
To be sure, I used ngrok to create a public DNS for my localhost:PORT. 可以肯定的是,我使用ngrok为我的localhost:PORT创建了一个公共DNS。 When i use ngrok's public DNS for Socket connection, android throws an error saying Internet permission denied and unable to resolve hostname.
当我使用ngrok的公共DNS进行套接字连接时,android引发错误,指出Internet权限被拒绝并且无法解析主机名。 (Is it because of redirects?) but that address works fine on laptop and mobile browser.
(是因为重定向吗?),但是该地址在笔记本电脑和移动浏览器上可以正常工作。
I am completely stuck here. 我完全被困在这里。 Android Socket.io is not connecting to server.
Android Socket.io未连接到服务器。 I am following this tutorial .
我正在关注本教程 。 What am I missing?
我想念什么?
Server: 服务器:
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
connections =[];
server.listen(3000);
console.log("server running ...");
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.sockets.on('connection', function(socket) {
connections.push(socket);
console.log("new connection");
console.log('connected: %s sockets', connections.length);
// Disconeect
socket.on('disconnect', function(data){
connections.splice(connections.indexOf(socket),1);
console.log('Disconnection');
console.log('connected: %s sockets', connections.length);
});
});
Android Client: Android客户端:
import io.socket.client.IO;
import io.socket.client.Socket;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity" ;
private Socket mSocket;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
Log.i(TAG, "Attempting socket connection");
mSocket = IO.socket("http://10.10.11.44:3000/");
}
catch (URISyntaxException e){
Toast.makeText(getApplicationContext(),"Not connected",Toast.LENGTH_LONG).show();
Log.i(TAG, "ERROR : Socket connection failed");
throw new RuntimeException();
}
mSocket.connect();
if(mSocket.connected()){
Log.i(TAG, "Socket connection successful");
}
else{
Log.i(TAG, "Socket connection failed");
}
}
.
.
.
Android client Manifest: Android客户端清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.batmanlost.chatapp" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
可能为时已晚,无法答复,但是您可以在Android应用中将“ http://10.10.11.44:3000/ ”替换为“ http:// ipaddr:3000 ”。
如果您使用仿真器进行测试,则使用本地主机地址作为“ http://10.0.2.2:3000 ”;如果您使用移动设备进行测试,则需要将手机连接到与您使用相同的网络PC /笔记本电脑已连接,然后使用上面给出的相同地址进行连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.