我使用socket.io
框架进行swift。我试图通过websockets API连接,我有一个方法,在viewDidLoad
的{{1}}中调用。我已经全局声明了我的经理和socketClient,我将为下面的方法添加代码:
ViewController
我面临的问题是private func setupSockets() {
manager = SocketManager(socketURL: URL(string: "http://kaboom.rksv.net")!, config: [.log(true), .compress])
socket = SocketIOClient(manager: manager, nsp: "/watch")//manager.defaultSocket
socket.onAny {print("Got event: \($0.event), with items: \($0.items)")}
socket.on("data", callback: { (data, ack) in
print(data)
})
socket.on("error", callback: { (data, ack) in
print(data)
print(ack)
})
socket.on("connect", callback: { (data, ack) in
print(data)
print(ack)
self.socket.emit("ping", [])
})
socket.connect()
}
回调没有被调用,因此,我无法发出ping消息。这是日志:
2018-02-18 19:02:20.589406 + 0530 stocks-cake [10965:3662189] LOG SocketIOClient {/ watch}:处理事件:statusChange包含数据:[连接]
2018-02-18 19:02:20.589692 + 0530 stocks-cake [10965:3662189] LOG SocketIOClient {/ watch}:加入名称/监视
2018-02-18 19:02:20.589850 + 0530 stocks-cake [10965:3662189] LOG SocketManager:当发动机未打开时尝试连接插座。连接
2018-02-18 19:02:20.589916 + 0530 stocks-cake [10965:3662189] LOG SocketManager:添加引擎
2018-02-18 19:02:32.986354 + 0530 stocks-cake [10965:3662271] LOG SocketEngine:启动发动机。服务器:http://kaboom.rksv.net
2018-02-18 19:02:32.988480 + 0530 stocks-cake [10965:3662271] LOG SocketEngine:Handshaking
2018-02-18 19:02:32.995078 + 0530 stocks-cake [10965:3662271] LOG SocketEnginePolling:正在进行投票获取http://kaboom.rksv.net/socket.io/?transport=polling&b64=1
2018-02-18 19:02:33.197687 + 0530 stocks-cake [10965:3662406] LOG SocketEnginePolling:得到民意调查回复
2018-02-18 19:02:33.200976 + 0530 stocks-cake [10965:3662406] LOG SocketEnginePolling:得到民意调查信息:97:0 {" sid":" 4Uf21Mr7_9DGjSXcAASj&#34 ;,"升级":["网页套接字&#34]," pingInterval":25000" pingTimeout":60000}
2018-02-18 19:02:33.208867 + 0530 stocks-cake [10965:3662406] LOG SocketEngine:得到消息:0 {" sid":" 4Uf21Mr7_9DGjSXcAASj",& #34;升级":["网页套接字&#34]," pingInterval":25000" pingTimeout":60000}
2018-02-18 19:02:33.226824 + 0530 stocks-cake [10965:3662189] LOG SocketManager:引擎打开Connect
2018-02-18 19:02:33.226894 + 0530 stocks-cake [10965:3662406] LOG SocketEnginePolling:进行投票获取http://kaboom.rksv.net/socket.io/?transport=polling&b64=1&sid=4Uf21Mr7_9DGjSXcAASj
2018-02-18 19:02:33.227367 + 0530 stocks-cake [10965:3662406] LOG SocketEngine:撰写民意调查:有数据:假
2018-02-18 19:02:33.227447 + 0530 stocks-cake [10965:3662406] LOG SocketEnginePolling:发送民意调查:类型:2
2018-02-18 19:02:33.230935 + 0530 stocks-cake [10965:3662406] LOG SocketEnginePolling:创建POST字符串:1:2
2018-02-18 19:02:33.231392 + 0530 stocks-cake [10965:3662406] LOG SocketEnginePolling:POSTing
2018-02-18 19:02:33.231510 + 0530 stocks-cake [10965:3662406] LOG SocketEnginePolling:进行轮询POST http://kaboom.rksv.net/socket.io/?transport=polling&b64=1&sid=4Uf21Mr7_9DGjSXcAASj
2018-02-18 19:02:33.280857 + 0530 stocks-cake [10965:3662406] LOG SocketEnginePolling:得到民意调查回复
2018-02-18 19:02:33.281066 + 0530 stocks-cake [10965:3662406] LOG SocketEnginePolling:得到民意调查信息:2:40
2018-02-18 19:02:33.281933 + 0530 stocks-cake [10965:3662406] LOG SocketEngine:得到的消息:40
2018-02-18 19:02:33.282346 + 0530 stocks-cake [10965:3662189] LOG SocketParser:Parsing 0
2018-02-18 19:02:33.283167 + 0530 stocks-cake [10965:3662189] LOG SocketParser:解码后的数据包为:SocketPacket {type:0;数据:[]; id:-1;占位符:0; nsp:/}
2018-02-18 19:02:33.283195 + 0530 stocks-cake [10965:3662406] LOG SocketEnginePolling:进行投票获取http://kaboom.rksv.net/socket.io/?transport=polling&b64=1&sid=4Uf21Mr7_9DGjSXcAASj
2018-02-18 19:02:33.330123 + 0530 stocks-cake [10965:3662271] LOG SocketEngineWebSocket:发送ws:probe作为类型:2
2018-02-18 19:02:33.340044 + 0530 stocks-cake [10965:3662407] LOG SocketEnginePolling:得到了投票回复
2018-02-18 19:02:33.340351 + 0530 stocks-cake [10965:3662407] LOG SocketEnginePolling:得到民意调查信息:1:3
2018-02-18 19:02:33.340621 + 0530 stocks-cake [10965:3662407] LOG SocketEngine:得到消息:3
2018-02-18 19:02:33.341829 + 0530 stocks-cake [10965:3662407] LOG SocketEnginePolling:进行投票获取http://kaboom.rksv.net/socket.io/?transport=polling&b64=1&sid=4Uf21Mr7_9DGjSXcAASj
2018-02-18 19:02:33.378473 + 0530 stocks-cake [10965:3662407] LOG SocketEngine:得到消息:3probe
2018-02-18 19:02:33.379059 + 0530 stocks-cake [10965:3662407] LOG SocketEngine:收到探测响应,应该升级到WebSockets
2018-02-18 19:02:33.379252 + 0530 stocks-cake [10965:3662407] LOG SocketEngine:升级到WebSockets的传输
2018-02-18 19:02:33.379368 + 0530 stocks-cake [10965:3662407] LOG SocketEnginePolling:发送民意调查:类型:6
2018-02-18 19:02:33.379511 + 0530 stocks-cake [10965:3662407] LOG SocketEnginePolling:创建的POST字符串:1:6
2018-02-18 19:02:33.379968 + 0530 stocks-cake [10965:3662407] LOG SocketEnginePolling:POSTing
2018-02-18 19:02:33.477785 + 0530 stocks-cake [10965:3662407] LOG SocketEnginePolling:得到民意调查回复
2018-02-18 19:02:33.478016 + 0530 stocks-cake [10965:3662407] LOG SocketEnginePolling:得到民意调查信息:1:6
2018-02-18 19:02:33.478639 + 0530 stocks-cake [10965:3662407] LOG SocketEngine:得到消息:6
2018-02-18 19:02:33.479275 + 0530 stocks-cake [10965:3662407] LOG SocketEngine:切换到WebSockets
2018-02-18 19:02:33.479594 + 0530 stocks-cake [10965:3662407] LOG SocketEngineWebSocket:发送ws:类型:5
2018-02-18 19:02:33.480317 + 0530 stocks-cake [10965:3662407] LOG SocketEngine:Flushing probe wait
2018-02-18 19:02:59.288327 + 0530 stocks-cake [10965:3662407] LOG SocketEngine:写ws:有数据:false
2018-02-18 19:02:59.288576 + 0530 stocks-cake [10965:3662407] LOG SocketEngineWebSocket:发送ws:类型:2
2018-02-18 19:02:59.338827 + 0530 stocks-cake [10965:3662407] LOG SocketEngine:得到消息:3
2018-02-18 19:03:26.788673 + 0530 stocks-cake [10965:3662679] LOG SocketEngine:写ws:有数据:false
我有什么遗漏的吗?做错了什么?提前谢谢。
答案 0 :(得分:0)
声明应如下所示,您忘记设置端口
let manager = SocketManager(socketURL: URL(string: "http://localhost:8080")!, config: [.log(true), .compress])
let socket = manager.defaultSocket
答案 1 :(得分:0)
使用如下所示的命名空间:
let socket = socketManager.socket(forNamespace: "/watch")