0

我已经在我的项目中尝试过Socketio库但无法建立连接,我还想发送mattermost服务器的身份验证头来检查连接是否成功。任何人都可以提出任何解决方案来解决这个问题吗?请参考我使用的以下代码和错误日志。

    let manager = SocketManager(socketURL: URL(string: "******")!,config: [.log(true), .compress, .connectParams([JSON DATA])
        }
            let namespaceSocket = self.manager.socket(forNamespace: "/****")
            self.addHandlers(socket: namespaceSocket)
            namespaceSocket.connect()
        namespaceSocket.on(clientEvent: .connect) {data, ack in
            print("socket connected")
        }
        namespaceSocket.on(clientEvent: .error) {data, ack in
            print("socket not connected")
        }

/*****错误*******/

**** LOG SocketIOClient{****}: Adding handler for event: connect
**** LOG SocketIOClient{****}: Adding handler for event: error
**** LOG SocketIOClient{****}: Handling event: statusChange with data: [connecting]
**** LOG SocketIOClient{****}: Joining namespace /****
**** LOG SocketManager: Tried connecting socket when engine isn't open. Connecting
**** LOG SocketManager: Adding engine
**** LOG SocketEngine: Starting engine. Server: ****
**** LOG SocketEngine: Handshaking
**** LOG SocketEnginePolling: Doing polling GET ****
**** LOG SocketIOClient{/***}: Handling event: error with data: ["Tried emitting when not connected"]
4

1 回答 1

2

试试这个https://github.com/swiftsocket/SwiftSocket。我在我的项目中使用它,它工作得很好。使用任何合适的依赖管理器在您的项目中安装 SwiftSocket。

import SwiftSocket

let client = TCPClient(address: "your_server_address", port: 80)
 switch client.connect(timeout: 10) {
   case .success:
     // Connection successful
     switch client.send(string: "GET / HTTP/1.0\n\n" ) {
          case .success:
            guard let data = client.read(1024*10) else { return }

            if let response = String(bytes: data, encoding: .utf8) {
              print(response)
            }
          case .failure(let error):
            print(error)
        }
   case .failure(let error):
     // connection failure
 }
于 2018-03-15T08:58:07.323 回答