I am doing everything as per this to connect localhost to app using socket.io .
but it is not connecting, i have connected my phone and laptop to my phone's network. I don't know where i am going in wrong direction. Please guide me.
I have this node server :
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 Side Code :
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)
}
}