Kotlin开发Solana移动端应用遇WebSocket连接失败求助
Solana Kotlin应用WebSocket连接失败问题
我正在开发一款与Solana区块链交互的Kotlin应用,已遵循官方指南,但仍遇到问题。每次点击按钮触发onBuyTicket函数时,代码执行到val txResult = walletAdapter.transact(sender) { authResult ->这一行就报错,错误信息为:WebSockets error com.neovisionaries.ws.client.WebSocketException: Failed to connect。我怀疑问题和Solana本身无关,但缺乏WebSocket相关知识,找不到修复方案。
相关代码
suspend fun onBuyTicket(ticket: Ticket, sender: ActivityResultSender) { val programId = SolanaPublicKey.from("FtyTxEhxtMcrzwag97cfabBCLzfdi5K6dhc81ybLqNqh") // Log program ID Log.d("Juraj", "Program ID: $programId") // Counter account has a single seed 'counter' val seeds = listOf("ticket".encodeToByteArray()) Log.d("Juraj", "Seeds: $seeds") // Calculate the PDA val result = ProgramDerivedAddress.find(seeds, programId) val counterAccountPDA = result.getOrNull() Log.d("Juraj", "Calculated counterAccountPDA: $counterAccountPDA") // Check if PDA was successfully calculated if (counterAccountPDA == null) { Log.e("Juraj", "Failed to calculate PDA") return } // Unwrap the result val rpcClient = SolanaRpcClient("https://api.devnet.solana.com", KtorNetworkDriver()) val solanaUri = Uri.parse("https://jurajapp.com") // Placeholder or generic URL val iconUri = Uri.parse("https://example.com/icon.png") // Placeholder or generic icon val identityName = "My dApp" // The name of your dApp Log.d("Juraj", "Setting up wallet adapter with URI: $solanaUri, icon: $iconUri, name: $identityName") val walletAdapter = MobileWalletAdapter(connectionIdentity = ConnectionIdentity( identityUri = solanaUri, iconUri = iconUri, identityName = identityName )) val encodedInstructionData = Borsh.encodeToByteArray( AnchorInstructionSerializer("create_ticket"), Args_ticket(ticket.id, ticket.name, ticket.departureTime, ticket.arrivalTime, ticket.price) ) Log.d("Juraj", "Encoded instruction data: ${encodedInstructionData.joinToString(",") { "%02x".format(it) }}") val ticketInstruction = TransactionInstruction( SolanaPublicKey.from("FtyTxEhxtMcrzwag97cfabBCLzfdi5K6dhc81ybLqNqh"), listOf(AccountMeta(counterAccountPDA, false, true)), encodedInstructionData ) Log.d("Juraj", "Ticket instruction: $ticketInstruction") val blockhashResponse = rpcClient.getLatestBlockhash() Log.d("Juraj", "Blockhash response: ${blockhashResponse.result?.blockhash}") val buyTicketMessage = Message.Builder() .addInstruction(ticketInstruction) .setRecentBlockhash(blockhashResponse.result!!.blockhash) .build() Log.d("Juraj", "Buy ticket message built") val unsignedIncrementTx = Transaction(buyTicketMessage) Log.d("Juraj", "Unsigned transaction: ${unsignedIncrementTx.serialize().joinToString(",") { "%02x".format(it) }}") val txResult = walletAdapter.transact(sender) { authResult -> Log.d("Juraj", "Transaction request made") val account = SolanaPublicKey(authResult.accounts.first().publicKey) Log.d("Juraj", "Signing and sending transaction for account: $account") signAndSendTransactions(arrayOf(unsignedIncrementTx.serialize())) } when (txResult) { is TransactionResult.Success -> { Log.d("Juraj", "Transaction successful. Signature:") } is TransactionResult.NoWalletFound -> { Log.e("Juraj", "No wallet found") println("No MWA compatible wallet app found on device.") } is TransactionResult.Failure -> { Log.e("Juraj", "Transaction failed. Error: ${txResult.e.message}") println("Error during signing and sending transactions: ${txResult.e.message}") } }
相关日志
- 应用提示选择钱包应用时的日志
- 选择Phantom或Solflare钱包时出现的错误日志
内容的提问来源于stack exchange,提问作者JurajMarincic
相关产品推荐
相关产品推荐

