Socket.io 与 Swift 不是握手
Socket.io with Swift is not handshaking
我正在尝试将 iOS 应用与 Node.js WebSocket 服务器集成。
我为 iOS 使用 SwiftUI,为服务器和客户端使用 socket.io。
但是当我尝试将 iOS 应用程序与服务器连接时,
好像连不上服务器
我通过将 React 应用程序连接到 Node.js 服务器来测试服务器是否 运行
并且连接在浏览器上工作正常。
我检查了 swift 日志,它似乎在尝试与服务器握手,但是,
它开始长轮询,所以我猜握手过程有一些问题。
如果有新连接,我正在控制台将“新连接”作为字符串记录在 Node.js 服务器中,它对 React 工作正常,但对 Swift。
但我没有提前尝试任何事情,所以我无法找出问题所在。
有人可以帮我吗?
import SwiftUI
import SocketIO
final class Service: ObservableObject {
private var manager = SocketManager(socketURL:URL(string: "http://localhost:5000")!, config: [.log(true), .compress])
init() {
let socket = manager.defaultSocket
socket.on(clientEvent: .connect) { (data,act) in
print("Connected")
}
socket.connect()
}
}
struct SocketIO: View {
@ObservedObject var service = Service()
var body: some View {
Text("Hello, Web socket!")
}
}
这是我从 SocketIO-client-swift
获取的日志
2020-12-18 13:08:24.033132+0900 Tests[4397:171225] LOG
SocketIOClient{/}: Adding handler for event: connect 2020-12-18
13:08:24.033663+0900 Tests[4397:171225] LOG SocketIOClient{/}:
Handling event: statusChange with data: [connecting, 2] 2020-12-18
13:08:24.033811+0900 Tests[4397:171225] LOG SocketIOClient{/}: Joining
namespace / 2020-12-18 13:08:24.033930+0900 Tests[4397:171225] LOG
SocketManager: Tried connecting socket when engine isn't open.
Connecting 2020-12-18 13:08:24.034033+0900 Tests[4397:171225] LOG
SocketManager: Adding engine 2020-12-18 13:08:24.035507+0900
Tests[4397:171427] LOG SocketEngine: Starting engine. Server:
http://localhost:5000 2020-12-18 13:08:24.035608+0900
Tests[4397:171427] LOG SocketEngine: Handshaking 2020-12-18
13:08:24.038113+0900 Tests[4397:171427] LOG SocketEnginePolling: Doing
polling GET http://localhost:5000/socket.io/?transport=polling&b64=1
2020-12-18 13:08:24.125554+0900 Tests[4397:171431] []
nw_protocol_get_quic_image_block_invoke dlopen libquic failed
2020-12-18 13:08:24.166652+0900 Tests[4397:171427] LOG
SocketEnginePolling: Got polling response 2020-12-18
13:08:24.166801+0900 Tests[4397:171427] LOG SocketEnginePolling: Got
poll message:
0{"sid":"97Fb8N3Ub5ZuRlfHAAAA","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000}
2020-12-18 13:08:24.168076+0900 Tests[4397:171427] LOG SocketEngine:
Got message:
0{"sid":"97Fb8N3Ub5ZuRlfHAAAA","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000}
2020-12-18 13:08:24.171813+0900 Tests[4397:171225] LOG
SocketIOClient{/}: Handling event: ping with data: [] 2020-12-18
13:08:24.171845+0900 Tests[4397:171427] LOG SocketEnginePolling: Doing
polling GET
http://localhost:5000/socket.io/?transport=polling&b64=1&sid=97Fb8N3Ub5ZuRlfHAAAA
2020-12-18 13:08:24.172148+0900 Tests[4397:171225] LOG SocketManager:
Engine opened Connect 2020-12-18 13:08:24.172267+0900
Tests[4397:171225] LOG SocketIOClient{/}: Socket connected 2020-12-18
13:08:24.172485+0900 Tests[4397:171225] LOG SocketIOClient{/}:
Handling event: statusChange with data: [connected, 3] 2020-12-18
13:08:24.172704+0900 Tests[4397:171225] LOG SocketIOClient{/}:
Handling event: connect with data: ["/"] Connected 2020-12-18
13:08:24.174125+0900 Tests[4397:171427] LOG SocketEngine: Writing
poll: has data: false 2020-12-18 13:08:24.174260+0900
Tests[4397:171427] LOG SocketEnginePolling: Sending poll: as type: 2
2020-12-18 13:08:24.174607+0900 Tests[4397:171427] LOG
SocketEnginePolling: Created POST string: 1:2 2020-12-18
13:08:24.175188+0900 Tests[4397:171427] LOG SocketEnginePolling:
POSTing 2020-12-18 13:08:24.175708+0900 Tests[4397:171427] LOG
SocketEnginePolling: Doing polling POST
http://localhost:5000/socket.io/?transport=polling&b64=1&sid=97Fb8N3Ub5ZuRlfHAAAA
2020-12-18 13:08:24.181566+0900 Tests[4397:171225] LOG
SocketIOClient{/}: Handling event: websocketUpgrade with data:
[["upgrade": "websocket", "connection": "Upgrade",
"sec-websocket-accept": "DK4ZA/luKjOL11P+2vm4APQNE5I="]] 2020-12-18
13:08:24.181780+0900 Tests[4397:171429] LOG SocketEngineWebSocket:
Sending ws: probe as type: 2 2020-12-18 13:08:24.183557+0900
Tests[4397:171431] LOG SocketEnginePolling: Got polling response
2020-12-18 13:08:24.183723+0900 Tests[4397:171431] LOG
SocketEnginePolling: Doing polling GET
http://localhost:5000/socket.io/?transport=polling&b64=1&sid=97Fb8N3Ub5ZuRlfHAAAA
2020-12-18 13:08:24.184529+0900 Tests[4397:171427] LOG SocketEngine:
Flushing probe wait 2020-12-18 13:08:24.186620+0900 Tests[4397:171428]
ERROR SocketEnginePolling: Error during long poll request 2020-12-18
13:08:24.186736+0900 Tests[4397:171428] ERROR SocketEngine: Error
2020-12-18 13:08:24.186892+0900 Tests[4397:171225] ERROR
SocketManager: Error 2020-12-18 13:08:24.186907+0900
Tests[4397:171428] LOG SocketEngine: Engine is being closed.
2020-12-18 13:08:24.187029+0900 Tests[4397:171225] LOG
SocketIOClient{/}: Handling event: error with data: ["Error"]
2020-12-18 13:08:24.187306+0900 Tests[4397:171428] LOG
SocketEnginePolling: Created POST string: 1:1 2020-12-18
13:08:24.187548+0900 Tests[4397:171428] LOG SocketEnginePolling: Doing
polling POST
http://localhost:5000/socket.io/?transport=polling&b64=1&sid=97Fb8N3Ub5ZuRlfHAAAA
2020-12-18 13:08:24.188161+0900 Tests[4397:171225] LOG SocketManager:
Starting reconnect 2020-12-18 13:08:24.188342+0900 Tests[4397:171225]
LOG SocketIOClient{/}: Handling event: statusChange with data:
[connecting, 2] 2020-12-18 13:08:24.188558+0900 Tests[4397:171225] LOG
SocketIOClient{/}: Handling event: reconnect with data: ["Error"]
2020-12-18 13:08:24.188729+0900 Tests[4397:171225] LOG SocketManager:
Trying to reconnect 2020-12-18 13:08:24.188830+0900 Tests[4397:171225]
LOG SocketIOClient{/}: Handling event: reconnectAttempt with data:
[-1] 2020-12-18 13:08:24.189043+0900 Tests[4397:171429] LOG
SocketEngine: Starting engine. Server: http://localhost:5000
2020-12-18 13:08:24.189113+0900 Tests[4397:171225] LOG SocketManager:
Scheduling reconnect in 15.068159317825785s 2020-12-18
13:08:24.189439+0900 Tests[4397:171429] LOG SocketEngine: Handshaking
2020-12-18 13:08:24.189732+0900 Tests[4397:171429] LOG
SocketEnginePolling: Doing polling GET
http://localhost:5000/socket.io/?transport=polling&b64=1 2020-12-18
13:08:24.197564+0900 Tests[4397:171432] LOG SocketEnginePolling: Got
polling response 2020-12-18 13:08:24.197690+0900 Tests[4397:171432]
LOG SocketEnginePolling: Got poll message:
0{"sid":"7RPKclRosgovPKY5AAAB","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000}
2020-12-18 13:08:24.197839+0900 Tests[4397:171432] LOG SocketEngine:
Got message:
0{"sid":"7RPKclRosgovPKY5AAAB","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000}
2020-12-18 13:08:24.198569+0900 Tests[4397:171225] LOG
SocketIOClient{/}: Handling event: ping with data: [] 2020-12-18
13:08:24.198587+0900 Tests[4397:171432] LOG SocketEnginePolling: Doing
polling GET
http://localhost:5000/socket.io/?transport=polling&b64=1&sid=7RPKclRosgovPKY5AAAB
2020-12-18 13:08:24.198671+0900 Tests[4397:171225] LOG SocketManager:
Engine opened Connect 2020-12-18 13:08:24.198848+0900
Tests[4397:171225] LOG SocketIOClient{/}: Socket connected 2020-12-18
13:08:24.199022+0900 Tests[4397:171225] LOG SocketIOClient{/}:
Handling event: statusChange with data: [connected, 3] 2020-12-18
13:08:24.199197+0900 Tests[4397:171225] LOG SocketIOClient{/}:
Handling event: connect with data: ["/"] Connected 2020-12-18
13:08:24.199445+0900 Tests[4397:171432] LOG SocketEngine: Writing
poll: has data: false 2020-12-18 13:08:24.199714+0900
Tests[4397:171432] LOG SocketEnginePolling: Sending poll: as type: 2
2020-12-18 13:08:24.199973+0900 Tests[4397:171432] LOG
SocketEnginePolling: Created POST string: 1:2 2020-12-18
13:08:24.200257+0900 Tests[4397:171432] LOG SocketEnginePolling:
POSTing 2020-12-18 13:08:24.200524+0900 Tests[4397:171432] LOG
SocketEnginePolling: Doing polling POST
http://localhost:5000/socket.io/?transport=polling&b64=1&sid=7RPKclRosgovPKY5AAAB
2020-12-18 13:08:24.205180+0900 Tests[4397:171225] LOG
SocketIOClient{/}: Handling event: websocketUpgrade with data:
[["upgrade": "websocket", "connection": "Upgrade",
"sec-websocket-accept": "e5m4DoLk6GlrzUzZSHtQ/n0gXl0="]] 2020-12-18
13:08:24.205309+0900 Tests[4397:171429] LOG SocketEngineWebSocket:
Sending ws: probe as type: 2 2020-12-18 13:08:24.207171+0900
Tests[4397:171429] LOG SocketEngine: Got message: 3probe 2020-12-18
13:08:24.207420+0900 Tests[4397:171429] LOG SocketEngine: Received
probe response, should upgrade to WebSockets 2020-12-18
13:08:24.207622+0900 Tests[4397:171429] LOG SocketEngine: Upgrading
transport to WebSockets 2020-12-18 13:08:24.209704+0900
Tests[4397:171429] LOG SocketEnginePolling: Sending poll: as type: 6
2020-12-18 13:08:24.210114+0900 Tests[4397:171225] LOG
SocketIOClient{/}: Handling event: pong with data: [] 2020-12-18
13:08:24.210826+0900 Tests[4397:171431] LOG SocketEngine: Flushing
probe wait 2020-12-18 13:08:24.211131+0900 Tests[4397:171431] LOG
SocketEnginePolling: Got polling response 2020-12-18
13:08:24.211279+0900 Tests[4397:171431] LOG SocketEngine: Switching to
WebSockets 2020-12-18 13:08:24.211500+0900 Tests[4397:171431] LOG
SocketEngineWebSocket: Sending ws: as type: 5 2020-12-18
13:08:24.211974+0900 Tests[4397:171431] LOG SocketEngine: Flushing
probe wait
可能与安全有关。
通常 ios 应用程序希望使用 https 进行连接。要允许 http 连接,请在您的
info.plist 添加这个(临时安全修复):
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
我正在尝试将 iOS 应用与 Node.js WebSocket 服务器集成。 我为 iOS 使用 SwiftUI,为服务器和客户端使用 socket.io。
但是当我尝试将 iOS 应用程序与服务器连接时, 好像连不上服务器
我通过将 React 应用程序连接到 Node.js 服务器来测试服务器是否 运行 并且连接在浏览器上工作正常。
我检查了 swift 日志,它似乎在尝试与服务器握手,但是, 它开始长轮询,所以我猜握手过程有一些问题。
如果有新连接,我正在控制台将“新连接”作为字符串记录在 Node.js 服务器中,它对 React 工作正常,但对 Swift。
但我没有提前尝试任何事情,所以我无法找出问题所在。
有人可以帮我吗?
import SwiftUI
import SocketIO
final class Service: ObservableObject {
private var manager = SocketManager(socketURL:URL(string: "http://localhost:5000")!, config: [.log(true), .compress])
init() {
let socket = manager.defaultSocket
socket.on(clientEvent: .connect) { (data,act) in
print("Connected")
}
socket.connect()
}
}
struct SocketIO: View {
@ObservedObject var service = Service()
var body: some View {
Text("Hello, Web socket!")
}
}
这是我从 SocketIO-client-swift
获取的日志2020-12-18 13:08:24.033132+0900 Tests[4397:171225] LOG SocketIOClient{/}: Adding handler for event: connect 2020-12-18 13:08:24.033663+0900 Tests[4397:171225] LOG SocketIOClient{/}: Handling event: statusChange with data: [connecting, 2] 2020-12-18 13:08:24.033811+0900 Tests[4397:171225] LOG SocketIOClient{/}: Joining namespace / 2020-12-18 13:08:24.033930+0900 Tests[4397:171225] LOG SocketManager: Tried connecting socket when engine isn't open. Connecting 2020-12-18 13:08:24.034033+0900 Tests[4397:171225] LOG SocketManager: Adding engine 2020-12-18 13:08:24.035507+0900 Tests[4397:171427] LOG SocketEngine: Starting engine. Server: http://localhost:5000 2020-12-18 13:08:24.035608+0900 Tests[4397:171427] LOG SocketEngine: Handshaking 2020-12-18 13:08:24.038113+0900 Tests[4397:171427] LOG SocketEnginePolling: Doing polling GET http://localhost:5000/socket.io/?transport=polling&b64=1 2020-12-18 13:08:24.125554+0900 Tests[4397:171431] [] nw_protocol_get_quic_image_block_invoke dlopen libquic failed 2020-12-18 13:08:24.166652+0900 Tests[4397:171427] LOG SocketEnginePolling: Got polling response 2020-12-18 13:08:24.166801+0900 Tests[4397:171427] LOG SocketEnginePolling: Got poll message: 0{"sid":"97Fb8N3Ub5ZuRlfHAAAA","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000} 2020-12-18 13:08:24.168076+0900 Tests[4397:171427] LOG SocketEngine: Got message: 0{"sid":"97Fb8N3Ub5ZuRlfHAAAA","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000} 2020-12-18 13:08:24.171813+0900 Tests[4397:171225] LOG SocketIOClient{/}: Handling event: ping with data: [] 2020-12-18 13:08:24.171845+0900 Tests[4397:171427] LOG SocketEnginePolling: Doing polling GET http://localhost:5000/socket.io/?transport=polling&b64=1&sid=97Fb8N3Ub5ZuRlfHAAAA 2020-12-18 13:08:24.172148+0900 Tests[4397:171225] LOG SocketManager: Engine opened Connect 2020-12-18 13:08:24.172267+0900 Tests[4397:171225] LOG SocketIOClient{/}: Socket connected 2020-12-18 13:08:24.172485+0900 Tests[4397:171225] LOG SocketIOClient{/}: Handling event: statusChange with data: [connected, 3] 2020-12-18 13:08:24.172704+0900 Tests[4397:171225] LOG SocketIOClient{/}: Handling event: connect with data: ["/"] Connected 2020-12-18 13:08:24.174125+0900 Tests[4397:171427] LOG SocketEngine: Writing poll: has data: false 2020-12-18 13:08:24.174260+0900 Tests[4397:171427] LOG SocketEnginePolling: Sending poll: as type: 2 2020-12-18 13:08:24.174607+0900 Tests[4397:171427] LOG SocketEnginePolling: Created POST string: 1:2 2020-12-18 13:08:24.175188+0900 Tests[4397:171427] LOG SocketEnginePolling: POSTing 2020-12-18 13:08:24.175708+0900 Tests[4397:171427] LOG SocketEnginePolling: Doing polling POST http://localhost:5000/socket.io/?transport=polling&b64=1&sid=97Fb8N3Ub5ZuRlfHAAAA 2020-12-18 13:08:24.181566+0900 Tests[4397:171225] LOG SocketIOClient{/}: Handling event: websocketUpgrade with data: [["upgrade": "websocket", "connection": "Upgrade", "sec-websocket-accept": "DK4ZA/luKjOL11P+2vm4APQNE5I="]] 2020-12-18 13:08:24.181780+0900 Tests[4397:171429] LOG SocketEngineWebSocket: Sending ws: probe as type: 2 2020-12-18 13:08:24.183557+0900 Tests[4397:171431] LOG SocketEnginePolling: Got polling response 2020-12-18 13:08:24.183723+0900 Tests[4397:171431] LOG SocketEnginePolling: Doing polling GET http://localhost:5000/socket.io/?transport=polling&b64=1&sid=97Fb8N3Ub5ZuRlfHAAAA 2020-12-18 13:08:24.184529+0900 Tests[4397:171427] LOG SocketEngine: Flushing probe wait 2020-12-18 13:08:24.186620+0900 Tests[4397:171428] ERROR SocketEnginePolling: Error during long poll request 2020-12-18 13:08:24.186736+0900 Tests[4397:171428] ERROR SocketEngine: Error 2020-12-18 13:08:24.186892+0900 Tests[4397:171225] ERROR SocketManager: Error 2020-12-18 13:08:24.186907+0900 Tests[4397:171428] LOG SocketEngine: Engine is being closed. 2020-12-18 13:08:24.187029+0900 Tests[4397:171225] LOG SocketIOClient{/}: Handling event: error with data: ["Error"] 2020-12-18 13:08:24.187306+0900 Tests[4397:171428] LOG SocketEnginePolling: Created POST string: 1:1 2020-12-18 13:08:24.187548+0900 Tests[4397:171428] LOG SocketEnginePolling: Doing polling POST http://localhost:5000/socket.io/?transport=polling&b64=1&sid=97Fb8N3Ub5ZuRlfHAAAA 2020-12-18 13:08:24.188161+0900 Tests[4397:171225] LOG SocketManager: Starting reconnect 2020-12-18 13:08:24.188342+0900 Tests[4397:171225] LOG SocketIOClient{/}: Handling event: statusChange with data: [connecting, 2] 2020-12-18 13:08:24.188558+0900 Tests[4397:171225] LOG SocketIOClient{/}: Handling event: reconnect with data: ["Error"] 2020-12-18 13:08:24.188729+0900 Tests[4397:171225] LOG SocketManager: Trying to reconnect 2020-12-18 13:08:24.188830+0900 Tests[4397:171225] LOG SocketIOClient{/}: Handling event: reconnectAttempt with data: [-1] 2020-12-18 13:08:24.189043+0900 Tests[4397:171429] LOG SocketEngine: Starting engine. Server: http://localhost:5000 2020-12-18 13:08:24.189113+0900 Tests[4397:171225] LOG SocketManager: Scheduling reconnect in 15.068159317825785s 2020-12-18 13:08:24.189439+0900 Tests[4397:171429] LOG SocketEngine: Handshaking 2020-12-18 13:08:24.189732+0900 Tests[4397:171429] LOG SocketEnginePolling: Doing polling GET http://localhost:5000/socket.io/?transport=polling&b64=1 2020-12-18 13:08:24.197564+0900 Tests[4397:171432] LOG SocketEnginePolling: Got polling response 2020-12-18 13:08:24.197690+0900 Tests[4397:171432] LOG SocketEnginePolling: Got poll message: 0{"sid":"7RPKclRosgovPKY5AAAB","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000} 2020-12-18 13:08:24.197839+0900 Tests[4397:171432] LOG SocketEngine: Got message: 0{"sid":"7RPKclRosgovPKY5AAAB","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000} 2020-12-18 13:08:24.198569+0900 Tests[4397:171225] LOG SocketIOClient{/}: Handling event: ping with data: [] 2020-12-18 13:08:24.198587+0900 Tests[4397:171432] LOG SocketEnginePolling: Doing polling GET http://localhost:5000/socket.io/?transport=polling&b64=1&sid=7RPKclRosgovPKY5AAAB 2020-12-18 13:08:24.198671+0900 Tests[4397:171225] LOG SocketManager: Engine opened Connect 2020-12-18 13:08:24.198848+0900 Tests[4397:171225] LOG SocketIOClient{/}: Socket connected 2020-12-18 13:08:24.199022+0900 Tests[4397:171225] LOG SocketIOClient{/}: Handling event: statusChange with data: [connected, 3] 2020-12-18 13:08:24.199197+0900 Tests[4397:171225] LOG SocketIOClient{/}: Handling event: connect with data: ["/"] Connected 2020-12-18 13:08:24.199445+0900 Tests[4397:171432] LOG SocketEngine: Writing poll: has data: false 2020-12-18 13:08:24.199714+0900 Tests[4397:171432] LOG SocketEnginePolling: Sending poll: as type: 2 2020-12-18 13:08:24.199973+0900 Tests[4397:171432] LOG SocketEnginePolling: Created POST string: 1:2 2020-12-18 13:08:24.200257+0900 Tests[4397:171432] LOG SocketEnginePolling: POSTing 2020-12-18 13:08:24.200524+0900 Tests[4397:171432] LOG SocketEnginePolling: Doing polling POST http://localhost:5000/socket.io/?transport=polling&b64=1&sid=7RPKclRosgovPKY5AAAB 2020-12-18 13:08:24.205180+0900 Tests[4397:171225] LOG SocketIOClient{/}: Handling event: websocketUpgrade with data: [["upgrade": "websocket", "connection": "Upgrade", "sec-websocket-accept": "e5m4DoLk6GlrzUzZSHtQ/n0gXl0="]] 2020-12-18 13:08:24.205309+0900 Tests[4397:171429] LOG SocketEngineWebSocket: Sending ws: probe as type: 2 2020-12-18 13:08:24.207171+0900 Tests[4397:171429] LOG SocketEngine: Got message: 3probe 2020-12-18 13:08:24.207420+0900 Tests[4397:171429] LOG SocketEngine: Received probe response, should upgrade to WebSockets 2020-12-18 13:08:24.207622+0900 Tests[4397:171429] LOG SocketEngine: Upgrading transport to WebSockets 2020-12-18 13:08:24.209704+0900 Tests[4397:171429] LOG SocketEnginePolling: Sending poll: as type: 6 2020-12-18 13:08:24.210114+0900 Tests[4397:171225] LOG SocketIOClient{/}: Handling event: pong with data: [] 2020-12-18 13:08:24.210826+0900 Tests[4397:171431] LOG SocketEngine: Flushing probe wait 2020-12-18 13:08:24.211131+0900 Tests[4397:171431] LOG SocketEnginePolling: Got polling response 2020-12-18 13:08:24.211279+0900 Tests[4397:171431] LOG SocketEngine: Switching to WebSockets 2020-12-18 13:08:24.211500+0900 Tests[4397:171431] LOG SocketEngineWebSocket: Sending ws: as type: 5 2020-12-18 13:08:24.211974+0900 Tests[4397:171431] LOG SocketEngine: Flushing probe wait
可能与安全有关。 通常 ios 应用程序希望使用 https 进行连接。要允许 http 连接,请在您的 info.plist 添加这个(临时安全修复):
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>