koush / AndroidAsync

Asynchronous socket, http(s) (client+server) and websocket library for android. Based on nio, not threads.
Other
7.52k stars 1.56k forks source link

WebSocket. Can't send message Client->Server #672

Open ralexey opened 4 years ago

ralexey commented 4 years ago

Hello.

I need to implement 2-way communication between socket server and client. I use 3.0.8 version of the library. App creates a WebSocket server and client. Server sends a welcome message on create - all work fine. But, when client tries to send a message to server it causes immediately connection close. This is the test app:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        startServer()
        buttonConnect.setOnClickListener {
            connectToServer()
        }
        buttonSend.setOnClickListener {
            sendMessageToServer()
        }
    }

    private var server: AsyncHttpServer? = null
    private val _sockets = ArrayList<WebSocket>()
    private var client: WebSocket? = null

    private fun startServer() {
        server = AsyncHttpServer()
        server?.listen(AsyncServer.getDefault(), 33333)
        Log.d("SERVER", "Ready to start")
        server?.websocket(
            "/socket"
        ) { webSocket, _ ->
            Log.d("SERVER", "New client")
            _sockets.add(webSocket)
            webSocket.setClosedCallback { error ->
                Log.d("SERVER", "Closed socket: ${error?.message}")
                _sockets.remove(webSocket)
            }
            webSocket.setStringCallback { data ->
                Log.d("SERVER", "Message: $data")
            }
            Log.d("SERVER", "Send message")
            webSocket.send("Hello from server")
        }
    }

    private fun connectToServer() {
        Log.d("CLIENT", "Ready to start")
        AsyncHttpClient.getDefaultInstance().websocket(
            "ws://127.0.0.1:33333/socket", emptyArray()
        ) { ex: Exception?, webSocket: WebSocket? ->
            if (ex != null) {
                return@websocket
            }
            Log.d("CLIENT", "Connected to server")
            client = webSocket
            webSocket?.setStringCallback { data ->
                Log.d("CLIENT", "Message: $data")
            }
            webSocket?.setClosedCallback { error ->
                Log.d("CLIENT", "Closed socket: ${error?.message}")
            }
        }
    }

    private fun sendMessageToServer() {
        Log.d("CLIENT", "Send message")
        client?.send("Hello from client")
    }

    override fun onDestroy() {
        super.onDestroy()
        client?.close()
        server?.stop()
    }
}

This is the log:

2020-03-31 16:16:22.701 D/SERVER: Ready to start
2020-03-31 16:16:26.429 D/CLIENT: Ready to start
2020-03-31 16:16:26.547 D/SERVER: New client
2020-03-31 16:16:26.547 D/SERVER: Send message
2020-03-31 16:16:26.550 D/CLIENT: Connected to server
2020-03-31 16:16:26.551 D/CLIENT: Message: Hello from server
2020-03-31 16:16:31.970 D/CLIENT: Send message
2020-03-31 16:16:31.974 D/SERVER: Closed socket: null
2020-03-31 16:16:31.975 D/CLIENT: Closed socket: null

What i am doing wrong?

hugebear commented 3 years ago

I meet the same problem, and it is fine in version 2.+