当我尝试使用 Couchbase lite 2.7 的 Replicator Android 时出现错误 "WebSocket connection closed by peer"
Getting error "WebSocket connection closed by peer" when I try Replicator with Couchbase lite 2.7 Android
此 class 提供 couchbase lite 数据库和模式 PUSH_AND_PULL 的复制器以发送和接收来自 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 connection closed by peer"
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");
}
}
})
}
怎么了?或者我错过了什么!
仅供参考:我使用社区版
谢谢!
要解决此问题,我必须获取 IP 地址并将其放入 xml 文件网络配置中以管理明文支持。
根据https://developer.android.com/training/articles/security-config#CleartextTrafficPermitted
此 class 提供 couchbase lite 数据库和模式 PUSH_AND_PULL 的复制器以发送和接收来自 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 connection closed by peer"
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");
}
}
})
}
怎么了?或者我错过了什么! 仅供参考:我使用社区版 谢谢!
要解决此问题,我必须获取 IP 地址并将其放入 xml 文件网络配置中以管理明文支持。
根据https://developer.android.com/training/articles/security-config#CleartextTrafficPermitted