gpt4 book ai didi

node.js - 使用 Swift 的 Socket.io 不是握手

转载 作者:行者123 更新时间:2023-12-04 03:44:48 25 4
gpt4 key购买 nike

我正在尝试将 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] LOGSocketIOClient{/}: Adding handler for event: connect 2020-12-1813:08:24.033663+0900 Tests[4397:171225] LOG SocketIOClient{/}:Handling event: statusChange with data: [connecting, 2] 2020-12-1813:08:24.033811+0900 Tests[4397:171225] LOG SocketIOClient{/}: Joiningnamespace / 2020-12-18 13:08:24.033930+0900 Tests[4397:171225] LOGSocketManager: Tried connecting socket when engine isn't open.Connecting 2020-12-18 13:08:24.034033+0900 Tests[4397:171225] LOGSocketManager: Adding engine 2020-12-18 13:08:24.035507+0900Tests[4397:171427] LOG SocketEngine: Starting engine. Server:http://localhost:5000 2020-12-18 13:08:24.035608+0900Tests[4397:171427] LOG SocketEngine: Handshaking 2020-12-1813:08:24.038113+0900 Tests[4397:171427] LOG SocketEnginePolling: Doingpolling GET http://localhost:5000/socket.io/?transport=polling&b64=12020-12-18 13:08:24.125554+0900 Tests[4397:171431] []nw_protocol_get_quic_image_block_invoke dlopen libquic failed2020-12-18 13:08:24.166652+0900 Tests[4397:171427] LOGSocketEnginePolling: Got polling response 2020-12-1813:08:24.166801+0900 Tests[4397:171427] LOG SocketEnginePolling: Gotpoll 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] LOGSocketIOClient{/}: Handling event: ping with data: [] 2020-12-1813:08:24.171845+0900 Tests[4397:171427] LOG SocketEnginePolling: Doingpolling GEThttp://localhost:5000/socket.io/?transport=polling&b64=1&sid=97Fb8N3Ub5ZuRlfHAAAA2020-12-18 13:08:24.172148+0900 Tests[4397:171225] LOG SocketManager:Engine opened Connect 2020-12-18 13:08:24.172267+0900Tests[4397:171225] LOG SocketIOClient{/}: Socket connected 2020-12-1813:08:24.172485+0900 Tests[4397:171225] LOG SocketIOClient{/}:Handling event: statusChange with data: [connected, 3] 2020-12-1813:08:24.172704+0900 Tests[4397:171225] LOG SocketIOClient{/}:Handling event: connect with data: ["/"] Connected 2020-12-1813:08:24.174125+0900 Tests[4397:171427] LOG SocketEngine: Writingpoll: has data: false 2020-12-18 13:08:24.174260+0900Tests[4397:171427] LOG SocketEnginePolling: Sending poll: as type: 22020-12-18 13:08:24.174607+0900 Tests[4397:171427] LOGSocketEnginePolling: Created POST string: 1:2 2020-12-1813:08:24.175188+0900 Tests[4397:171427] LOG SocketEnginePolling:POSTing 2020-12-18 13:08:24.175708+0900 Tests[4397:171427] LOGSocketEnginePolling: Doing polling POSThttp://localhost:5000/socket.io/?transport=polling&b64=1&sid=97Fb8N3Ub5ZuRlfHAAAA2020-12-18 13:08:24.181566+0900 Tests[4397:171225] LOGSocketIOClient{/}: Handling event: websocketUpgrade with data:[["upgrade": "websocket", "connection": "Upgrade","sec-websocket-accept": "DK4ZA/luKjOL11P+2vm4APQNE5I="]] 2020-12-1813:08:24.181780+0900 Tests[4397:171429] LOG SocketEngineWebSocket:Sending ws: probe as type: 2 2020-12-18 13:08:24.183557+0900Tests[4397:171431] LOG SocketEnginePolling: Got polling response2020-12-18 13:08:24.183723+0900 Tests[4397:171431] LOGSocketEnginePolling: Doing polling GEThttp://localhost:5000/socket.io/?transport=polling&b64=1&sid=97Fb8N3Ub5ZuRlfHAAAA2020-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-1813:08:24.186736+0900 Tests[4397:171428] ERROR SocketEngine: Error2020-12-18 13:08:24.186892+0900 Tests[4397:171225] ERRORSocketManager: Error 2020-12-18 13:08:24.186907+0900Tests[4397:171428] LOG SocketEngine: Engine is being closed.2020-12-18 13:08:24.187029+0900 Tests[4397:171225] LOGSocketIOClient{/}: Handling event: error with data: ["Error"]2020-12-18 13:08:24.187306+0900 Tests[4397:171428] LOGSocketEnginePolling: Created POST string: 1:1 2020-12-1813:08:24.187548+0900 Tests[4397:171428] LOG SocketEnginePolling: Doingpolling POSThttp://localhost:5000/socket.io/?transport=polling&b64=1&sid=97Fb8N3Ub5ZuRlfHAAAA2020-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] LOGSocketIOClient{/}: 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] LOGSocketEngine: Starting engine. Server: http://localhost:50002020-12-18 13:08:24.189113+0900 Tests[4397:171225] LOG SocketManager:Scheduling reconnect in 15.068159317825785s 2020-12-1813:08:24.189439+0900 Tests[4397:171429] LOG SocketEngine: Handshaking2020-12-18 13:08:24.189732+0900 Tests[4397:171429] LOGSocketEnginePolling: Doing polling GEThttp://localhost:5000/socket.io/?transport=polling&b64=1 2020-12-1813:08:24.197564+0900 Tests[4397:171432] LOG SocketEnginePolling: Gotpolling 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] LOGSocketIOClient{/}: Handling event: ping with data: [] 2020-12-1813:08:24.198587+0900 Tests[4397:171432] LOG SocketEnginePolling: Doingpolling GEThttp://localhost:5000/socket.io/?transport=polling&b64=1&sid=7RPKclRosgovPKY5AAAB2020-12-18 13:08:24.198671+0900 Tests[4397:171225] LOG SocketManager:Engine opened Connect 2020-12-18 13:08:24.198848+0900Tests[4397:171225] LOG SocketIOClient{/}: Socket connected 2020-12-1813:08:24.199022+0900 Tests[4397:171225] LOG SocketIOClient{/}:Handling event: statusChange with data: [connected, 3] 2020-12-1813:08:24.199197+0900 Tests[4397:171225] LOG SocketIOClient{/}:Handling event: connect with data: ["/"] Connected 2020-12-1813:08:24.199445+0900 Tests[4397:171432] LOG SocketEngine: Writingpoll: has data: false 2020-12-18 13:08:24.199714+0900Tests[4397:171432] LOG SocketEnginePolling: Sending poll: as type: 22020-12-18 13:08:24.199973+0900 Tests[4397:171432] LOGSocketEnginePolling: Created POST string: 1:2 2020-12-1813:08:24.200257+0900 Tests[4397:171432] LOG SocketEnginePolling:POSTing 2020-12-18 13:08:24.200524+0900 Tests[4397:171432] LOGSocketEnginePolling: Doing polling POSThttp://localhost:5000/socket.io/?transport=polling&b64=1&sid=7RPKclRosgovPKY5AAAB2020-12-18 13:08:24.205180+0900 Tests[4397:171225] LOGSocketIOClient{/}: Handling event: websocketUpgrade with data:[["upgrade": "websocket", "connection": "Upgrade","sec-websocket-accept": "e5m4DoLk6GlrzUzZSHtQ/n0gXl0="]] 2020-12-1813:08:24.205309+0900 Tests[4397:171429] LOG SocketEngineWebSocket:Sending ws: probe as type: 2 2020-12-18 13:08:24.207171+0900Tests[4397:171429] LOG SocketEngine: Got message: 3probe 2020-12-1813:08:24.207420+0900 Tests[4397:171429] LOG SocketEngine: Receivedprobe response, should upgrade to WebSockets 2020-12-1813:08:24.207622+0900 Tests[4397:171429] LOG SocketEngine: Upgradingtransport to WebSockets 2020-12-18 13:08:24.209704+0900Tests[4397:171429] LOG SocketEnginePolling: Sending poll: as type: 62020-12-18 13:08:24.210114+0900 Tests[4397:171225] LOGSocketIOClient{/}: Handling event: pong with data: [] 2020-12-1813:08:24.210826+0900 Tests[4397:171431] LOG SocketEngine: Flushingprobe wait 2020-12-18 13:08:24.211131+0900 Tests[4397:171431] LOGSocketEnginePolling: Got polling response 2020-12-1813:08:24.211279+0900 Tests[4397:171431] LOG SocketEngine: Switching toWebSockets 2020-12-18 13:08:24.211500+0900 Tests[4397:171431] LOGSocketEngineWebSocket: Sending ws: as type: 5 2020-12-1813:08:24.211974+0900 Tests[4397:171431] LOG SocketEngine: Flushingprobe wait

最佳答案

这可能与安全有关。通常 ios 应用程序期望使用 https 进行连接。要允许 http 连接,请在您的info.plist 添加这个(临时安全修复):

<key>NSAppTransportSecurity</key>
<dict>

<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

关于node.js - 使用 Swift 的 Socket.io 不是握手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65351721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com