无法从Android客户端连接Nodejs Socket io服务器(Openshift免费帐户)

时间:2016-04-10 13:16:38

标签: android node.js sockets socket.io openshift

使用nodejs socket io作为服务器,Android作为客户端设备开发聊天应用程序。

在Openshift(免费帐号)中部署服务器..并且能够从Web应用程序连接服务器,无法从android连接服务器。

我参考了https://github.com/nkzawa/socket.io-android-chat

服务器:

var express = require('express');
var app = express();
app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "http://xxx-xxx.rhcloud.com:8000");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Headers", "Content-Type");
    res.header("Access-Control-Allow-Methods", "PUT, GET, POST, DELETE, OPTIONS");
    res.header('Access-Control-Allow-Credentials', true);
    next();
});
var server = require('http').createServer(app);
var io = require('socket.io').listen(server, { origins: 'http://xxx-xxx.rhcloud.com:8000' });

app.get('/', function (req, res) {
    res.sendfile('index.html');
});

io.on('connection', function (socket) {
    socket.on('chatmessage', function (msg) {      
        console.log(msg);       
        io.emit('chatmessage', msg);
    });
server.listen(process.env.OPENSHIFT_NODEJS_PORT, process.env.OPENSHIFT_NODEJS_IP);

Android客户端:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    try {           
        mSocket = IO.socket("http://xxx-xxx.rhcloud.com:8000");
        mSocket.connect();
        mSocket.on("chatmessage", Test);
        mSocket.emit("chatmessage", "xxxxxxxxxxx");

        mTxtView = (TextView) findViewById(R.id.txt_test);
        mTxtView.setText("test.....");
    } catch (Exception ex) {
        System.out.println("Error=" + ex.getMessage());
    }
}
private Emitter.Listener Test = new Emitter.Listener() {
    @Override
    public void call(final Object... args) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Gson j = new Gson();
                String s = j.toJson(args[0]);
                String username;
                String message;                   
                mTxtView.setText(s);
            }
        });
    }
};

Android Logcat:

 I/System.out: [CDS][DNS] getAllByNameImpl netId = 0
 D/libc-netbsd: [getaddrinfo]: hostname=xxx-xxxxx.rhcloud.com; servname=(null); cache_mode=(null), netid=0; mark=0
 D/libc-netbsd: [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0
04-10 18:31:25.990 32324-1181/example.com.chatsocketio D/libc-netbsd: [getaddrinfo]: hostname=xxx-xxxxx.rhcloud.com; servname=(null); cache_mode=(null), netid=0; mark=0
 D/libc-netbsd: [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=1024; ai_family=0
 D/libc-netbsd: getaddrinfo: xxx-xxxxx.rhcloud.com get result from proxy >>
 I/System.out: propertyValue:true
 I/System.out: [CDS]rx timeout:0
 I/System.out: [socket][13] connection xxx-xxxxx.rhcloud.com/xx.xxx.xxx.xxx:8000;LocalPort=51588(10000)
 I/System.out: [CDS]connect[xxx-xxxxx.rhcloud.com/xx.xxx.xxx.xxx:8000] tm:10
 D/Posix: [Posix_connect Debug]Process example.com.chatsocketio :8000 
 I/System.out: [socket][/10.0.0.12:51588] connected
 I/System.out: [OkHttp] sendRequest>>
 I/System.out: [OkHttp] sendRequest<<
 I/System.out: [CDS]close[51588]
 I/System.out: [CDS][DNS] getAllByNameImpl netId = 0

注意:我已成功从Web应用程序连接到服务器,但未从Android

连接

请帮帮我。

1 个答案:

答案 0 :(得分:1)

由于您已经开发了自己的网络应用程序,您可能不想考虑我的答案并退后一步,但我写了一个教程,将Android客户端与Node.js网络服务器连接(没有任何外部库) ):

https://causeyourestuck.io/2016/04/27/node-js-android-tcpip/

祝你好运!