socket.io-client-java
socket.io-client-java copied to clipboard
Kotlin: No connection between my server and my client when i try to connect
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.
try using kitkat.. is it works?
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.
in manifest file, on application tag, try to add attribute:
android:usesCleartextTraffic="true"
just tried that, seems not to get better
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
& let's try to replace println
with Log.d
:)
@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