Socket.io未连接到本地主机服务器

时间:2019-09-12 11:40:20

标签: android sockets socket.io

I am doing everything as per this to connect localhost to app using socket.io .

但未连接,我已将手机和笔记本电脑连接到手机的网络。我不知道我往错误的方向去。请引导我。

我有此节点服务器:

var express = require('express');
const app = express();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
var mysql = require('mysql');


var con = mysql.createConnection({
    host: "localhost",
    port : "3306",
    user: "root",
    password: "",
    database: "socket_database",
    charset : 'utf8mb4_bin',
    multipleStatements: true
});

app.use(express.static('public'));

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

io.on('connection', function(socket){
    let sender = socket.handshake.query.user_id;
    console.log('a user connected with id - '+ sender);

.
.
.

}

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

Android附加代码:

private var mSocket: Socket? = null


    override fun onCreate() {
        super.onCreate()

        try {

            mSocket = IO.socket("http://192.168.40.28:3000")

            mSocket?.on(Socket.EVENT_CONNECT, Emitter.Listener {
                Log.d("TAG", "Socket Connected!")

            })
        } catch (e: Exception) {
            println(e.localizedMessage)
        }
    }

0 个答案:

没有答案