Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ackage com.superbet.sportsbook
- import android.os.Bundle
- import android.support.v7.app.AppCompatActivity
- import com.superbet.mqtt_client.SuperbetMqttConnectOptions
- class MainActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- val serverUri = "ws://oppy-prod.dev.superbet.com:80/ws/";
- val connectionOptions = SuperbetMqttConnectOptions(clientId = "androidId21321321", serverUri = serverUri)
- }
- }
- package com.superbet.mqtt_client
- import org.eclipse.paho.client.mqttv3.MqttClient
- /**
- *
- */
- class SuperbetMqttClient(url: String, port: Int) {
- var mqttClient: MqttClient
- init {
- mqttClient = MqttClient(url, url).apply {
- connect()
- }
- with(mqttClient) {
- this.connect()
- }
- mqttClient.let {
- }
- }
- fun connect(connectOptions: SuperbetMqttConnectOptions) {
- mqttClient.connect(connectOptions.toDefaultMqttConnectionOptions())
- }
- }
- package com.superbet.mqtt_client
- import org.eclipse.paho.client.mqttv3.MqttConnectOptions
- /**
- * Connection parameters for establishing connection via [SuperbetMqttClient]. It contains all
- * available connection settings, url and port parameters, its different from [MqttConnectOptions]
- * since it has more options unified under the same model.
- *
- * @param autoReconnect - auto reconnection after connection is dropped due to some error.
- * @param cleanSession - whether the client and server should remember state for the client across reconnects.
- * @param mqttVersion - the version of the MQTT protocol.
- * @param clientId - A client identifier must be specified and be less that 65535 characters. It must be unique across all clients connecting to the same server.
- * @param serverUri - where to connect with port specified, example: ws://oppy-prod.dev.superbet.com:80/ws/.
- * @param userrname - if authenticated session required, specify username as plain text.
- * @param password - if authenticated session required, specify username as char array.
- *
- */
- class SuperbetMqttConnectOptions(
- val autoReconnect: Boolean = true,
- val cleanSession: Boolean = true,
- val mqttVersion: Int = 4,
- val clientId: String,
- val serverUri: String
- ) {
- var username: String? = null
- var password: String? = null
- constructor(username: String?,
- password: String?,
- clientId: String,
- serverUri: String) : this(clientId = clientId, serverUri = serverUri) {
- this.username = username;
- this.password = password;
- }
- /**
- * Method used to convert [SuperbetMqttConnectOptions] to model used by mqtt libary.
- */
- fun toDefaultMqttConnectionOptions() = MqttConnectOptions().apply {
- isAutomaticReconnect = true
- isCleanSession = true
- mqttVersion = 4;
- password = this@SuperbetMqttConnectOptions.password?.toCharArray()
- userName = this@SuperbetMqttConnectOptions.username
- }
- }
Add Comment
Please, Sign In to add comment