Swift-SocketIO 聊天室
2022-04-20 本文已影响0人
9岁就很6
集成pod
# scoket连接框架
pod 'Socket.IO-Client-Swift', '15.2.0'
# 异步编程框架
pod 'PromiseKit'
# 模型转换框架
pod 'KakaJSON', '1.1.2'
private var manager:SocketManager?
private var socket:SocketIOClient?
// [] 里面传递需要的字段 ,比如版本号 [ "h": HttpRequestUtil.share.platformVersion]
private var socketHeaders : [String:String] = [:]
private var config:SocketIOClientConfiguration = []
/// 连接socket
public func connect(){
if manager != nil {
//断开连接
disconnect()
}
socketHeaders["t"] = "用户token"
socketHeaders["isEnabled"] = "false"
config = [
.log(true),//打印日志
.compress,
.extraHeaders(socketHeaders),
.reconnects(true), //是否重连机制
.reconnectWaitMax(7) //重新连接等待最大秒数
]
manager = SocketManager(socketURL: WSURL!, config: config)
socket = manager?.defaultSocket
listeningStatus()
listeningMsgEvent()
socket?.connect()
}
具体实现请看demo
https://github.com/SwiftHsw/SocketIO