avi silwal
avi silwal

Reputation: 41

Exstablishing a simple socket connect with localhost and android emulator

I am building an application for student attendance for my college. Where in the attendance is being taken via the college network. Each class room has a separate 'routers', and the system detects if student has present in the range. For the same, what I am trying to do is establish a socket connection, and every time that it connects, the server in return receives the WiFi information to judge the class room.

To establish, I first made this index.js file, here is the code for it

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req,res){
  res.sendFile(__dirname+'/index.html');
})

http.listen(4040, function(){
console.log("Server is listening on port 4040");
})
io.on('connection', function(socket){
  console.log('connect', "A user connected "+ socket.id)
  io.on('disconnect', function(socket){
    console.log("disconnect A user disconnected" + socket.id)
  })
})

Problem: The problem is that, 'connection' event is not working. It says that it's listening but nothing is getting console logged. I am using an Emulator as client and a local XAMPP server. However, the 'connection' event does work when I use browser as a client.

Here is my client side code:

private val socket = Socket()

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_student_home)
    setSupportActionBar(student_toolbar)

    val toggle = ActionBarDrawerToggle(
            this, student_drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
    student_drawer_layout.addDrawerListener(toggle)
    toggle.syncState()
    student_nav_view.setNavigationItemSelectedListener(this)

    socket.connect()
}
override fun onResume() {
    super.onResume()
    socket.connect()
}
override fun onStart() {
    super.onStart()
    socket.connect()
}
override fun onDestroy() {
    super.onDestroy()
    socket.disconnect()
}//[End : onDestroy]

fun Socket() : Socket{
    val socket : Socket
    try {
        socket = IO.socket("http://localhost:4040")
    } catch (e: URISyntaxException) {
        throw RuntimeException(e)
    }
    return socket;
}

Upvotes: 0

Views: 2826

Answers (1)

user6819011
user6819011

Reputation:

try to change "localhost" to your machine's ip address like "http://192.168.1.100:4040". can you post screenshot of backtrace?

Upvotes: 2

Related Questions