与NodeJs和Express.io的连接收到错误

时间:2015-03-04 17:26:57

标签: java android ios node.js sockets

我是nodejs和express.io的新手,在android应用程序中我写了这个简单的代码来连接android或java但是iget连接错误

app = require('express.io')()
app.http().io()

// Broadcast the new visitor event on ready route.
app.io.route('ready', function(req) {
    req.io.broadcast('new visitor')
    console.log('ready');
})

// Send client html.
app.get('/', function(req, res) {
    //res.sendfile(__dirname + '/client.html')
  req.io.broadcast('new visitor')
  console.log('hello');
})

app.listen(7076)

此代码可能是正确的,在浏览器中打开http://192.168.1.5:7076链接后我收到hello消息,现在使用下面的代码我尝试连接但是我收到连接错误:

private static final String SERVER_ADDRESS = "http://192.168.1.5:7076";
private Socket mSocket;
{
    try {
        mSocket = IO.socket(SERVER_ADDRESS);
    } catch (URISyntaxException e) {
        Log.e("Error URI", String.valueOf(e));
        throw new RuntimeException(e);
    }
}
public class ActivityHome extends FragmentActivity {
    ...
    mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError);
    mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);
    mSocket.on("new message", onNewMessage);
    mSocket.on("user joined", onUserJoined);
    mSocket.on("user left", onUserLeft);
    mSocket.on("typing", onTyping);
    mSocket.on("stop typing", onStopTyping);
    mSocket.connect();
    ...
}

private Emitter.Listener onConnectError = new Emitter.Listener() {
    @Override
    public void call(Object... args) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(),
                        "CONNECT ERROR", Toast.LENGTH_LONG).show();
            }
        });
    }
};

0 个答案:

没有答案