Tuesday 22 September 2020

Connect wamp server in andorid

I'm trying to connect with the WAMPServer or want to subscribe to the channel in android. not connecting in android but working on the web browser.

I used this library https://github.com/crossbario/autobahn-java

class AutoBhanActivity : AppCompatActivity() {

lateinit var dataObject:JSONObject
lateinit var rootObject:JSONObject
lateinit var rootObjectDriver:JSONObject
 val key="driver106apikey"


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_auto_bhan)

    dataObject=JSONObject()

    dataObject.put("lat", 123.2123)
    dataObject.put("lng", 22.5543)
    dataObject.put("status", 1)

    rootObject=JSONObject()
    rootObject.put("event", "0")
    rootObject.put("data", dataObject)

    rootObjectDriver=JSONObject()
    rootObjectDriver.put(key, rootObject)
    Log.i("OBJECT----", rootObjectDriver.toString())

    tvText.setOnClickListener {

        val key="driver106apikey"
        val session = Session()
        session.addOnJoinListener(this::demonstrateSubscribe)
        session.addOnJoinListener(this::demonstratePublish)
       // session.addOnJoinListener(this::demonstrateCall)
         //  session.addOnJoinListener(this::demonstrateRegister)
        val client = Client( "wss://dev.ctlf.co.uk/devcall")
        val exitInfoCompletableFuture: CompletableFuture<ExitInfo> = client.connect()
        Log.i("OBJECT----", exitInfoCompletableFuture.get().toString())
       // startSocket()
    }
}


fun startSocket() {
    val connection = WebSocketConnection()
    connection.connect("wss://dev.ctlf.co.uk/devcall", object : WebSocketConnectionHandler() {
        override fun onConnect(response: ConnectionResponse) {
            println("Connected to server------")
        }

        override fun onOpen() {
            connection.sendMessage("Echo with Autobah----n")
        }

        override fun onClose(code: Int, reason: String) {
            println("Connection closed------")
        }

        override fun onMessage(payload: String) {
            println("Received message--------: $payload")
            connection.sendMessage(payload)
        }
    })
}

fun demonstrateSubscribe(
    session: Session,
    details: SessionDetails?
) {
    val subFuture = session.subscribe(key, ::onEvent)
    subFuture.whenComplete { subscription: Subscription, throwable: Throwable? ->
        if (throwable == null) {
            // We have successfully subscribed.
            println("Subscribed to topic----- " + subscription.topic)
        } else {
            // Something went bad.
            println("Subscribed to topic----- " + throwable)
            throwable.printStackTrace()
        }
    }
}

fun demonstrateCall(
    session: Session,
    details: SessionDetails?
) {

    // Call a remote procedure.
    val callFuture =
        session.call("wss://dev.ctlf.co.uk/devcall", 10, 20)
    callFuture.thenAccept { callResult: CallResult ->
        println(
            String.format("Call result: %s-------", callResult.results[0])
        )
    }
}


fun demonstratePublish(
    session: Session,
    details: SessionDetails?
) {

    val pubFuture = session.publish(key, rootObject)
}

private fun onEvent(
    args: List<Any>,
    kwargs: Map<String, Any>,
    details: EventDetails
) {
    println(String.format("Got event: %s", args[0]))
}

fun demonstrateRegister(
    session: Session,
    details: SessionDetails?
) {

    // Register a procedure.
    val regFuture =
        session.register("com.myapp.add2", this::add2)
    regFuture.thenAccept { registration: Registration ->
        println(
            "Successfully registered procedure: " + registration.procedure
        )
    }
}

private fun add2(
    args: List<Any>,
    kwargs: Map<String, Any>,
    details: InvocationDetails
): CompletableFuture<InvocationResult>? {
    val res = args[0] as Int + args[1] as Int
    val arr: MutableList<Any> = ArrayList()
    arr.add(res)
    return CompletableFuture.completedFuture(
        InvocationResult(
            arr
        )
    )
}
}

No Error showing in android

this is how data is getting sent through the browser.

enter image description here

autobahn.js socket.htlm



from Connect wamp server in andorid

No comments:

Post a Comment