Socket IO Client swift 无法连接到我的 Socket 服务器

时间:2021-07-02 07:54:16

标签: ios swift express socket.io

我正在尝试将我的套接字连接到本地套接字服务器,但出现如下错误。

2021-07-02 14:50:24.160971+0700 Bitnance[34074:3797706] LOG SocketManager: Trying to reconnect
2021-07-02 14:50:24.161214+0700 Bitnance[34074:3797706] LOG SocketIOClient{/}: Handling event: reconnectAttempt with data: [-3]
2021-07-02 14:50:24.161370+0700 Bitnance[34074:3797706] LOG SocketManager: Scheduling reconnect in 30.0s
2021-07-02 14:50:24.161427+0700 Bitnance[34074:3800408] LOG SocketEngine: Starting engine. Server: http://192.168.101.123:3000
2021-07-02 14:50:24.161568+0700 Bitnance[34074:3800408] LOG SocketEngine: Handshaking
2021-07-02 14:50:24.161786+0700 Bitnance[34074:3800408] LOG SocketEnginePolling: Doing polling GET http://192.168.101.123:3000/socket.io/?transport=polling&b64=1
2021-07-02 14:50:24.166476+0700 Bitnance[34074:3800408] ERROR SocketEnginePolling: Error during long poll request
2021-07-02 14:50:24.166616+0700 Bitnance[34074:3800408] ERROR SocketEngine: Error
2021-07-02 14:50:24.166774+0700 Bitnance[34074:3797706] ERROR SocketManager: Error
2021-07-02 14:50:24.166909+0700 Bitnance[34074:3797706] LOG SocketIOClient{/}: Handling event: error with data: ["Error"]

我的套接字管理器类


import Foundation
import SocketIO

class SocketIOManager: NSObject {
    static let sharedInstance = SocketIOManager()

    let manager = SocketManager(socketURL: URL(string: "http://192.168.101.123:3000")!, config: [.log(true)])
    var socket: SocketIOClient!

    override init() {
        super.init()
        socket = manager.defaultSocket
    }
    
    //Function to establish the socket connection with your server. Generally you want to call this method from your `Appdelegate` in the `applicationDidBecomeActive` method.
    func establishConnection() {
        socket.connect()
        print("Connected to Socket !")
    }

    //Function to close established socket connection. Call this method from `applicationDidEnterBackground` in your `Appdelegate` method.
    func closeConnection() {
        socket.disconnect()
        print("Disconnected from Socket !")
    }
}

我的 server.js

const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const port = process.env.PORT || 3000;

function onConnection(socket){
   console.log("connected")
}

io.on('connection', onConnection);

http.listen(port, () => console.log('listening on port ' + port));

我不知道为什么它不起作用。有没有人见过这个错误? 了解更多信息 pod 'Socket.IO-Client-Swift', '~> 15.2.0'

0 个答案:

没有答案
相关问题