Guest User

Untitled

a guest
Mar 28th, 2018
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.15 KB | None | 0 0
  1. ackage com.superbet.sportsbook
  2.  
  3. import android.os.Bundle
  4. import android.support.v7.app.AppCompatActivity
  5. import com.superbet.mqtt_client.SuperbetMqttConnectOptions
  6.  
  7. class MainActivity : AppCompatActivity() {
  8.  
  9.     override fun onCreate(savedInstanceState: Bundle?) {
  10.         super.onCreate(savedInstanceState)
  11.         setContentView(R.layout.activity_main)
  12.  
  13.         val serverUri = "ws://oppy-prod.dev.superbet.com:80/ws/";
  14.  
  15.  
  16.         val connectionOptions = SuperbetMqttConnectOptions(clientId = "androidId21321321", serverUri = serverUri)
  17.  
  18.  
  19.     }
  20. }
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27. package com.superbet.mqtt_client
  28.  
  29. import org.eclipse.paho.client.mqttv3.MqttClient
  30.  
  31.  
  32. /**
  33.  *
  34.  */
  35. class SuperbetMqttClient(url: String, port: Int) {
  36.  
  37.     var mqttClient: MqttClient
  38.  
  39.     init {
  40.         mqttClient = MqttClient(url, url).apply {
  41.             connect()
  42.         }
  43.         with(mqttClient) {
  44.             this.connect()
  45.         }
  46.  
  47.         mqttClient.let {
  48.  
  49.         }
  50.     }
  51.  
  52.     fun connect(connectOptions: SuperbetMqttConnectOptions) {
  53.         mqttClient.connect(connectOptions.toDefaultMqttConnectionOptions())
  54.     }
  55. }
  56.  
  57.  
  58.  
  59.  
  60.  
  61. package com.superbet.mqtt_client
  62.  
  63. import org.eclipse.paho.client.mqttv3.MqttConnectOptions
  64.  
  65. /**
  66.  * Connection parameters for establishing connection via [SuperbetMqttClient]. It contains all
  67.  * available connection settings, url and port parameters, its different from [MqttConnectOptions]
  68.  * since it has more options unified under the same model.
  69.  *
  70.  * @param autoReconnect - auto reconnection after connection is dropped due to some error.
  71.  * @param cleanSession - whether the client and server should remember state for the client across reconnects.
  72.  * @param mqttVersion - the version of the MQTT protocol.
  73.  * @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.
  74.  * @param serverUri - where to connect with port specified, example: ws://oppy-prod.dev.superbet.com:80/ws/.
  75.  * @param userrname - if authenticated session required, specify username as plain text.
  76.  * @param password - if authenticated session required, specify username as char array.
  77.  *
  78.  */
  79. class SuperbetMqttConnectOptions(
  80.         val autoReconnect: Boolean = true,
  81.         val cleanSession: Boolean = true,
  82.         val mqttVersion: Int = 4,
  83.         val clientId: String,
  84.         val serverUri: String
  85. ) {
  86.  
  87.     var username: String? = null
  88.     var password: String? = null
  89.  
  90.     constructor(username: String?,
  91.                 password: String?,
  92.                 clientId: String,
  93.                 serverUri: String) : this(clientId = clientId, serverUri = serverUri) {
  94.         this.username = username;
  95.         this.password = password;
  96.     }
  97.  
  98.  
  99.     /**
  100.      * Method used to convert [SuperbetMqttConnectOptions] to model used by mqtt libary.
  101.      */
  102.     fun toDefaultMqttConnectionOptions() = MqttConnectOptions().apply {
  103.         isAutomaticReconnect = true
  104.         isCleanSession = true
  105.         mqttVersion = 4;
  106.         password = this@SuperbetMqttConnectOptions.password?.toCharArray()
  107.         userName = this@SuperbetMqttConnectOptions.username
  108.     }
  109. }
Add Comment
Please, Sign In to add comment