这是我第一次使用套接字。我正在创建一个聊天功能,可将我的应用程序连接到服务器。我尝试了几种方法,但没有任何效果。我将我的应用程序成功连接到套接字/ Websocket,但发出但未从服务器获取返回数据。
let SocketURL:String = "https://apps.abc.com:8080"
let WSSSocketURL:String = "wss://apps.abc.com:8080"
var conversationID:Int = 0
var socket:SocketIOClient?
var socketManager:SocketManager?
var otherChatUser:ChatUser = ChatUser()
var messageList:[ChatMessage] = [ChatMessage]()
var NumberOfSection:Int = 2
func setupSocket() -> Void {
socketManager = SocketManager(socketURL: URL(string: SocketURL)!, config: [.log(true), .compress])
self.socket = socketManager?.defaultSocket
socketManager?.config = SocketIOClientConfiguration(
arrayLiteral: .connectParams(["token": KeychainService.getWssToken() as Any, "EIO" : 3]),
.path("/wss"))
self.socket!.connect()
self.socket?.on(clientEvent: .connect) {
data, ack in
self.con_create()
}
}
func con_create() -> Void {
self.socketManager?.engine?.ws?.connect()
self.socketManager?.engine?.ws?.onConnect = {
let JsonData = [ "user" : self.otherChatUser.AuthID ]
self.socket?.emit("con_create", JsonData)
// expect to get back the conversation ID from the server after socket.on
self.socket?.on("con_create") {
(data, ack) in
print("con_create")
let json = data[1] as? JSON
self.conversationID = json!["id"].intValue
}
}
}
self.conversationID应该是16或15这样的整数。