socket-io Java客户端未连接到nginx / node js服务器

时间:2018-09-04 17:30:29

标签: node.js sockets nginx socket.io

我有一个android应用程序,它在我的本地环境中通过nodejs和express运行良好。

我在服务器上安装了nodejs和socket io,我的应用似乎无法正确连接到服务器。

当应用程序打开时,套接字io客户端应该连接到套接字io服务器。客户端可以在服务器看到客户端并对其进行注册时连接到服务器...但是,当我尝试从客户端或服务器发出某些信息时,它无法到达目的地。

那是我的android客户端的一部分

    socket = IO.socket(SERVER_ADDRESS);
        socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {

            @Override
            public void call(Object... args) {
                NifiLog.log("Connected To Socket IO"); //this does not work
            AccessUtils.setOnlineStatus(context, "online");
            if(AccessUtils.getUser(context)!=null&&AccessUtils.getUser(context).get_id()!=null){
                socket.emit("online_acknowledgement", AccessUtils.getUser(context).get_id(),AccessUtils.getToken(context), new Ack() {
                    @Override
                    public void call(Object... args) {
                        String response = (String) args[0];
                    }
                });
            }
            }

        }).on("friend_online", new Emitter.Listener() {

            @Override
            public void call(Object... args) {
            User friend = NifiGson.getGson().fromJson(args[0].toString(), User.class);
            addFriend(friend);
            eventBus.postSticky(new FriendOnlineEvent(friend));
            }

        })

NifiLog.log(“已连接到套接字IO”);永远不会发生

这是我的服务器设置

  location / {
            proxy_pass http://localhost:3040;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
            proxy_request_buffering off;
            proxy_buffering off;
            proxy_read_timeout 180s;
    }

    location /socket {
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_http_version 1.1;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_pass http://localhost:3030;
}

节点js在端口3040上运行,而套接字io在端口3030上运行

服务器上的套接字io

 const express = require('express');

 var http = require('http').Server(express);
 global.io = require('socket.io')(http);

 const server = app.listen(config.port, function(){
console.log("Express started on " +config.base_url +' in '+config.env +' environment. Press Ctrl + C to terminate');
mongoose.connect(config.db.uri, config.db.options)
.then(()=> { log.info(`Succesfully Connected to the Mongodb Database  at URL : `+config.db.uri)})
.catch((error)=> { log.error(error)})
});

require('./routes/socketio_routes.route');

http.listen(config.socket_port, function(){
    console.log('Socket IO listening on *:'+config.socket_port); //this works
});

 io.on('connection', function(socket){
    console.log('a user connected'); //this works
    socket.on('online_acknowledgement', function(id, token, acknowledgement){

    }

    socket.on('disconnect', function(data){
    console.log('a user disconnected #######'); //this works
 } 

 socket.on('game_start', function(game, token){
    console.log('a game start'); //client sends this to server but it never reaches here - this never works
 }


 }

感谢您的帮助。

0 个答案:

没有答案