socket.io-client-java icon indicating copy to clipboard operation
socket.io-client-java copied to clipboard

Kotlin: No connection between my server and my client when i try to connect

Open nowlow opened this issue 4 years ago • 7 comments

Hello, I'm trying to integrate socket.io in my Kotlin application but when I launch it, it doesn't look like it's trying to connect to the server. What I tried: This is my client-side code:

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import io.socket.client.IO
import io.socket.client.Socket
import io.socket.emitter.Emitter
import java.lang.Exception


class MainActivity : AppCompatActivity() {
    lateinit var socket: Socket
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        try {
            socket = IO.socket("http://address:port/")
            socket.on(Socket.EVENT_CONNECT) {
                println("Socket connected")
            }
            socket.connect()
        } catch (ex:Exception) {
            println(ex)
        }
    }
}

And back in nodejs:

and express = require('express')
let app = express()
let http = require('http').createServer(app)
let io = require('socket.io')(http)
const path = require('path')

app.use(express.static(path.join(__dirname, "public")))

app.get('/', (req, res) => {
    res.send ("you shouldn't be here")
})

io.on('connection', (socket) => {
    console.log('new user')
})

io.on('foo', (data) => {
    console.log(data)
})

http.listen(5624, () => {
    console.log('listening on localhost:5624')
})

I also tried to open a C socket, same problem, when I use the javascript client I receive data but when I try from the application, no connection attempt is made. Is my code no good? It's from an official tutorial.

Devices: Client: Oneplus 7 on Android Q. Server: VPS on archlinux.

nowlow avatar Apr 25 '20 11:04 nowlow

try using kitkat.. is it works?

erlangparasu avatar Apr 27 '20 14:04 erlangparasu

I've just tried with the Kit-kat SDK and that does not work. Unfortunately, I don't have any Kit-kat device under my hand for the moment.

nowlow avatar Apr 27 '20 14:04 nowlow

in manifest file, on application tag, try to add attribute: android:usesCleartextTraffic="true"

erlangparasu avatar Apr 27 '20 23:04 erlangparasu

just tried that, seems not to get better

nowlow avatar Apr 28 '20 11:04 nowlow

yesterday i have try to make simple app to connect to socket.io server (android device connected via wifi, same network) and it works.. server emit an event, and on android app, i got the event https://gitlab.com/erlangparasu/myapptrysocketio

erlangparasu avatar Apr 28 '20 12:04 erlangparasu

& let's try to replace println with Log.d :)

erlangparasu avatar Apr 28 '20 12:04 erlangparasu

@erlangp { "name": "socket-chat-example", "version": "0.0.1", "description": "my first socket.io app", "dependencies": { "express": "^4.15.2", "socket.io": "^2.3.0" } } youre using 2.3.0 try using 3.0.0 it will not work

spiritbroski avatar Nov 13 '20 12:11 spiritbroski