简体   繁体   English

无法在android socketio客户端与socketio服务器之间建立连接

[英]Can't establish connection between android socketio client to socketio server

I'm trying to develop a chat app with Socket.IO. 我正在尝试使用Socket.IO开发一个聊天应用程序。 My server side is Node.js with express + socket.io libraries installed and my client side is Android + Web. 我的服务器端是Node.js,安装了express + socket.io库,我的客户端是Android + Web。

I tried using at my client side all of these jars: 我尝试在我的客户端使用所有这些罐子:

  1. 'com.github.nkzawa:socket.io-client:0.3.0' - Which is found at socket.io website tutorial on making a android to socketio server. 'com.github.nkzawa:socket.io-client:0.3.0' - 在socket.io网站上可以找到关于制作android到socketio服务器的教程。
  2. 'io.socket:socket.io-client:0.6.1' - Which is found at nkzawa android chat demo repository. 'io.socket:socket.io-client:0.6.1' - 在nkzawa android chat demo repository中找到。
  3. 'com.github.nkzawa:socket.io-client:0.6.0' - another version in the past that worked for me. 'com.github.nkzawa:socket.io-client:0.6.0' - 过去的另一个版本对我有用。

All of these jars didn't work for the android side. 所有这些罐子都不适用于android方面。 On my server side i have the latest version - 1.3.7 of Socket.IO library and i tried each of the jars above to connect to it. 在我的服务器端,我有最新版本--Socket.IO库的1.3.7,我尝试了上面的每个罐子连接到它。

I also tried using Socket.IO version 1.3.6 that worked for me in the past with the 3rd jar on the list. 我也尝试使用Socket.IO版本1.3.6,它在过去对我有用,列表中的第3个jar。

Plus i tried changing the url in the android side to http://10.0.2.2:3000 , http://192.168.1.100:3000 on each of these jars versions... 另外,我试图改变在Android端的URL http://10.0.2.2:3000http://192.168.1.100:3000每个这些JAR版本的...

Notes - 笔记 -

  1. Note - i am working with genymotion in Android. 注意 - 我在Android中使用genymotion。
  2. I have INTERNET_PERMISSION. 我有INTERNET_PERMISSION。
  3. The socket.io on the server side works on web browser(local pc). 服务器端的socket.io适用于Web浏览器(本地PC)。

This is my server side: 这是我的服务器端:

var app = require('express')();
var bodyParser = require('body-parser');
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));

io.on('connection', function(socket){
    console.log(socket.id);
    console.log('Client connected');
});

http.listen(3000, function(){
    console.log('listening on port 3000');
});

This is my client side : 这是我的客户方:

 private Socket mSocket;
    {
        try {
            mSocket = IO.socket("http://10.0.3.2:3000");
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSocket.connect();
        //setupActivity();
    }

I got to the point that i am really clueless to what to do next , so any help will help me right now :) Thanks 我明白我对下一步该做什么一无所知,所以任何帮助都会帮助我吧:)谢谢

Edit - The problem has been solved. 编辑 - 问题已解决。 Apparently genymotion doesn't need the 10.0.3.2 ip to connect to localhost so I entered my pc ip address and it worked. 显然genymotion不需要10.0.3.2 ip连接到localhost所以我输入了我的电脑IP地址,它工作。 By the way I didn't thought about connection remotely , that way i could have eliminate the problem and know that the problem is connecting with the ip address. 顺便说一句,我没有想到远程连接,这样我可以消除问题,并知道问题是连接到IP地址。

Solution Goes here, I almost missed it because it wasn't where it was supposed to be. 解决方案来到这里,我几乎错过了它,因为它不是应该的位置。 (credit to OP: Ido Magor): (信用OP:Ido Magor):

The problem has been solved. 问题已经解决了。 Apparently genymotion doesn't need the 10.0.3.2 ip to connect to localhost so I entered my pc ip address and it worked. 显然genymotion不需要10.0.3.2 ip连接到localhost所以我输入了我的电脑IP地址,它工作。 By the way I didn't thought about connection remotely , that way i could have eliminate the problem and know that the problem is connecting with the ip address. 顺便说一句,我没有想到远程连接,这样我可以消除问题,并知道问题是连接到IP地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM