客户端无法与StreamIO连接



我正在学习流聊天的最新教程,它看起来很棒。

看起来我按照它的字母,除了我用一个为我创建的仪表板替换了apiKey。这是我注册免费试用版时提供的。

不幸的是,我无法连接。

这是我的代码

SwiftUIChatDemo

import SwiftUI
// 1 Add imports
import StreamChat
import StreamChatSwiftUI
@main
struct SwiftUIChatDemoApp: App {

// 2 Add Adapter
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ChatChannelListView()
}
}
}

应用程序委托

import StreamChat
import StreamChatSwiftUI
import UIKit
import SwiftUI
class AppDelegate: NSObject, UIApplicationDelegate {

// Add context provider

var streamChat: StreamChat?

var chatClient: ChatClient = {
// Low-level client variable with a hard-coded apikey
var config = ChatClientConfig(apiKey: .init("[key I created in dashboard]"))

// Set to use the chat in offline mode
config.isLocalStorageEnabled = true

// Pass the low-level client variable as a parameter of the ChatClient
let client = ChatClient(config: config)
return client
}()

func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions:
[UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
// Initialize the stream chat client
streamChat = StreamChat(chatClient: chatClient)

connectUser()

return true
}

// The `connectUser` function we need to add.
private func connectUser() {
// This is a hardcoded token valid on Stream's tutorial environment.
let token = try! Token(rawValue: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoibHVrZV9za3l3YWxrZXIifQ.kFSLHRB5X62t0Zlc7nwczWUfsQMwfkpylC6jCUZ6Mc0")

// Call `connectUser` on our SDK to get started.
chatClient.connectUser(
userInfo: .init(id: "luke_skywalker",
name: "Luke Skywalker",
imageURL: URL(string: "https://vignette.wikia.nocookie.net/starwars/images/2/20/LukeTLJ.jpg")!),
token: token
) { error in
if let error = error {
// Some very basic error handling only logging the error.
log.error("connecting the user failed (error)")
return
}
}
}
}

SwiftUIChatDemo.app

import SwiftUI
// 1 Add imports
import StreamChat
import StreamChatSwiftUI
@main
struct SwiftUIChatDemoApp: App {

// 2 Add Adapter
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ChatChannelListView()
}
}
}

当您更改apiKey时,您将需要使用仪表板中提供给您的密钥生成新的令牌。对于开发,您可以使用下面的工具生成令牌:https://getstream.io/chat/docs/ios-swift/tokens_and_authentication/?language=swift#manually-generating-tokens。用户id还需要与您的用户匹配。注意,对于生产使用,您应该在后端生成这些令牌。如果有其他问题,您也可以访问我们的支持渠道:https://getstream.io/contact/support/

UPDATE

所以,我从教程中得到了apiKeytoken值。做得很好。如何处理我自己的值可能需要我自己的用户。

最新更新