The rawhttp-duplex module can be used to create a duplex communication channel as either a client or a server.
The entry point of the library is the com.athaydes.rawhttp.duplex.RawHttpDuplex class.
Its connect methods are used from a client to connect to a server,
while the accept methods should be used within a HTTP server to handle requests from a client.
Example Kotlin code on the server:
import rawhttp.core.*
import com.athaydes.rawhttp.duplex.*
import rawhttp.core.server.TcpRawHttpServer;
val http = RawHttp()
val duplex = RawHttpDuplex()
val server = TcpRawHttpServer(8082)
server.start { request ->
// TODO check the request is a POST to the /connect path!
// call duplex.accept() to return a response that can initiate duplex communication
Optional.of(duplex.accept(request, { sender ->
object : MessageHandler {
override fun onTextMessage(message: String) {
// handle text message
sender.sendTextMessage(“Hi there! You sent this: $message”)
}
override fun onBinaryMessage(message: ByteArray, headers: RawHttpHeaders) { /* handle binary message */ }
override fun onClose() { /* handle closed connection */ }
}
Original URL: http://feedproxy.google.com/~r/feedsapi/BwPx/~3/lqpnXAkXUDA/