带有 iOS 的 Socket.io 未在移动客户端上连接

Posted

技术标签:

【中文标题】带有 iOS 的 Socket.io 未在移动客户端上连接【英文标题】:Socket.io with iOS not connecting on the mobile client 【发布时间】:2019-11-14 02:31:45 【问题描述】:

我正在尝试在服务器和 ios 客户端之间创建简单的 socket.io 通信。 我的服务器代码很简单:

var io = require('socket.io').listen(1337);

io.sockets.on('connection', function (socket) 
    console.log("Someone just connected!");

    // Echo back messages from the client
    socket.on('image', function (message) 
        console.log("Got image of length: " + message.length);
        socket.emit('image', message);
    );

    socket.on('sound', function (message) 
        console.log("Got sound of length: " + message.length);
        socket.emit('sound', message);
    );

    socket.on('pdf', function (message) 
        console.log("Got pdf of length: " + message.length);
        socket.emit('pdf', message);
    );
);

并遵循以下示例:https://github.com/socketio/socket.io-client-swift

我已将我的客户端实现为:

class ViewController: UIViewController 

    override func viewDidLoad() 
        super.viewDidLoad()
        setupSocketIO()
    

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

        socket.on(clientEvent: .connect) data, ack in
            print("socket connected")
        

        socket.on("image") data, ack in
            guard let cur = data[0] as? String else  return 
            print("got image back: ", cur)
        

        socket.connect()

        socket.emit("image", "sample")
    

运行代码后,行为真的很奇怪,因为在服务器端我的日志消息Someone just connected!,但在客户端没有连接迹象(这意味着socket connected ,也没有看到日志中的 sample 消息)。

登录 XCode:

SocketIOClient/: Adding handler for event: connect
SocketIOClient/: Adding handler for event: image
SocketIOClient/: Handling event: error with data: ["Tried emitting when not connected"]
SocketIOClient/: Handling event: statusChange with data: [connecting, 2]
SocketIOClient/: Joining namespace /
SocketManager: Tried connecting socket when engine isn't open. Connecting
SocketManager: Adding engine
SocketManager: Manager is being released
SocketEngine: Starting engine. Server: http://127.0.0.1:1337
SocketEngine: Handshaking
SocketIOClient/: Client is being relea2
SocketEnginePolling: Doing polling GET http://127.0.0.1:1337/socket.io/?transport=polling&b64=1
SocketEnginePolling: Got polling response
SocketEnginePolling: Got poll message: 96:0"sid":"4k2wFGd250H88zmYAAAE","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":50002:40
SocketEngine: Got message: 0"sid":"4k2wFGd250H88zmYAAAE","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000
SocketEngine: Got message: 40
SocketEngine: Writing poll:  has data: false
SocketEnginePolling: Sending poll:  as type: 2
SocketEnginePolling: Created POST string: 1:2
SocketEnginePolling: POSTing
SocketEngine: Engine is being released

【问题讨论】:

在我看来,您的服务器使用的是旧版本的 socket io,0.9 版本,根据github.com/socketio/socket.io-client-swift,您应该使用版本 2+,也许您的问题来自于此。跨度> 您是否尝试在函数之外设置套接字变量?在课堂上声明它不会被释放 我已经为服务器升级了 socket.io,但它没有帮助。在类中设置套接字变量就可以了!可能这两件事都是它工作所必需的:)谢谢你们! 【参考方案1】:

当前版本的 IOS 套接字 io 客户端库 (socketio/socket.io-client-swift:15.x) 尚不支持套接字 io 服务器版本 3.x。

理由:新协议下的默认命名空间声明存在问题。如果启用调试模式,您会看到 Socket 服务器仍然接收来自客户端的消息,但不会触发连接、发射等事件。

解决方案:将您的 NodeJS 套接字 io 服务器版本降级到 2.x 版本

例如: npm install socket.io@2.0.4

【讨论】:

nodejs服务器降级了,ios客户端还是不行。

以上是关于带有 iOS 的 Socket.io 未在移动客户端上连接的主要内容,如果未能解决你的问题,请参考以下文章

使用带有 Socket.io IOS 客户端的客户端证书

使用带有多个模块的 socket.io 客户端

socket.io 连接事件未在 Firefox 中触发

带有 Node.js 的 Socket.io

MongoError:未在 db 上授权执行命令?与 socket.io 连接时

带有 socket.io 的节点服务器不会建立连接