如何在 Swift 4 开始流会话后开始接收流数据

How to start receiving streaming data after stream session started with Swift 4

我一直在寻找如何在流启动后开始接收数据,但一定有遗漏的东西。在以下函数中,我从其他对等方获得了流名称,但无法开始接收流数据。

func stream(_ aStream: Stream, handle eventCode: Stream.Event) 函数永远不会被调用。

此外,outputStream!。streamStatus.rawValue returns 当我们尝试发送流数据时,在接收方接收到流名称后,发送方 2。

rawValue == 2 表示; 无法联系到连接的远端,或者连接因其他原因被切断。

但是我们仍然收到流名称。

 func session(_ session: MCSession, didReceive stream: InputStream,      withName streamName: String, fromPeer peerID: MCPeerID) {


    print("received stream = \(streamName)")


    stream.delegate = self as? StreamDelegate
    stream.schedule(in: RunLoop.main, forMode: RunLoop.Mode.default)
    stream.open()

}


func stream(_ aStream: Stream, handle eventCode: Stream.Event){
   // Code to handle stream data.

}

申请委托列表是这样的

class ViewController:   UIViewController,
                    MCSessionDelegate,
                    MCNearbyServiceAdvertiserDelegate,
                    MCNearbyServiceBrowserDelegate,
                   {

"StreamDelegate"应该已经添加了

class ViewController:   UIViewController,
                    MCSessionDelegate,
                    MCNearbyServiceAdvertiserDelegate,
                    MCNearbyServiceBrowserDelegate,
                    StreamDelegate{

之后

 func session(_ session: MCSession, didReceive stream: InputStream,      withName streamName: String, fromPeer peerID: MCPeerID) {}

开始获取流。