Код: Выделить всё
< /code> и
android:usesCleartextTraffic="true"Это код на стороне клиента:
import io.socket.client.IO
import io.socket.client.Socket
object SocketTest {
private val socket = IO.socket("http://10.0.2.2:3000/")
fun connect() {
socket.connect()
socket.on(Socket.EVENT_CONNECT) {
println("Connected to server")
}
socket.on(Socket.EVENT_CONNECT_ERROR) {
println("Error connecting to server")
}
socket.on(Socket.EVENT_DISCONNECT) {
println("Disconnected from server")
}
}
}
< /code>
@Composable
fun SocketTestScreen(){
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(text = "Socket Test Screen")
Button(onClick = { SocketTest.connect() }) {
Text(text = "Connect to Socket")
}
}
}
< /code>
And this is the server code:
const express = require('express');
const http = require('http');
const socketIO = require('socket.io');
const app = express();
const server = http.createServer(app);
const io = socketIO(server);
io.on('connection', (socket) => {
console.log('A user connected');
socket.on('disconnect', () => {
console.log('A user disconnected');
});
});
server.listen(3000, () => {
console.log('Server is running on port 3000');
});
< /code>
When I start listening with the server and run the app I only get the "Error connecting to the server" message in the client. Any help on what the issue might be would be appriciated!
Подробнее здесь: https://stackoverflow.com/questions/795 ... n-and-node
Мобильная версия