iOS集成GoEasy Websocket
集成iOS SDK
CocoaPods
pod 'GoEasySDK', :git => 'https://gitee.com/goeasy-io/goeasy-swift.git', :tag => '0.1.3'
Swift Package Manager
.package(url: "https://gitee.com/goeasy-io/goeasy-swift.git", exact: "0.1.3")
Demo源码下载
初始化GoEasy
import UIKit
import GoEasySDK
class AppDelegate: NSObject, UIApplicationDelegate {
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
) -> Bool {
//初始化GoEasy,新加坡host:singapore.goeasy.io
GoEasy.initSDK(host: "hangzhou.goeasy.io", appkey: "您的appkey")
return true
}
}
建立Websocket连接
// 连接
try await GoEasy.connect()
订阅(接收)消息
try await GoEasy.ws.subscribe(
channels: ["demo_channel"],
onMessage: { message in
print("收到消息: \(message.content),channel: \(message.channel)")
}
)
发送消息
let options = PublishOptions(
channel: "demo_channel",
content: "Hello, world!"
)
try await GoEasy.ws.publish(options)
取消订阅
try await GoEasy.ws.unsubscribe("demo_channel")
断开连接
try await GoEasy.disconnect()
GoEasy连接状态
获取当前连接状态
let state = GoEasy.state;
print("当前连接状态: \(state)")
监听连接状态变化
//监听
GoEasy.onStateChange { event in
print("当前状态: \(event.current), 尝试连接次数: \(event.attempts), 错误: \(event.error.map { "[\($0.code)] \($0.message)" } ?? "无")")
}
//取消监听
GoEasy.onStateChange(nil)


