I’m trying to establish a connection from my Android Studio application to a Server who’s running on Visual Studio Code, to send data with Websockets. The server works just fine and is implemented with this function:
async def main():
server = await websockets.serve(
connect,
'localhost',
8766,
)
await server.wait_closed()
Now I’m trying to connect to this server on my Android Studio Application with the following code (example from ktor.io):
fun main() {
val client = HttpClient(CIO) {
install(WebSockets)
}
runBlocking {
client.webSocket(port = 8766) {
while(true) {
val othersMessage = incoming.receive() as? Frame.Text
println(othersMessage?.readText())
val myMessage = Scanner(System.`in`).next()
if(myMessage != null) {
send(myMessage)
}
}
}
}
client.close()
}
Therefore, i added the necessary dependencies:
implementation("io.ktor:ktor-client-cio:1.6.7")
implementation("io.ktor:ktor-client-websockets:1.6.7")
Unfortunately, the connection is refused (“Failure(java.net.ConnectException: Connection refused)”). When I try to connect on Visual Studio Code, it works perfectly fine (with deactivated Firewall):
async with websockets.connect("ws://localhost:8766") as websocket:
...
Does anybody know where the problem could be?