You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ObjectBox Android客户端无法连接Docker部署的同步服务器问题求助

ObjectBox 同步连接报错问题及解决方案

报错信息

W/Box: [Cl-Lws] LWS: Unable to open socket [Cl-Lws] LWS client connection error: unable to open socket

问题描述

尝试将Android应用与ObjectBox同步服务器做数据同步时,客户端无法连接服务器。已确认Admin UI可以通过localhost:9980正常访问,服务器部署在Docker环境中。

服务器启动日志如下:

001-13:29:41.2231 [INFO ] [SvrUws] UwsServer listening on all interfaces, port 9999 without SSL
001-13:29:41.2234 [WARN ] [SvSync] UNSECURED no-authentication mode enabled - every client allowed without any validation
001-13:29:41.2256 [INFO ] [SvSync] Started on port 9999
001-13:29:41.2257 [INFO ] [SySvAp] Starting object browser on 0.0.0.0:9980
001-13:29:41.2263 [INFO ] [HttpSv] Running in single-store mode with an already opened store
001-13:29:41.2263 [INFO ] [HttpSv] Listening on 0.0.0.0:9980
001-13:29:41.2266 [WARN ] [HttpSv] UNSECURED no-authentication mode enabled:
!!! Allowing access to anyone without any user validation
!!! After e.g. adjusting user logins, please restart asap to secure the server again
001-13:29:41.2266 [INFO ] [HttpSv] User management: enabled
001-13:29:41.2305 [INFO ] [HttpSv] HttpServer listening on all interfaces, port 9980
001-13:29:41.2318 [INFO ] [SySvAp] ObjectBox sync server started in 62 ms

运行状态截图

排查过程

最初按照文档尝试使用wss://127.0.0.1连接,之后切换为ws协议,分别测试了9980、9999端口,还修改了服务配置文件,内容如下:

{
    "dbDirectory": "objectbox",
    "bind": "ws://0.0.0.0:9999",
    "browserBind": "http://127.0.0.1:9980",
    "browserThreads": 4,
    "certificatePath": ""
}

以上操作均未解决连接问题。

最终解决方案

Android模拟器访问宿主机本地服务时,不能直接使用127.0.0.1(该地址指向模拟器自身),需要使用Android模拟器预留的宿主机映射地址10.0.2.2。

在MainActivity的onCreate()方法中修改同步客户端初始化代码即可:

BoxStore boxStore = ObjectBox.get();

SyncClient syncClient = Sync.client(
        boxStore,
        "ws://10.0.2.2", 
        SyncCredentials.none()
).buildAndStart();

解决后运行截图

内容的提问来源于stack exchange,提问作者Menci

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 04:18:04