Skip to content

Commit

Permalink
Merge pull request #2 from raheemadamboev/1.1.1
Browse files Browse the repository at this point in the history
1.1.1
  • Loading branch information
raheemadamboev authored May 8, 2022
2 parents 1ea89a9 + 514dfa3 commit fba6622
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
2 changes: 1 addition & 1 deletion check_internet/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ afterEvaluate {

groupId = 'com.github.raheemadamboev'
artifactId = 'check-internet-android'
version = '1.1'
version = '1.1.1'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import kotlinx.coroutines.withContext
import java.io.IOException
import java.net.InetSocketAddress
import java.net.Socket
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine

class CheckInternet {

Expand All @@ -21,9 +19,7 @@ class CheckInternet {
fun check(listener: (connected: Boolean) -> Unit) {
CoroutineScope(Dispatchers.IO).launch(Dispatchers.IO) {
try {
val socket = Socket()
socket.connect(InetSocketAddress(HOST_NAME, PORT), TIMEOUT)
socket.close()
Socket().use { it.connect(InetSocketAddress(HOST_NAME, PORT), TIMEOUT) }
withContext(Dispatchers.Main) {
listener(true)
}
Expand All @@ -36,16 +32,12 @@ class CheckInternet {
}

suspend fun check(): Boolean {
return suspendCoroutine { continuation ->
CoroutineScope(Dispatchers.IO).launch(Dispatchers.IO) {
try {
val socket = Socket()
socket.connect(InetSocketAddress(HOST_NAME, PORT), TIMEOUT)
socket.close()
continuation.resume(true)
} catch (e: IOException) {
continuation.resume(false)
}
return withContext(Dispatchers.IO) {
try {
Socket().use { it.connect(InetSocketAddress(HOST_NAME, PORT), TIMEOUT) }
return@withContext true
} catch (e: IOException) {
return@withContext false
}
}
}
Expand Down

0 comments on commit fba6622

Please sign in to comment.