无法通过 Socket.io 快速建立连接

Posted

技术标签:

【中文标题】无法通过 Socket.io 快速建立连接【英文标题】:Not able to establish a connection over Socket.io in swift 【发布时间】:2018-07-28 22:05:44 【问题描述】:

我正在使用socket.io swift 框架。我正在尝试通过 websockets API 进行连接,我有一个方法,它在 ViewControllerviewDidLoad 中被调用。我已经在全局范围内声明了我的经理和 socketClient,我将把该方法的代码放在下面:

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()

我面临的问题是 connect 回调没有被调用,因此我无法发出 ping 消息。以下是日志:

2018-02-18 19:02:20.589406+0530 stock-cake[10965:3662189] LOG SocketIOClient/watch:处理事件:statusChange with data:[connecting]

2018-02-18 19:02:20.589692+0530 stock-cake[10965:3662189] LOG SocketIOClient/watch:加入命名空间/watch

2018-02-18 19:02:20.589850+0530 stock-cake[10965:3662189] LOG SocketManager:在引擎未打开时尝试连接套接字。连接

2018-02-18 19:02:20.589916+0530 stock-cake[10965:3662189] LOG SocketManager: 添加引擎

2018-02-18 19:02:32.986354+0530 stock-cake[10965:3662271] LOG SocketEngine:启动引擎。服务器:http://kaboom.rksv.net

2018-02-18 19:02:32.988480+0530 stock-cake[10965:3662271] LOG SocketEngine:握手

2018-02-18 19:02:32.995078+0530 stock-cake[10965:3662271] LOG SocketEnginePolling: Doing polling GET http://kaboom.rksv.net/socket.io/?transport=polling&b64=1

2018-02-18 19:02:33.197687+0530 stock-cake[10965:3662406] LOG SocketEnginePolling: 得到轮询响应

2018-02-18 19:02:33.200976+0530 stock-cake[10965:3662406] LOG SocketEnginePolling: Got poll message: 97:0"sid":"4Uf21Mr7_9DGjSXcAASj","upgrades":["websocket" ],"pingInterval":25000,"pingTimeout":60000

2018-02-18 19:02:33.208867+0530 stock-cake[10965:3662406] LOG SocketEngine: Got message: 0"sid":"4Uf21Mr7_9DGjSXcAASj","upgrades":["websocket"]," pingInterval":25000,"pingTimeout":60000

2018-02-18 19:02:33.226824+0530 stock-cake[10965:3662189] LOG SocketManager:引擎打开连接

2018-02-18 19:02:33.226894+0530 stock-cake[10965:3662406] LOG SocketEnginePolling: Doing polling GET http://kaboom.rksv.net/socket.io/?transport=polling&b64=1&sid=4Uf21Mr7_9DGjSXcAASj

2018-02-18 19:02:33.227367+0530 stock-cake[10965:3662406] LOG SocketEngine:写入投票:有数据:false

2018-02-18 19:02:33.227447+0530 stock-cake[10965:3662406] LOG SocketEnginePolling: 发送投票: as type: 2

2018-02-18 19:02:33.230935+0530 stock-cake[10965:3662406] LOG SocketEnginePolling:创建的 POST 字符串:1:2

2018-02-18 19:02:33.231392+0530 stock-cake[10965:3662406] LOG SocketEnginePolling: POSTing

2018-02-18 19:02:33.231510+0530 stock-cake[10965:3662406] LOG SocketEnginePolling: Doing polling POST http://kaboom.rksv.net/socket.io/?transport=polling&b64=1&sid=4Uf21Mr7_9DGjSXcAASj

2018-02-18 19:02:33.280857+0530 stock-cake[10965:3662406] LOG SocketEnginePolling: 得到轮询响应

2018-02-18 19:02:33.281066+0530 stock-cake[10965:3662406] LOG SocketEnginePolling: 得到轮询消息: 2:40

2018-02-18 19:02:33.281933+0530 stock-cake[10965:3662406] LOG SocketEngine: Got message: 40

2018-02-18 19:02:33.282346+0530 stock-cake[10965:3662189] LOG SocketParser: Parsing 0

2018-02-18 19:02:33.283167+0530 stock-cake[10965:3662189] LOG SocketParser: 解码包为: SocketPacket type: 0;数据: [];编号:-1;占位符:0; nsp:/

2018-02-18 19:02:33.283195+0530 stock-cake[10965:3662406] LOG SocketEnginePolling: Doing polling GET http://kaboom.rksv.net/socket.io/?transport=polling&b64=1&sid=4Uf21Mr7_9DGjSXcAASj

2018-02-18 19:02:33.330123+0530 stock-cake[10965:3662271] LOG SocketEngineWebSocket: 发送 ws: probe as type: 2

2018-02-18 19:02:33.340044+0530 stock-cake[10965:3662407] LOG SocketEnginePolling: 得到轮询响应

2018-02-18 19:02:33.340351+0530stocks-cake[10965:3662407] LOG SocketEnginePolling:得到轮询消息:1:3

2018-02-18 19:02:33.340621+0530 stock-cake[10965:3662407] LOG SocketEngine: Got message: 3

2018-02-18 19:02:33.341829+0530 stock-cake[10965:3662407] LOG SocketEnginePolling: Doing polling GET http://kaboom.rksv.net/socket.io/?transport=polling&b64=1&sid=4Uf21Mr7_9DGjSXcAASj

2018-02-18 19:02:33.378473+0530 stock-cake[10965:3662407] LOG SocketEngine: Got message: 3probe

2018-02-18 19:02:33.379059+0530 stock-cake[10965:3662407] LOG SocketEngine: 收到探测响应,应该升级到 WebSockets

2018-02-18 19:02:33.379252+0530 stock-cake[10965:3662407] LOG SocketEngine:将传输升级到 WebSockets

2018-02-18 19:02:33.379368+0530 stock-cake[10965:3662407] LOG SocketEnginePolling: 发送投票: as type: 6

2018-02-18 19:02:33.379511+0530 stock-cake[10965:3662407] LOG SocketEnginePolling: Created POST string: 1:6

2018-02-18 19:02:33.379968+0530 stock-cake[10965:3662407] LOG SocketEnginePolling: POSTing

2018-02-18 19:02:33.477785+0530 stock-cake[10965:3662407] LOG SocketEnginePolling: 得到轮询响应

2018-02-18 19:02:33.478016+0530stocks-cake[10965:3662407] LOG SocketEnginePolling:得到轮询消息:1:6

2018-02-18 19:02:33.478639+0530 stock-cake[10965:3662407] LOG SocketEngine: Got message: 6

2018-02-18 19:02:33.479275+0530 stock-cake[10965:3662407] LOG SocketEngine:切换到 WebSockets

2018-02-18 19:02:33.479594+0530 stock-cake[10965:3662407] LOG SocketEngineWebSocket: Sending ws: as type: 5

2018-02-18 19:02:33.480317+0530 stock-cake[10965:3662407] LOG SocketEngine:刷新探针等待

2018-02-18 19:02:59.288327+0530 stock-cake[10965:3662407] LOG SocketEngine: 写入 ws: has data: false

2018-02-18 19:02:59.288576+0530 stock-cake[10965:3662407] LOG SocketEngineWebSocket: Sending ws: as type: 2

2018-02-18 19:02:59.338827+0530 stock-cake[10965:3662407] LOG SocketEngine: Got message: 3

2018-02-18 19:03:26.788673+0530 stock-cake[10965:3662679] LOG SocketEngine: 写入 ws: has data: false

我有什么遗漏的吗?做错了什么?提前致谢。

【问题讨论】:

你是怎么解决的? 在方法范围外声明管理器。我遇到了一个问题,当执行超出方法时,经理被释放。 @shravan.sukumar 非常感谢你的朋友......你拯救了我的日子,我试图在 2 天内解决这个问题,最后我解决了。 嗨 @shravan.sukumar 你找到任何解决方案了吗? 【参考方案1】:

声明应该是这样的,你忘了设置端口

   let manager = SocketManager(socketURL: URL(string: "http://localhost:8080")!, config: [.log(true), .compress])
   let socket = manager.defaultSocket

【讨论】:

我没有在本地运行它。它是第三方服务器。 @shravan.sukumar 你得到解决方案了吗?【参考方案2】:

使用如下命名空间:

let socket = socketManager.socket(forNamespace: "/watch")

【讨论】:

以上是关于无法通过 Socket.io 快速建立连接的主要内容,如果未能解决你的问题,请参考以下文章

Cors 标头未正确处理(http 请求很好,但 socke.io 连接不正确)

无法在 Node.JS 和 React-Native (Socket.IO) 之间建立连接

如何通过 socks5 代理使用 JavaScript socket.io-client 库?

使用socket.io连接mongodb

Socket.IO 无法通过 https 连接

Socket.io 服务器无法向客户端发送数据