该类提供PUSH_AND_PULL模式下的couchbase lite数据库和复制器,以从sync_gateway发送和接收数据
class CouchBaseLiteManager(private val context: Context) {
private val NAME_DB = "db"
private val REMOTE_URI = "ws://localhost:4984/db"
private val user = "..."
private val password = "..."
private lateinit var configuration: DatabaseConfiguration
private lateinit var mDatabase: Database
private lateinit var mReplicator: Replicator
private lateinit var mReplicatorPush: Replicator
private lateinit var mReplicatorPull: Replicator
init {
instantiate()
createDatabase()
instantiateReplicator()
instantiateReplicatorPush()
instantiateReplicatorPull()
}
fun getDatabase(): Database = this.mDatabase!!
fun getDatabaseName(): String = this.NAME_DB
fun getReplicatorPush(): Replicator = this.mReplicatorPush
private fun instantiate() {
// Initialize the Couchbase Lite system
CouchbaseLite.init(context)
}
private fun createDatabase(){
// Get the database (and create it if it doesn’t exist).
configuration = DatabaseConfiguration()
mDatabase = Database(NAME_DB, configuration)
}
private fun instantiateReplicatorPush() {
var uri:URI ? = null
try {
uri = URI(REMOTE_URI)
}catch (c: CouchbaseLiteException) {
c.printStackTrace()
}
uri?.let {
val endpoint = URLEndpoint(it)
val config = ReplicatorConfiguration(mDatabase,endpoint)
config.replicatorType = CustomReplicatorType.getPushAndPull()
config.isContinuous = true
config.authenticator = BasicAuthenticator(user,password)
//send to remote Endpoint
config.pushFilter = ReplicationFilter { document, flags ->
true
}
config.setPullFilter { document, flags ->
true
}
mReplicatorPush = Replicator(config)
}
}
}
在这部分代码中,我启动了复制器,但出现了错误:{Repl#16}得到了LiteCore错误:WebSocket错误1001“ WebSocket连接被同级关闭”
fun test_couchbase_lite() {
val manager = CouchBaseLiteManager(context)
val db = manager.getDatabase()
Injection.getReplicatorPush().start()
Injection.getReplicatorPush().addChangeListener(object: ReplicatorChangeListener {
override fun changed(change: ReplicatorChange) {
Log.d("debug","replicator push and pull change !!!!! *")
Log.d("debug","${change.status} -")
if (change.replicator.status.activityLevel == CustomReplicatorType.getIDLE()) {
Log.d("debug", "Scheduler Completed");
}
if (change.replicator.status.activityLevel == CustomReplicatorType.getStopped()
|| change.replicator.status.activityLevel == CustomReplicatorType.getOffline()) {
Log.d("debug", "ReplicationTag Stopped");
}
}
})
}
怎么了?否则我错过了什么!仅供参考:我使用社区版谢谢!